blob: fa7bc60187a793af7b9aabc709c2787e36bb8c90 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001package pflag
2
3// -- stringArray Value
4type stringArrayValue struct {
5 value *[]string
6 changed bool
7}
8
9func newStringArrayValue(val []string, p *[]string) *stringArrayValue {
10 ssv := new(stringArrayValue)
11 ssv.value = p
12 *ssv.value = val
13 return ssv
14}
15
16func (s *stringArrayValue) Set(val string) error {
17 if !s.changed {
18 *s.value = []string{val}
19 s.changed = true
20 } else {
21 *s.value = append(*s.value, val)
22 }
23 return nil
24}
25
26func (s *stringArrayValue) Type() string {
27 return "stringArray"
28}
29
30func (s *stringArrayValue) String() string {
31 str, _ := writeAsCSV(*s.value)
32 return "[" + str + "]"
33}
34
35func stringArrayConv(sval string) (interface{}, error) {
36 sval = sval[1 : len(sval)-1]
37 // An empty string would cause a array with one (empty) string
38 if len(sval) == 0 {
39 return []string{}, nil
40 }
41 return readAsCSV(sval)
42}
43
44// GetStringArray return the []string value of a flag with the given name
45func (f *FlagSet) GetStringArray(name string) ([]string, error) {
46 val, err := f.getFlagType(name, "stringArray", stringArrayConv)
47 if err != nil {
48 return []string{}, err
49 }
50 return val.([]string), nil
51}
52
53// StringArrayVar defines a string flag with specified name, default value, and usage string.
54// The argument p points to a []string variable in which to store the values of the multiple flags.
55// The value of each argument will not try to be separated by comma. Use a StringSlice for that.
56func (f *FlagSet) StringArrayVar(p *[]string, name string, value []string, usage string) {
57 f.VarP(newStringArrayValue(value, p), name, "", usage)
58}
59
60// StringArrayVarP is like StringArrayVar, but accepts a shorthand letter that can be used after a single dash.
61func (f *FlagSet) StringArrayVarP(p *[]string, name, shorthand string, value []string, usage string) {
62 f.VarP(newStringArrayValue(value, p), name, shorthand, usage)
63}
64
65// StringArrayVar defines a string flag with specified name, default value, and usage string.
66// The argument p points to a []string variable in which to store the value of the flag.
67// The value of each argument will not try to be separated by comma. Use a StringSlice for that.
68func StringArrayVar(p *[]string, name string, value []string, usage string) {
69 CommandLine.VarP(newStringArrayValue(value, p), name, "", usage)
70}
71
72// StringArrayVarP is like StringArrayVar, but accepts a shorthand letter that can be used after a single dash.
73func StringArrayVarP(p *[]string, name, shorthand string, value []string, usage string) {
74 CommandLine.VarP(newStringArrayValue(value, p), name, shorthand, usage)
75}
76
77// StringArray defines a string flag with specified name, default value, and usage string.
78// The return value is the address of a []string variable that stores the value of the flag.
79// The value of each argument will not try to be separated by comma. Use a StringSlice for that.
80func (f *FlagSet) StringArray(name string, value []string, usage string) *[]string {
81 p := []string{}
82 f.StringArrayVarP(&p, name, "", value, usage)
83 return &p
84}
85
86// StringArrayP is like StringArray, but accepts a shorthand letter that can be used after a single dash.
87func (f *FlagSet) StringArrayP(name, shorthand string, value []string, usage string) *[]string {
88 p := []string{}
89 f.StringArrayVarP(&p, name, shorthand, value, usage)
90 return &p
91}
92
93// StringArray defines a string flag with specified name, default value, and usage string.
94// The return value is the address of a []string variable that stores the value of the flag.
95// The value of each argument will not try to be separated by comma. Use a StringSlice for that.
96func StringArray(name string, value []string, usage string) *[]string {
97 return CommandLine.StringArrayP(name, "", value, usage)
98}
99
100// StringArrayP is like StringArray, but accepts a shorthand letter that can be used after a single dash.
101func StringArrayP(name, shorthand string, value []string, usage string) *[]string {
102 return CommandLine.StringArrayP(name, shorthand, value, usage)
103}