1 package pflag
2
3 import (
4 "io"
5 "strconv"
6 "strings"
7 )
8
9
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
23
24 func (s *boolSliceValue) Set(val string) error {
25
26
27 rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")
28
29
30 boolStrSlice, err := readAsCSV(rmQuote.Replace(val))
31 if err != nil && err != io.EOF {
32 return err
33 }
34
35
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
57 func (s *boolSliceValue) Type() string {
58 return "boolSlice"
59 }
60
61
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 (s *boolSliceValue) fromString(val string) (bool, error) {
75 return strconv.ParseBool(val)
76 }
77
78 func (s *boolSliceValue) toString(val bool) string {
79 return strconv.FormatBool(val)
80 }
81
82 func (s *boolSliceValue) Append(val string) error {
83 i, err := s.fromString(val)
84 if err != nil {
85 return err
86 }
87 *s.value = append(*s.value, i)
88 return nil
89 }
90
91 func (s *boolSliceValue) Replace(val []string) error {
92 out := make([]bool, len(val))
93 for i, d := range val {
94 var err error
95 out[i], err = s.fromString(d)
96 if err != nil {
97 return err
98 }
99 }
100 *s.value = out
101 return nil
102 }
103
104 func (s *boolSliceValue) GetSlice() []string {
105 out := make([]string, len(*s.value))
106 for i, d := range *s.value {
107 out[i] = s.toString(d)
108 }
109 return out
110 }
111
112 func boolSliceConv(val string) (interface{}, error) {
113 val = strings.Trim(val, "[]")
114
115 if len(val) == 0 {
116 return []bool{}, nil
117 }
118 ss := strings.Split(val, ",")
119 out := make([]bool, len(ss))
120 for i, t := range ss {
121 var err error
122 out[i], err = strconv.ParseBool(t)
123 if err != nil {
124 return nil, err
125 }
126 }
127 return out, nil
128 }
129
130
131 func (f *FlagSet) GetBoolSlice(name string) ([]bool, error) {
132 val, err := f.getFlagType(name, "boolSlice", boolSliceConv)
133 if err != nil {
134 return []bool{}, err
135 }
136 return val.([]bool), nil
137 }
138
139
140
141 func (f *FlagSet) BoolSliceVar(p *[]bool, name string, value []bool, usage string) {
142 f.VarP(newBoolSliceValue(value, p), name, "", usage)
143 }
144
145
146 func (f *FlagSet) BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) {
147 f.VarP(newBoolSliceValue(value, p), name, shorthand, usage)
148 }
149
150
151
152 func BoolSliceVar(p *[]bool, name string, value []bool, usage string) {
153 CommandLine.VarP(newBoolSliceValue(value, p), name, "", usage)
154 }
155
156
157 func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) {
158 CommandLine.VarP(newBoolSliceValue(value, p), name, shorthand, usage)
159 }
160
161
162
163 func (f *FlagSet) BoolSlice(name string, value []bool, usage string) *[]bool {
164 p := []bool{}
165 f.BoolSliceVarP(&p, name, "", value, usage)
166 return &p
167 }
168
169
170 func (f *FlagSet) BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool {
171 p := []bool{}
172 f.BoolSliceVarP(&p, name, shorthand, value, usage)
173 return &p
174 }
175
176
177
178 func BoolSlice(name string, value []bool, usage string) *[]bool {
179 return CommandLine.BoolSliceP(name, "", value, usage)
180 }
181
182
183 func BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool {
184 return CommandLine.BoolSliceP(name, shorthand, value, usage)
185 }
186
View as plain text