Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame^] | 1 | package pflag |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/csv" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // -- stringSlice Value |
| 10 | type stringSliceValue struct { |
| 11 | value *[]string |
| 12 | changed bool |
| 13 | } |
| 14 | |
| 15 | func newStringSliceValue(val []string, p *[]string) *stringSliceValue { |
| 16 | ssv := new(stringSliceValue) |
| 17 | ssv.value = p |
| 18 | *ssv.value = val |
| 19 | return ssv |
| 20 | } |
| 21 | |
| 22 | func readAsCSV(val string) ([]string, error) { |
| 23 | if val == "" { |
| 24 | return []string{}, nil |
| 25 | } |
| 26 | stringReader := strings.NewReader(val) |
| 27 | csvReader := csv.NewReader(stringReader) |
| 28 | return csvReader.Read() |
| 29 | } |
| 30 | |
| 31 | func writeAsCSV(vals []string) (string, error) { |
| 32 | b := &bytes.Buffer{} |
| 33 | w := csv.NewWriter(b) |
| 34 | err := w.Write(vals) |
| 35 | if err != nil { |
| 36 | return "", err |
| 37 | } |
| 38 | w.Flush() |
| 39 | return strings.TrimSuffix(b.String(), "\n"), nil |
| 40 | } |
| 41 | |
| 42 | func (s *stringSliceValue) Set(val string) error { |
| 43 | v, err := readAsCSV(val) |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | if !s.changed { |
| 48 | *s.value = v |
| 49 | } else { |
| 50 | *s.value = append(*s.value, v...) |
| 51 | } |
| 52 | s.changed = true |
| 53 | return nil |
| 54 | } |
| 55 | |
| 56 | func (s *stringSliceValue) Type() string { |
| 57 | return "stringSlice" |
| 58 | } |
| 59 | |
| 60 | func (s *stringSliceValue) String() string { |
| 61 | str, _ := writeAsCSV(*s.value) |
| 62 | return "[" + str + "]" |
| 63 | } |
| 64 | |
| 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 |
| 68 | if len(sval) == 0 { |
| 69 | return []string{}, nil |
| 70 | } |
| 71 | return readAsCSV(sval) |
| 72 | } |
| 73 | |
| 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) |
| 77 | if err != nil { |
| 78 | return []string{}, err |
| 79 | } |
| 80 | return val.([]string), nil |
| 81 | } |
| 82 | |
| 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. |
| 86 | // For example: |
| 87 | // --ss="v1,v2" -ss="v3" |
| 88 | // will result in |
| 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) |
| 92 | } |
| 93 | |
| 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) |
| 97 | } |
| 98 | |
| 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. |
| 102 | // For example: |
| 103 | // --ss="v1,v2" -ss="v3" |
| 104 | // will result in |
| 105 | // []string{"v1", "v2", "v3"} |
| 106 | func StringSliceVar(p *[]string, name string, value []string, usage string) { |
| 107 | CommandLine.VarP(newStringSliceValue(value, p), name, "", usage) |
| 108 | } |
| 109 | |
| 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) |
| 113 | } |
| 114 | |
| 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. |
| 118 | // For example: |
| 119 | // --ss="v1,v2" -ss="v3" |
| 120 | // will result in |
| 121 | // []string{"v1", "v2", "v3"} |
| 122 | func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string { |
| 123 | p := []string{} |
| 124 | f.StringSliceVarP(&p, name, "", value, usage) |
| 125 | return &p |
| 126 | } |
| 127 | |
| 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 { |
| 130 | p := []string{} |
| 131 | f.StringSliceVarP(&p, name, shorthand, value, usage) |
| 132 | return &p |
| 133 | } |
| 134 | |
| 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. |
| 138 | // For example: |
| 139 | // --ss="v1,v2" -ss="v3" |
| 140 | // will result in |
| 141 | // []string{"v1", "v2", "v3"} |
| 142 | func StringSlice(name string, value []string, usage string) *[]string { |
| 143 | return CommandLine.StringSliceP(name, "", value, usage) |
| 144 | } |
| 145 | |
| 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) |
| 149 | } |