Path: blob/main/vendor/github.com/spf13/pflag/bool_slice.go
2875 views
package pflag12import (3"io"4"strconv"5"strings"6)78// -- boolSlice Value9type boolSliceValue struct {10value *[]bool11changed bool12}1314func newBoolSliceValue(val []bool, p *[]bool) *boolSliceValue {15bsv := new(boolSliceValue)16bsv.value = p17*bsv.value = val18return bsv19}2021// Set converts, and assigns, the comma-separated boolean argument string representation as the []bool value of this flag.22// If Set is called on a flag that already has a []bool assigned, the newly converted values will be appended.23func (s *boolSliceValue) Set(val string) error {2425// remove all quote characters26rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")2728// read flag arguments with CSV parser29boolStrSlice, err := readAsCSV(rmQuote.Replace(val))30if err != nil && err != io.EOF {31return err32}3334// parse boolean values into slice35out := make([]bool, 0, len(boolStrSlice))36for _, boolStr := range boolStrSlice {37b, err := strconv.ParseBool(strings.TrimSpace(boolStr))38if err != nil {39return err40}41out = append(out, b)42}4344if !s.changed {45*s.value = out46} else {47*s.value = append(*s.value, out...)48}4950s.changed = true5152return nil53}5455// Type returns a string that uniquely represents this flag's type.56func (s *boolSliceValue) Type() string {57return "boolSlice"58}5960// String defines a "native" format for this boolean slice flag value.61func (s *boolSliceValue) String() string {6263boolStrSlice := make([]string, len(*s.value))64for i, b := range *s.value {65boolStrSlice[i] = strconv.FormatBool(b)66}6768out, _ := writeAsCSV(boolStrSlice)6970return "[" + out + "]"71}7273func (s *boolSliceValue) fromString(val string) (bool, error) {74return strconv.ParseBool(val)75}7677func (s *boolSliceValue) toString(val bool) string {78return strconv.FormatBool(val)79}8081func (s *boolSliceValue) Append(val string) error {82i, err := s.fromString(val)83if err != nil {84return err85}86*s.value = append(*s.value, i)87return nil88}8990func (s *boolSliceValue) Replace(val []string) error {91out := make([]bool, len(val))92for i, d := range val {93var err error94out[i], err = s.fromString(d)95if err != nil {96return err97}98}99*s.value = out100return nil101}102103func (s *boolSliceValue) GetSlice() []string {104out := make([]string, len(*s.value))105for i, d := range *s.value {106out[i] = s.toString(d)107}108return out109}110111func boolSliceConv(val string) (interface{}, error) {112val = strings.Trim(val, "[]")113// Empty string would cause a slice with one (empty) entry114if len(val) == 0 {115return []bool{}, nil116}117ss := strings.Split(val, ",")118out := make([]bool, len(ss))119for i, t := range ss {120var err error121out[i], err = strconv.ParseBool(t)122if err != nil {123return nil, err124}125}126return out, nil127}128129// GetBoolSlice returns the []bool value of a flag with the given name.130func (f *FlagSet) GetBoolSlice(name string) ([]bool, error) {131val, err := f.getFlagType(name, "boolSlice", boolSliceConv)132if err != nil {133return []bool{}, err134}135return val.([]bool), nil136}137138// BoolSliceVar defines a boolSlice flag with specified name, default value, and usage string.139// The argument p points to a []bool variable in which to store the value of the flag.140func (f *FlagSet) BoolSliceVar(p *[]bool, name string, value []bool, usage string) {141f.VarP(newBoolSliceValue(value, p), name, "", usage)142}143144// BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash.145func (f *FlagSet) BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) {146f.VarP(newBoolSliceValue(value, p), name, shorthand, usage)147}148149// BoolSliceVar defines a []bool flag with specified name, default value, and usage string.150// The argument p points to a []bool variable in which to store the value of the flag.151func BoolSliceVar(p *[]bool, name string, value []bool, usage string) {152CommandLine.VarP(newBoolSliceValue(value, p), name, "", usage)153}154155// BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash.156func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) {157CommandLine.VarP(newBoolSliceValue(value, p), name, shorthand, usage)158}159160// BoolSlice defines a []bool flag with specified name, default value, and usage string.161// The return value is the address of a []bool variable that stores the value of the flag.162func (f *FlagSet) BoolSlice(name string, value []bool, usage string) *[]bool {163p := []bool{}164f.BoolSliceVarP(&p, name, "", value, usage)165return &p166}167168// BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash.169func (f *FlagSet) BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool {170p := []bool{}171f.BoolSliceVarP(&p, name, shorthand, value, usage)172return &p173}174175// BoolSlice defines a []bool flag with specified name, default value, and usage string.176// The return value is the address of a []bool variable that stores the value of the flag.177func BoolSlice(name string, value []bool, usage string) *[]bool {178return CommandLine.BoolSliceP(name, "", value, usage)179}180181// BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash.182func BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool {183return CommandLine.BoolSliceP(name, shorthand, value, usage)184}185186187