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 (s *durationSliceValue) fromString(val string) (time.Duration, error) {
55
return time.ParseDuration(val)
58
func (s *durationSliceValue) toString(val time.Duration) string {
59
return fmt.Sprintf("%s", val)
62
func (s *durationSliceValue) Append(val string) error {
63
i, err := s.fromString(val)
67
*s.value = append(*s.value, i)
71
func (s *durationSliceValue) Replace(val []string) error {
72
out := make([]time.Duration, len(val))
73
for i, d := range val {
75
out[i], err = s.fromString(d)
84
func (s *durationSliceValue) GetSlice() []string {
85
out := make([]string, len(*s.value))
86
for i, d := range *s.value {
87
out[i] = s.toString(d)
92
func durationSliceConv(val string) (interface{}, error) {
93
val = strings.Trim(val, "[]")
94
// Empty string would cause a slice with one (empty) entry
96
return []time.Duration{}, nil
98
ss := strings.Split(val, ",")
99
out := make([]time.Duration, len(ss))
100
for i, d := range ss {
102
out[i], err = time.ParseDuration(d)
111
// GetDurationSlice returns the []time.Duration value of a flag with the given name
112
func (f *FlagSet) GetDurationSlice(name string) ([]time.Duration, error) {
113
val, err := f.getFlagType(name, "durationSlice", durationSliceConv)
115
return []time.Duration{}, err
117
return val.([]time.Duration), nil
120
// DurationSliceVar defines a durationSlice flag with specified name, default value, and usage string.
121
// The argument p points to a []time.Duration variable in which to store the value of the flag.
122
func (f *FlagSet) DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
123
f.VarP(newDurationSliceValue(value, p), name, "", usage)
126
// DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.
127
func (f *FlagSet) DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {
128
f.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
131
// DurationSliceVar defines a duration[] flag with specified name, default value, and usage string.
132
// The argument p points to a duration[] variable in which to store the value of the flag.
133
func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
134
CommandLine.VarP(newDurationSliceValue(value, p), name, "", usage)
137
// DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.
138
func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) {
139
CommandLine.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
142
// DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.
143
// The return value is the address of a []time.Duration variable that stores the value of the flag.
144
func (f *FlagSet) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {
145
p := []time.Duration{}
146
f.DurationSliceVarP(&p, name, "", value, usage)
150
// DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.
151
func (f *FlagSet) DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration {
152
p := []time.Duration{}
153
f.DurationSliceVarP(&p, name, shorthand, value, usage)
157
// DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.
158
// The return value is the address of a []time.Duration variable that stores the value of the flag.
159
func DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {
160
return CommandLine.DurationSliceP(name, "", value, usage)
163
// DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.
164
func DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration {
165
return CommandLine.DurationSliceP(name, shorthand, value, usage)