traefik/vendor/github.com/spf13/pflag/bool.go

95 lines
3 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
package pflag
2017-04-07 10:49:53 +00:00
import "strconv"
2017-02-07 21:33:23 +00:00
// optional interface to indicate boolean flags that can be
// supplied without "=value" text
type boolFlag interface {
Value
IsBoolFlag() bool
}
// -- bool Value
type boolValue bool
func newBoolValue(val bool, p *bool) *boolValue {
*p = val
return (*boolValue)(p)
}
func (b *boolValue) Set(s string) error {
v, err := strconv.ParseBool(s)
*b = boolValue(v)
return err
}
func (b *boolValue) Type() string {
return "bool"
}
2017-04-07 10:49:53 +00:00
func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) }
2017-02-07 21:33:23 +00:00
func (b *boolValue) IsBoolFlag() bool { return true }
2017-04-07 10:49:53 +00:00
func boolConv(sval string) (interface{}, error) {
return strconv.ParseBool(sval)
}
// GetBool return the bool value of a flag with the given name
func (f *FlagSet) GetBool(name string) (bool, error) {
val, err := f.getFlagType(name, "bool", boolConv)
if err != nil {
return false, err
}
return val.(bool), nil
}
2017-02-07 21:33:23 +00:00
// BoolVar defines a bool flag with specified name, default value, and usage string.
// The argument p points to a bool variable in which to store the value of the flag.
func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
2017-04-07 10:49:53 +00:00
f.BoolVarP(p, name, "", value, usage)
2017-02-07 21:33:23 +00:00
}
2017-04-07 10:49:53 +00:00
// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
2017-02-07 21:33:23 +00:00
func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
2017-04-07 10:49:53 +00:00
flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage)
flag.NoOptDefVal = "true"
2017-02-07 21:33:23 +00:00
}
// BoolVar defines a bool flag with specified name, default value, and usage string.
// The argument p points to a bool variable in which to store the value of the flag.
func BoolVar(p *bool, name string, value bool, usage string) {
2017-04-07 10:49:53 +00:00
BoolVarP(p, name, "", value, usage)
2017-02-07 21:33:23 +00:00
}
2017-04-07 10:49:53 +00:00
// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
2017-02-07 21:33:23 +00:00
func BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
2017-04-07 10:49:53 +00:00
flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage)
flag.NoOptDefVal = "true"
2017-02-07 21:33:23 +00:00
}
// Bool defines a bool flag with specified name, default value, and usage string.
// The return value is the address of a bool variable that stores the value of the flag.
func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
2017-04-07 10:49:53 +00:00
return f.BoolP(name, "", value, usage)
2017-02-07 21:33:23 +00:00
}
2017-04-07 10:49:53 +00:00
// BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.
2017-02-07 21:33:23 +00:00
func (f *FlagSet) BoolP(name, shorthand string, value bool, usage string) *bool {
p := new(bool)
f.BoolVarP(p, name, shorthand, value, usage)
return p
}
// Bool defines a bool flag with specified name, default value, and usage string.
// The return value is the address of a bool variable that stores the value of the flag.
func Bool(name string, value bool, usage string) *bool {
2017-04-07 10:49:53 +00:00
return BoolP(name, "", value, usage)
2017-02-07 21:33:23 +00:00
}
2017-04-07 10:49:53 +00:00
// BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.
2017-02-07 21:33:23 +00:00
func BoolP(name, shorthand string, value bool, usage string) *bool {
2017-04-07 10:49:53 +00:00
b := CommandLine.BoolP(name, shorthand, value, usage)
return b
2017-02-07 21:33:23 +00:00
}