blob: 52c6b6dc1041f48e56a84c79369f0a15c7c3bee7 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001package pflag
2
3import (
4 "fmt"
5 "strings"
6 "time"
7)
8
9// -- durationSlice Value
10type durationSliceValue struct {
11 value *[]time.Duration
12 changed bool
13}
14
15func newDurationSliceValue(val []time.Duration, p *[]time.Duration) *durationSliceValue {
16 dsv := new(durationSliceValue)
17 dsv.value = p
18 *dsv.value = val
19 return dsv
20}
21
22func (s *durationSliceValue) Set(val string) error {
23 ss := strings.Split(val, ",")
24 out := make([]time.Duration, len(ss))
25 for i, d := range ss {
26 var err error
27 out[i], err = time.ParseDuration(d)
28 if err != nil {
29 return err
30 }
31
32 }
33 if !s.changed {
34 *s.value = out
35 } else {
36 *s.value = append(*s.value, out...)
37 }
38 s.changed = true
39 return nil
40}
41
42func (s *durationSliceValue) Type() string {
43 return "durationSlice"
44}
45
46func (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)
50 }
51 return "[" + strings.Join(out, ",") + "]"
52}
53
54func durationSliceConv(val string) (interface{}, error) {
55 val = strings.Trim(val, "[]")
56 // Empty string would cause a slice with one (empty) entry
57 if len(val) == 0 {
58 return []time.Duration{}, nil
59 }
60 ss := strings.Split(val, ",")
61 out := make([]time.Duration, len(ss))
62 for i, d := range ss {
63 var err error
64 out[i], err = time.ParseDuration(d)
65 if err != nil {
66 return nil, err
67 }
68
69 }
70 return out, nil
71}
72
73// GetDurationSlice returns the []time.Duration value of a flag with the given name
74func (f *FlagSet) GetDurationSlice(name string) ([]time.Duration, error) {
75 val, err := f.getFlagType(name, "durationSlice", durationSliceConv)
76 if err != nil {
77 return []time.Duration{}, err
78 }
79 return val.([]time.Duration), nil
80}
81
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.
84func (f *FlagSet) DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
85 f.VarP(newDurationSliceValue(value, p), name, "", usage)
86}
87
88// DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.
89func (f *FlagSet) DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {
90 f.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
91}
92
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.
95func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
96 CommandLine.VarP(newDurationSliceValue(value, p), name, "", usage)
97}
98
99// DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.
100func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {
101 CommandLine.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
102}
103
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.
106func (f *FlagSet) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {
107 p := []time.Duration{}
108 f.DurationSliceVarP(&p, name, "", value, usage)
109 return &p
110}
111
112// DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.
113func (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)
116 return &p
117}
118
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.
121func DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {
122 return CommandLine.DurationSliceP(name, "", value, usage)
123}
124
125// DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.
126func DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration {
127 return CommandLine.DurationSliceP(name, shorthand, value, usage)
128}