bool.go (3072B)
1 package pflag 2 3 import "strconv" 4 5 // optional interface to indicate boolean flags that can be 6 // supplied without "=value" text 7 type boolFlag interface { 8 Value 9 IsBoolFlag() bool 10 } 11 12 // -- bool Value 13 type boolValue bool 14 15 func newBoolValue(val bool, p *bool) *boolValue { 16 *p = val 17 return (*boolValue)(p) 18 } 19 20 func (b *boolValue) Set(s string) error { 21 v, err := strconv.ParseBool(s) 22 *b = boolValue(v) 23 return err 24 } 25 26 func (b *boolValue) Type() string { 27 return "bool" 28 } 29 30 func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) } 31 32 func (b *boolValue) IsBoolFlag() bool { return true } 33 34 func boolConv(sval string) (interface{}, error) { 35 return strconv.ParseBool(sval) 36 } 37 38 // GetBool return the bool value of a flag with the given name 39 func (f *FlagSet) GetBool(name string) (bool, error) { 40 val, err := f.getFlagType(name, "bool", boolConv) 41 if err != nil { 42 return false, err 43 } 44 return val.(bool), nil 45 } 46 47 // BoolVar defines a bool flag with specified name, default value, and usage string. 48 // The argument p points to a bool variable in which to store the value of the flag. 49 func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) { 50 f.BoolVarP(p, name, "", value, usage) 51 } 52 53 // BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash. 54 func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) { 55 flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage) 56 flag.NoOptDefVal = "true" 57 } 58 59 // BoolVar defines a bool flag with specified name, default value, and usage string. 60 // The argument p points to a bool variable in which to store the value of the flag. 61 func BoolVar(p *bool, name string, value bool, usage string) { 62 BoolVarP(p, name, "", value, usage) 63 } 64 65 // BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash. 66 func BoolVarP(p *bool, name, shorthand string, value bool, usage string) { 67 flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage) 68 flag.NoOptDefVal = "true" 69 } 70 71 // Bool defines a bool flag with specified name, default value, and usage string. 72 // The return value is the address of a bool variable that stores the value of the flag. 73 func (f *FlagSet) Bool(name string, value bool, usage string) *bool { 74 return f.BoolP(name, "", value, usage) 75 } 76 77 // BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash. 78 func (f *FlagSet) BoolP(name, shorthand string, value bool, usage string) *bool { 79 p := new(bool) 80 f.BoolVarP(p, name, shorthand, value, usage) 81 return p 82 } 83 84 // Bool defines a bool flag with specified name, default value, and usage string. 85 // The return value is the address of a bool variable that stores the value of the flag. 86 func Bool(name string, value bool, usage string) *bool { 87 return BoolP(name, "", value, usage) 88 } 89 90 // BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash. 91 func BoolP(name, shorthand string, value bool, usage string) *bool { 92 b := CommandLine.BoolP(name, shorthand, value, usage) 93 return b 94 }