blob: e9debef88ee08abab1332c5359c76b693577ded9 [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001package pflag
2
3import (
4 "time"
5)
6
7// -- time.Duration Value
8type durationValue time.Duration
9
10func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
11 *p = val
12 return (*durationValue)(p)
13}
14
15func (d *durationValue) Set(s string) error {
16 v, err := time.ParseDuration(s)
17 *d = durationValue(v)
18 return err
19}
20
21func (d *durationValue) Type() string {
22 return "duration"
23}
24
25func (d *durationValue) String() string { return (*time.Duration)(d).String() }
26
27func 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
32func (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.
42func (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.
47func (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.
53func 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.
58func 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.
64func (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.
71func (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.
79func 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.
84func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration {
85 return CommandLine.DurationP(name, shorthand, value, usage)
86}