Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame^] | 1 | package pflag |
| 2 | |
| 3 | // -- stringArray Value |
| 4 | type stringArrayValue struct { |
| 5 | value *[]string |
| 6 | changed bool |
| 7 | } |
| 8 | |
| 9 | func newStringArrayValue(val []string, p *[]string) *stringArrayValue { |
| 10 | ssv := new(stringArrayValue) |
| 11 | ssv.value = p |
| 12 | *ssv.value = val |
| 13 | return ssv |
| 14 | } |
| 15 | |
| 16 | func (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 | |
| 26 | func (s *stringArrayValue) Type() string { |
| 27 | return "stringArray" |
| 28 | } |
| 29 | |
| 30 | func (s *stringArrayValue) String() string { |
| 31 | str, _ := writeAsCSV(*s.value) |
| 32 | return "[" + str + "]" |
| 33 | } |
| 34 | |
| 35 | func 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 |
| 45 | func (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. |
| 56 | func (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. |
| 61 | func (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. |
| 68 | func 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. |
| 73 | func 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. |
| 80 | func (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. |
| 87 | func (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. |
| 96 | func 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. |
| 101 | func StringArrayP(name, shorthand string, value []string, usage string) *[]string { |
| 102 | return CommandLine.StringArrayP(name, shorthand, value, usage) |
| 103 | } |