podman
86 строк · 3.2 Кб
1package 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
12return (*durationValue)(p)
13}
14
15func (d *durationValue) Set(s string) error {
16v, err := time.ParseDuration(s)
17*d = durationValue(v)
18return err
19}
20
21func (d *durationValue) Type() string {
22return "duration"
23}
24
25func (d *durationValue) String() string { return (*time.Duration)(d).String() }
26
27func durationConv(sval string) (interface{}, error) {
28return 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) {
33val, err := f.getFlagType(name, "duration", durationConv)
34if err != nil {
35return 0, err
36}
37return 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) {
43f.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) {
48f.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) {
54CommandLine.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) {
59CommandLine.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 {
65p := new(time.Duration)
66f.DurationVarP(p, name, "", value, usage)
67return 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 {
72p := new(time.Duration)
73f.DurationVarP(p, name, shorthand, value, usage)
74return 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 {
80return 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 {
85return CommandLine.DurationP(name, shorthand, value, usage)
86}
87