9 // -- durationSlice Value
10 type durationSliceValue struct {
11 value *[]time.Duration
15 func newDurationSliceValue(val []time.Duration, p *[]time.Duration) *durationSliceValue {
16 dsv := new(durationSliceValue)
22 func (s *durationSliceValue) Set(val string) error {
23 ss := strings.Split(val, ",")
24 out := make([]time.Duration, len(ss))
25 for i, d := range ss {
27 out[i], err = time.ParseDuration(d)
36 *s.value = append(*s.value, out...)
42 func (s *durationSliceValue) Type() string {
43 return "durationSlice"
46 func (s *durationSliceValue) String() string {
47 out := make([]string, len(*s.value))
48 for i, d := range *s.value {
49 out[i] = fmt.Sprintf("%s", d)
51 return "[" + strings.Join(out, ",") + "]"
54 func durationSliceConv(val string) (interface{}, error) {
55 val = strings.Trim(val, "[]")
56 // Empty string would cause a slice with one (empty) entry
58 return []time.Duration{}, nil
60 ss := strings.Split(val, ",")
61 out := make([]time.Duration, len(ss))
62 for i, d := range ss {
64 out[i], err = time.ParseDuration(d)
73 // GetDurationSlice returns the []time.Duration value of a flag with the given name
74 func (f *FlagSet) GetDurationSlice(name string) ([]time.Duration, error) {
75 val, err := f.getFlagType(name, "durationSlice", durationSliceConv)
77 return []time.Duration{}, err
79 return val.([]time.Duration), nil
82 // DurationSliceVar defines a durationSlice flag with specified name, default value, and usage string.
83 // The argument p points to a []time.Duration variable in which to store the value of the flag.
84 func (f *FlagSet) DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
85 f.VarP(newDurationSliceValue(value, p), name, "", usage)
88 // DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.
89 func (f *FlagSet) DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {
90 f.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
93 // DurationSliceVar defines a duration[] flag with specified name, default value, and usage string.
94 // The argument p points to a duration[] variable in which to store the value of the flag.
95 func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
96 CommandLine.VarP(newDurationSliceValue(value, p), name, "", usage)
99 // DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.
100 func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {
101 CommandLine.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
104 // DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.
105 // The return value is the address of a []time.Duration variable that stores the value of the flag.
106 func (f *FlagSet) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {
107 p := []time.Duration{}
108 f.DurationSliceVarP(&p, name, "", value, usage)
112 // DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.
113 func (f *FlagSet) DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration {
114 p := []time.Duration{}
115 f.DurationSliceVarP(&p, name, shorthand, value, usage)
119 // DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.
120 // The return value is the address of a []time.Duration variable that stores the value of the flag.
121 func DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {
122 return CommandLine.DurationSliceP(name, "", value, usage)
125 // DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.
126 func DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration {
127 return CommandLine.DurationSliceP(name, shorthand, value, usage)