Path: blob/main/vendor/github.com/spf13/pflag/bool.go
2875 views
package pflag12import "strconv"34// optional interface to indicate boolean flags that can be5// supplied without "=value" text6type boolFlag interface {7Value8IsBoolFlag() bool9}1011// -- bool Value12type boolValue bool1314func newBoolValue(val bool, p *bool) *boolValue {15*p = val16return (*boolValue)(p)17}1819func (b *boolValue) Set(s string) error {20v, err := strconv.ParseBool(s)21*b = boolValue(v)22return err23}2425func (b *boolValue) Type() string {26return "bool"27}2829func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) }3031func (b *boolValue) IsBoolFlag() bool { return true }3233func boolConv(sval string) (interface{}, error) {34return strconv.ParseBool(sval)35}3637// GetBool return the bool value of a flag with the given name38func (f *FlagSet) GetBool(name string) (bool, error) {39val, err := f.getFlagType(name, "bool", boolConv)40if err != nil {41return false, err42}43return val.(bool), nil44}4546// BoolVar defines a bool flag with specified name, default value, and usage string.47// The argument p points to a bool variable in which to store the value of the flag.48func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {49f.BoolVarP(p, name, "", value, usage)50}5152// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.53func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) {54flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage)55flag.NoOptDefVal = "true"56}5758// BoolVar defines a bool flag with specified name, default value, and usage string.59// The argument p points to a bool variable in which to store the value of the flag.60func BoolVar(p *bool, name string, value bool, usage string) {61BoolVarP(p, name, "", value, usage)62}6364// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.65func BoolVarP(p *bool, name, shorthand string, value bool, usage string) {66flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage)67flag.NoOptDefVal = "true"68}6970// Bool defines a bool flag with specified name, default value, and usage string.71// The return value is the address of a bool variable that stores the value of the flag.72func (f *FlagSet) Bool(name string, value bool, usage string) *bool {73return f.BoolP(name, "", value, usage)74}7576// BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.77func (f *FlagSet) BoolP(name, shorthand string, value bool, usage string) *bool {78p := new(bool)79f.BoolVarP(p, name, shorthand, value, usage)80return p81}8283// Bool defines a bool flag with specified name, default value, and usage string.84// The return value is the address of a bool variable that stores the value of the flag.85func Bool(name string, value bool, usage string) *bool {86return BoolP(name, "", value, usage)87}8889// BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.90func BoolP(name, shorthand string, value bool, usage string) *bool {91b := CommandLine.BoolP(name, shorthand, value, usage)92return b93}949596