5 // optional interface to indicate boolean flags that can be
6 // supplied without "=value" text
7 type boolFlag interface {
15 func newBoolValue(val bool, p *bool) *boolValue {
17 return (*boolValue)(p)
20 func (b *boolValue) Set(s string) error {
21 v, err := strconv.ParseBool(s)
26 func (b *boolValue) Type() string {
30 func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) }
32 func (b *boolValue) IsBoolFlag() bool { return true }
34 func boolConv(sval string) (interface{}, error) {
35 return strconv.ParseBool(sval)
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)
44 return val.(bool), nil
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)
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"
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)
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"
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)
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 {
80 f.BoolVarP(p, name, shorthand, value, usage)
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)
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)