podman
1package pflag2
3import (4"io"5"strconv"6"strings"7)
8
9// -- boolSlice Value
10type boolSliceValue struct {11value *[]bool12changed bool13}
14
15func newBoolSliceValue(val []bool, p *[]bool) *boolSliceValue {16bsv := new(boolSliceValue)17bsv.value = p18*bsv.value = val19return bsv20}
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 characters27rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")28
29// read flag arguments with CSV parser30boolStrSlice, err := readAsCSV(rmQuote.Replace(val))31if err != nil && err != io.EOF {32return err33}34
35// parse boolean values into slice36out := make([]bool, 0, len(boolStrSlice))37for _, boolStr := range boolStrSlice {38b, err := strconv.ParseBool(strings.TrimSpace(boolStr))39if err != nil {40return err41}42out = append(out, b)43}44
45if !s.changed {46*s.value = out47} else {48*s.value = append(*s.value, out...)49}50
51s.changed = true52
53return nil54}
55
56// Type returns a string that uniquely represents this flag's type.
57func (s *boolSliceValue) Type() string {58return "boolSlice"59}
60
61// String defines a "native" format for this boolean slice flag value.
62func (s *boolSliceValue) String() string {63
64boolStrSlice := make([]string, len(*s.value))65for i, b := range *s.value {66boolStrSlice[i] = strconv.FormatBool(b)67}68
69out, _ := writeAsCSV(boolStrSlice)70
71return "[" + out + "]"72}
73
74func (s *boolSliceValue) fromString(val string) (bool, error) {75return strconv.ParseBool(val)76}
77
78func (s *boolSliceValue) toString(val bool) string {79return strconv.FormatBool(val)80}
81
82func (s *boolSliceValue) Append(val string) error {83i, err := s.fromString(val)84if err != nil {85return err86}87*s.value = append(*s.value, i)88return nil89}
90
91func (s *boolSliceValue) Replace(val []string) error {92out := make([]bool, len(val))93for i, d := range val {94var err error95out[i], err = s.fromString(d)96if err != nil {97return err98}99}100*s.value = out101return nil102}
103
104func (s *boolSliceValue) GetSlice() []string {105out := make([]string, len(*s.value))106for i, d := range *s.value {107out[i] = s.toString(d)108}109return out110}
111
112func boolSliceConv(val string) (interface{}, error) {113val = strings.Trim(val, "[]")114// Empty string would cause a slice with one (empty) entry115if len(val) == 0 {116return []bool{}, nil117}118ss := strings.Split(val, ",")119out := make([]bool, len(ss))120for i, t := range ss {121var err error122out[i], err = strconv.ParseBool(t)123if err != nil {124return nil, err125}126}127return out, nil128}
129
130// GetBoolSlice returns the []bool value of a flag with the given name.
131func (f *FlagSet) GetBoolSlice(name string) ([]bool, error) {132val, err := f.getFlagType(name, "boolSlice", boolSliceConv)133if err != nil {134return []bool{}, err135}136return val.([]bool), nil137}
138
139// BoolSliceVar defines a boolSlice flag with specified name, default value, and usage string.
140// The argument p points to a []bool variable in which to store the value of the flag.
141func (f *FlagSet) BoolSliceVar(p *[]bool, name string, value []bool, usage string) {142f.VarP(newBoolSliceValue(value, p), name, "", usage)143}
144
145// BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash.
146func (f *FlagSet) BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) {147f.VarP(newBoolSliceValue(value, p), name, shorthand, usage)148}
149
150// BoolSliceVar defines a []bool flag with specified name, default value, and usage string.
151// The argument p points to a []bool variable in which to store the value of the flag.
152func BoolSliceVar(p *[]bool, name string, value []bool, usage string) {153CommandLine.VarP(newBoolSliceValue(value, p), name, "", usage)154}
155
156// BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash.
157func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) {158CommandLine.VarP(newBoolSliceValue(value, p), name, shorthand, usage)159}
160
161// BoolSlice defines a []bool flag with specified name, default value, and usage string.
162// The return value is the address of a []bool variable that stores the value of the flag.
163func (f *FlagSet) BoolSlice(name string, value []bool, usage string) *[]bool {164p := []bool{}165f.BoolSliceVarP(&p, name, "", value, usage)166return &p167}
168
169// BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash.
170func (f *FlagSet) BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool {171p := []bool{}172f.BoolSliceVarP(&p, name, shorthand, value, usage)173return &p174}
175
176// BoolSlice defines a []bool flag with specified name, default value, and usage string.
177// The return value is the address of a []bool variable that stores the value of the flag.
178func BoolSlice(name string, value []bool, usage string) *[]bool {179return CommandLine.BoolSliceP(name, "", value, usage)180}
181
182// BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash.
183func BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool {184return CommandLine.BoolSliceP(name, shorthand, value, usage)185}
186