podman
94 строки · 3.0 Кб
1package pflag
2
3import "strconv"
4
5// optional interface to indicate boolean flags that can be
6// supplied without "=value" text
7type boolFlag interface {
8Value
9IsBoolFlag() bool
10}
11
12// -- bool Value
13type boolValue bool
14
15func newBoolValue(val bool, p *bool) *boolValue {
16*p = val
17return (*boolValue)(p)
18}
19
20func (b *boolValue) Set(s string) error {
21v, err := strconv.ParseBool(s)
22*b = boolValue(v)
23return err
24}
25
26func (b *boolValue) Type() string {
27return "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) {
35return 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) {
40val, err := f.getFlagType(name, "bool", boolConv)
41if err != nil {
42return false, err
43}
44return 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) {
50f.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) {
55flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage)
56flag.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) {
62BoolVarP(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) {
67flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage)
68flag.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 {
74return 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 {
79p := new(bool)
80f.BoolVarP(p, name, shorthand, value, usage)
81return 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 {
87return 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 {
92b := CommandLine.BoolP(name, shorthand, value, usage)
93return b
94}
95