blob: 5af02f1a75a9d05d17b47fbae84848fe2237e373 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001package pflag
2
3import (
4 "io"
5 "strconv"
6 "strings"
7)
8
9// -- boolSlice Value
10type boolSliceValue struct {
11 value *[]bool
12 changed bool
13}
14
15func 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.
24func (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.
57func (s *boolSliceValue) Type() string {
58 return "boolSlice"
59}
60
61// String defines a "native" format for this boolean slice flag value.
62func (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
74func 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.
93func (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.
103func (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.
108func (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.
114func 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.
119func 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.
125func (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.
132func (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.
140func 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.
145func BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool {
146 return CommandLine.BoolSliceP(name, shorthand, value, usage)
147}