blob: c4c5c0bfda095257e849395a0b79e7e83b2a121a [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001package pflag
2
3import "strconv"
4
5// optional interface to indicate boolean flags that can be
6// supplied without "=value" text
7type boolFlag interface {
8 Value
9 IsBoolFlag() bool
10}
11
12// -- bool Value
13type boolValue bool
14
15func newBoolValue(val bool, p *bool) *boolValue {
16 *p = val
17 return (*boolValue)(p)
18}
19
20func (b *boolValue) Set(s string) error {
21 v, err := strconv.ParseBool(s)
22 *b = boolValue(v)
23 return err
24}
25
26func (b *boolValue) Type() string {
27 return "bool"
28}
29
30func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) }
31
32func (b *boolValue) IsBoolFlag() bool { return true }
33
34func 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
39func (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.
49func (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.
54func (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.
61func 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.
66func 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.
73func (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.
78func (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.
86func 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.
91func BoolP(name, shorthand string, value bool, usage string) *bool {
92 b := CommandLine.BoolP(name, shorthand, value, usage)
93 return b
94}