10 type boolSliceValue struct {
15 func newBoolSliceValue(val []bool, p *[]bool) *boolSliceValue {
16 bsv := new(boolSliceValue)
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 {
26 // remove all quote characters
27 rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")
29 // read flag arguments with CSV parser
30 boolStrSlice, err := readAsCSV(rmQuote.Replace(val))
31 if err != nil && err != io.EOF {
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))
48 *s.value = append(*s.value, out...)
56 // Type returns a string that uniquely represents this flag's type.
57 func (s *boolSliceValue) Type() string {
61 // String defines a "native" format for this boolean slice flag value.
62 func (s *boolSliceValue) String() string {
64 boolStrSlice := make([]string, len(*s.value))
65 for i, b := range *s.value {
66 boolStrSlice[i] = strconv.FormatBool(b)
69 out, _ := writeAsCSV(boolStrSlice)
71 return "[" + out + "]"
74 func boolSliceConv(val string) (interface{}, error) {
75 val = strings.Trim(val, "[]")
76 // Empty string would cause a slice with one (empty) entry
80 ss := strings.Split(val, ",")
81 out := make([]bool, len(ss))
82 for i, t := range ss {
84 out[i], err = strconv.ParseBool(t)
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)
98 return val.([]bool), nil
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)
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)
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)
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)
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 {
127 f.BoolSliceVarP(&p, name, "", value, usage)
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 {
134 f.BoolSliceVarP(&p, name, shorthand, value, usage)
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)
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)