Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | package pflag |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | ) |
| 6 | |
| 7 | // -- time.Duration Value |
| 8 | type durationValue time.Duration |
| 9 | |
| 10 | func newDurationValue(val time.Duration, p *time.Duration) *durationValue { |
| 11 | *p = val |
| 12 | return (*durationValue)(p) |
| 13 | } |
| 14 | |
| 15 | func (d *durationValue) Set(s string) error { |
| 16 | v, err := time.ParseDuration(s) |
| 17 | *d = durationValue(v) |
| 18 | return err |
| 19 | } |
| 20 | |
| 21 | func (d *durationValue) Type() string { |
| 22 | return "duration" |
| 23 | } |
| 24 | |
| 25 | func (d *durationValue) String() string { return (*time.Duration)(d).String() } |
| 26 | |
| 27 | func durationConv(sval string) (interface{}, error) { |
| 28 | return time.ParseDuration(sval) |
| 29 | } |
| 30 | |
| 31 | // GetDuration return the duration value of a flag with the given name |
| 32 | func (f *FlagSet) GetDuration(name string) (time.Duration, error) { |
| 33 | val, err := f.getFlagType(name, "duration", durationConv) |
| 34 | if err != nil { |
| 35 | return 0, err |
| 36 | } |
| 37 | return val.(time.Duration), nil |
| 38 | } |
| 39 | |
| 40 | // DurationVar defines a time.Duration flag with specified name, default value, and usage string. |
| 41 | // The argument p points to a time.Duration variable in which to store the value of the flag. |
| 42 | func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) { |
| 43 | f.VarP(newDurationValue(value, p), name, "", usage) |
| 44 | } |
| 45 | |
| 46 | // DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash. |
| 47 | func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) { |
| 48 | f.VarP(newDurationValue(value, p), name, shorthand, usage) |
| 49 | } |
| 50 | |
| 51 | // DurationVar defines a time.Duration flag with specified name, default value, and usage string. |
| 52 | // The argument p points to a time.Duration variable in which to store the value of the flag. |
| 53 | func DurationVar(p *time.Duration, name string, value time.Duration, usage string) { |
| 54 | CommandLine.VarP(newDurationValue(value, p), name, "", usage) |
| 55 | } |
| 56 | |
| 57 | // DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash. |
| 58 | func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) { |
| 59 | CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage) |
| 60 | } |
| 61 | |
| 62 | // Duration defines a time.Duration flag with specified name, default value, and usage string. |
| 63 | // The return value is the address of a time.Duration variable that stores the value of the flag. |
| 64 | func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration { |
| 65 | p := new(time.Duration) |
| 66 | f.DurationVarP(p, name, "", value, usage) |
| 67 | return p |
| 68 | } |
| 69 | |
| 70 | // DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash. |
| 71 | func (f *FlagSet) DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration { |
| 72 | p := new(time.Duration) |
| 73 | f.DurationVarP(p, name, shorthand, value, usage) |
| 74 | return p |
| 75 | } |
| 76 | |
| 77 | // Duration defines a time.Duration flag with specified name, default value, and usage string. |
| 78 | // The return value is the address of a time.Duration variable that stores the value of the flag. |
| 79 | func Duration(name string, value time.Duration, usage string) *time.Duration { |
| 80 | return CommandLine.DurationP(name, "", value, usage) |
| 81 | } |
| 82 | |
| 83 | // DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash. |
| 84 | func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration { |
| 85 | return CommandLine.DurationP(name, shorthand, value, usage) |
| 86 | } |