9 // -- stringSlice Value
10 type stringSliceValue struct {
15 func newStringSliceValue(val []string, p *[]string) *stringSliceValue {
16 ssv := new(stringSliceValue)
22 func readAsCSV(val string) ([]string, error) {
24 return []string{}, nil
26 stringReader := strings.NewReader(val)
27 csvReader := csv.NewReader(stringReader)
28 return csvReader.Read()
31 func writeAsCSV(vals []string) (string, error) {
39 return strings.TrimSuffix(b.String(), "\n"), nil
42 func (s *stringSliceValue) Set(val string) error {
43 v, err := readAsCSV(val)
50 *s.value = append(*s.value, v...)
56 func (s *stringSliceValue) Type() string {
60 func (s *stringSliceValue) String() string {
61 str, _ := writeAsCSV(*s.value)
62 return "[" + str + "]"
65 func stringSliceConv(sval string) (interface{}, error) {
66 sval = sval[1 : len(sval)-1]
67 // An empty string would cause a slice with one (empty) string
69 return []string{}, nil
71 return readAsCSV(sval)
74 // GetStringSlice return the []string value of a flag with the given name
75 func (f *FlagSet) GetStringSlice(name string) ([]string, error) {
76 val, err := f.getFlagType(name, "stringSlice", stringSliceConv)
78 return []string{}, err
80 return val.([]string), nil
83 // StringSliceVar defines a string flag with specified name, default value, and usage string.
84 // The argument p points to a []string variable in which to store the value of the flag.
85 // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
87 // --ss="v1,v2" -ss="v3"
89 // []string{"v1", "v2", "v3"}
90 func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) {
91 f.VarP(newStringSliceValue(value, p), name, "", usage)
94 // StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
95 func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
96 f.VarP(newStringSliceValue(value, p), name, shorthand, usage)
99 // StringSliceVar defines a string flag with specified name, default value, and usage string.
100 // The argument p points to a []string variable in which to store the value of the flag.
101 // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
103 // --ss="v1,v2" -ss="v3"
105 // []string{"v1", "v2", "v3"}
106 func StringSliceVar(p *[]string, name string, value []string, usage string) {
107 CommandLine.VarP(newStringSliceValue(value, p), name, "", usage)
110 // StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
111 func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
112 CommandLine.VarP(newStringSliceValue(value, p), name, shorthand, usage)
115 // StringSlice defines a string flag with specified name, default value, and usage string.
116 // The return value is the address of a []string variable that stores the value of the flag.
117 // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
119 // --ss="v1,v2" -ss="v3"
121 // []string{"v1", "v2", "v3"}
122 func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
124 f.StringSliceVarP(&p, name, "", value, usage)
128 // StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
129 func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage string) *[]string {
131 f.StringSliceVarP(&p, name, shorthand, value, usage)
135 // StringSlice defines a string flag with specified name, default value, and usage string.
136 // The return value is the address of a []string variable that stores the value of the flag.
137 // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
139 // --ss="v1,v2" -ss="v3"
141 // []string{"v1", "v2", "v3"}
142 func StringSlice(name string, value []string, usage string) *[]string {
143 return CommandLine.StringSliceP(name, "", value, usage)
146 // StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
147 func StringSliceP(name, shorthand string, value []string, usage string) *[]string {
148 return CommandLine.StringSliceP(name, shorthand, value, usage)