Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | package pflag |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // -- boolSlice Value |
| 10 | type boolSliceValue struct { |
| 11 | value *[]bool |
| 12 | changed bool |
| 13 | } |
| 14 | |
| 15 | func newBoolSliceValue(val []bool, p *[]bool) *boolSliceValue { |
| 16 | bsv := new(boolSliceValue) |
| 17 | bsv.value = p |
| 18 | *bsv.value = val |
| 19 | return bsv |
| 20 | } |
| 21 | |
| 22 | // Set converts, and assigns, the comma-separated boolean argument string representation as the []bool value of this flag. |
| 23 | // If Set is called on a flag that already has a []bool assigned, the newly converted values will be appended. |
| 24 | func (s *boolSliceValue) Set(val string) error { |
| 25 | |
| 26 | // remove all quote characters |
| 27 | rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "") |
| 28 | |
| 29 | // read flag arguments with CSV parser |
| 30 | boolStrSlice, err := readAsCSV(rmQuote.Replace(val)) |
| 31 | if err != nil && err != io.EOF { |
| 32 | return err |
| 33 | } |
| 34 | |
| 35 | // parse boolean values into slice |
| 36 | out := make([]bool, 0, len(boolStrSlice)) |
| 37 | for _, boolStr := range boolStrSlice { |
| 38 | b, err := strconv.ParseBool(strings.TrimSpace(boolStr)) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | out = append(out, b) |
| 43 | } |
| 44 | |
| 45 | if !s.changed { |
| 46 | *s.value = out |
| 47 | } else { |
| 48 | *s.value = append(*s.value, out...) |
| 49 | } |
| 50 | |
| 51 | s.changed = true |
| 52 | |
| 53 | return nil |
| 54 | } |
| 55 | |
| 56 | // Type returns a string that uniquely represents this flag's type. |
| 57 | func (s *boolSliceValue) Type() string { |
| 58 | return "boolSlice" |
| 59 | } |
| 60 | |
| 61 | // String defines a "native" format for this boolean slice flag value. |
| 62 | func (s *boolSliceValue) String() string { |
| 63 | |
| 64 | boolStrSlice := make([]string, len(*s.value)) |
| 65 | for i, b := range *s.value { |
| 66 | boolStrSlice[i] = strconv.FormatBool(b) |
| 67 | } |
| 68 | |
| 69 | out, _ := writeAsCSV(boolStrSlice) |
| 70 | |
| 71 | return "[" + out + "]" |
| 72 | } |
| 73 | |
| 74 | func boolSliceConv(val string) (interface{}, error) { |
| 75 | val = strings.Trim(val, "[]") |
| 76 | // Empty string would cause a slice with one (empty) entry |
| 77 | if len(val) == 0 { |
| 78 | return []bool{}, nil |
| 79 | } |
| 80 | ss := strings.Split(val, ",") |
| 81 | out := make([]bool, len(ss)) |
| 82 | for i, t := range ss { |
| 83 | var err error |
| 84 | out[i], err = strconv.ParseBool(t) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | } |
| 89 | return out, nil |
| 90 | } |
| 91 | |
| 92 | // GetBoolSlice returns the []bool value of a flag with the given name. |
| 93 | func (f *FlagSet) GetBoolSlice(name string) ([]bool, error) { |
| 94 | val, err := f.getFlagType(name, "boolSlice", boolSliceConv) |
| 95 | if err != nil { |
| 96 | return []bool{}, err |
| 97 | } |
| 98 | return val.([]bool), nil |
| 99 | } |
| 100 | |
| 101 | // BoolSliceVar defines a boolSlice flag with specified name, default value, and usage string. |
| 102 | // The argument p points to a []bool variable in which to store the value of the flag. |
| 103 | func (f *FlagSet) BoolSliceVar(p *[]bool, name string, value []bool, usage string) { |
| 104 | f.VarP(newBoolSliceValue(value, p), name, "", usage) |
| 105 | } |
| 106 | |
| 107 | // BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash. |
| 108 | func (f *FlagSet) BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) { |
| 109 | f.VarP(newBoolSliceValue(value, p), name, shorthand, usage) |
| 110 | } |
| 111 | |
| 112 | // BoolSliceVar defines a []bool flag with specified name, default value, and usage string. |
| 113 | // The argument p points to a []bool variable in which to store the value of the flag. |
| 114 | func BoolSliceVar(p *[]bool, name string, value []bool, usage string) { |
| 115 | CommandLine.VarP(newBoolSliceValue(value, p), name, "", usage) |
| 116 | } |
| 117 | |
| 118 | // BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash. |
| 119 | func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) { |
| 120 | CommandLine.VarP(newBoolSliceValue(value, p), name, shorthand, usage) |
| 121 | } |
| 122 | |
| 123 | // BoolSlice defines a []bool flag with specified name, default value, and usage string. |
| 124 | // The return value is the address of a []bool variable that stores the value of the flag. |
| 125 | func (f *FlagSet) BoolSlice(name string, value []bool, usage string) *[]bool { |
| 126 | p := []bool{} |
| 127 | f.BoolSliceVarP(&p, name, "", value, usage) |
| 128 | return &p |
| 129 | } |
| 130 | |
| 131 | // BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash. |
| 132 | func (f *FlagSet) BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool { |
| 133 | p := []bool{} |
| 134 | f.BoolSliceVarP(&p, name, shorthand, value, usage) |
| 135 | return &p |
| 136 | } |
| 137 | |
| 138 | // BoolSlice defines a []bool flag with specified name, default value, and usage string. |
| 139 | // The return value is the address of a []bool variable that stores the value of the flag. |
| 140 | func BoolSlice(name string, value []bool, usage string) *[]bool { |
| 141 | return CommandLine.BoolSliceP(name, "", value, usage) |
| 142 | } |
| 143 | |
| 144 | // BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash. |
| 145 | func BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool { |
| 146 | return CommandLine.BoolSliceP(name, shorthand, value, usage) |
| 147 | } |