...
1 package cli
2
3 import (
4 "errors"
5 "flag"
6 "fmt"
7 "strconv"
8 )
9
10
11
12
13
14
15
16
17 type boolValue struct {
18 destination *bool
19 count *int
20 }
21
22 func newBoolValue(val bool, p *bool, count *int) *boolValue {
23 *p = val
24 return &boolValue{
25 destination: p,
26 count: count,
27 }
28 }
29
30 func (b *boolValue) Set(s string) error {
31 v, err := strconv.ParseBool(s)
32 if err != nil {
33 err = errors.New("parse error")
34 return err
35 }
36 *b.destination = v
37 if b.count != nil {
38 *b.count = *b.count + 1
39 }
40 return err
41 }
42
43 func (b *boolValue) Get() interface{} { return *b.destination }
44
45 func (b *boolValue) String() string {
46 if b.destination != nil {
47 return strconv.FormatBool(*b.destination)
48 }
49 return strconv.FormatBool(false)
50 }
51
52 func (b *boolValue) IsBoolFlag() bool { return true }
53
54 func (b *boolValue) Count() int {
55 if b.count != nil && *b.count > 0 {
56 return *b.count
57 }
58 return 0
59 }
60
61
62 func (f *BoolFlag) TakesValue() bool {
63 return false
64 }
65
66
67 func (f *BoolFlag) GetUsage() string {
68 return f.Usage
69 }
70
71
72 func (f *BoolFlag) GetCategory() string {
73 return f.Category
74 }
75
76
77
78 func (f *BoolFlag) GetValue() string {
79 return ""
80 }
81
82
83 func (f *BoolFlag) GetDefaultText() string {
84 if f.DefaultText != "" {
85 return f.DefaultText
86 }
87 if f.defaultValueSet {
88 return fmt.Sprintf("%v", f.defaultValue)
89 }
90 return fmt.Sprintf("%v", f.Value)
91 }
92
93
94 func (f *BoolFlag) GetEnvVars() []string {
95 return f.EnvVars
96 }
97
98
99 func (f *BoolFlag) RunAction(c *Context) error {
100 if f.Action != nil {
101 return f.Action(c, c.Bool(f.Name))
102 }
103
104 return nil
105 }
106
107
108 func (f *BoolFlag) Apply(set *flag.FlagSet) error {
109
110 f.defaultValue = f.Value
111 f.defaultValueSet = true
112
113 if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
114 if val != "" {
115 valBool, err := strconv.ParseBool(val)
116
117 if err != nil {
118 return fmt.Errorf("could not parse %q as bool value from %s for flag %s: %s", val, source, f.Name, err)
119 }
120
121 f.Value = valBool
122 } else {
123
124
125
126 f.Value = false
127 }
128 f.HasBeenSet = true
129 }
130
131 count := f.Count
132 dest := f.Destination
133
134 if count == nil {
135 count = new(int)
136 }
137
138
139
140 *count -= len(f.Aliases)
141
142 if dest == nil {
143 dest = new(bool)
144 }
145
146 for _, name := range f.Names() {
147 value := newBoolValue(f.Value, dest, count)
148 set.Var(value, name, f.Usage)
149 }
150
151 return nil
152 }
153
154
155 func (f *BoolFlag) Get(ctx *Context) bool {
156 return ctx.Bool(f.Name)
157 }
158
159
160
161 func (cCtx *Context) Bool(name string) bool {
162 if fs := cCtx.lookupFlagSet(name); fs != nil {
163 return lookupBool(name, fs)
164 }
165 return false
166 }
167
168 func lookupBool(name string, set *flag.FlagSet) bool {
169 f := set.Lookup(name)
170 if f != nil {
171 parsed, err := strconv.ParseBool(f.Value.String())
172 if err != nil {
173 return false
174 }
175 return parsed
176 }
177 return false
178 }
179
View as plain text