-
Notifications
You must be signed in to change notification settings - Fork 4
/
string_slice.go
47 lines (39 loc) · 1.25 KB
/
string_slice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// source: https://github.com/spf13/pflag/blob/master/string_slice.go
package conflag
type stringSliceValue struct {
value *[]string
changed bool
}
func newStringSliceValue(val []string, p *[]string) *stringSliceValue {
ssv := new(stringSliceValue)
ssv.value = p
*ssv.value = val
return ssv
}
func (s *stringSliceValue) Set(val string) error {
if !s.changed {
*s.value = []string{val}
s.changed = true
} else {
*s.value = append(*s.value, val)
}
return nil
}
func (s *stringSliceValue) Type() string {
return "stringSlice"
}
func (s *stringSliceValue) String() string {
return ""
}
// StringSliceVar defines a string flag with specified name, default value, and usage string.
// The argument p points to a []string variable in which to store the value of the flag.
func (c *Conflag) StringSliceVar(p *[]string, name string, value []string, usage string) {
c.Var(newStringSliceValue(value, p), name, usage)
}
// StringSlice defines a string flag with specified name, default value, and usage string.
// The return value is the address of a []string variable that stores the value of the flag.
func (c *Conflag) StringSlice(name string, value []string, usage string) *[]string {
p := []string{}
c.StringSliceVar(&p, name, value, usage)
return &p
}