forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_bool_with_inverse.go
194 lines (154 loc) · 4.72 KB
/
flag_bool_with_inverse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package cli
import (
"context"
"flag"
"fmt"
"strings"
)
var DefaultInverseBoolPrefix = "no-"
type BoolWithInverseFlag struct {
// The BoolFlag which the positive and negative flags are generated from
*BoolFlag
// The prefix used to indicate a negative value
// Default: `env` becomes `no-env`
InversePrefix string
positiveFlag *BoolFlag
negativeFlag *BoolFlag
// pointers obtained from the embedded bool flag
posDest *bool
posCount *int
negDest *bool
}
func (parent *BoolWithInverseFlag) Flags() []Flag {
return []Flag{parent.positiveFlag, parent.negativeFlag}
}
func (parent *BoolWithInverseFlag) IsSet() bool {
return (*parent.posCount > 0) || (parent.positiveFlag.IsSet() || parent.negativeFlag.IsSet())
}
func (parent *BoolWithInverseFlag) Value() bool {
return *parent.posDest
}
func (parent *BoolWithInverseFlag) RunAction(ctx context.Context, cmd *Command) error {
if *parent.negDest && *parent.posDest {
return fmt.Errorf("cannot set both flags `--%s` and `--%s`", parent.positiveFlag.Name, parent.negativeFlag.Name)
}
if *parent.negDest {
err := cmd.Set(parent.positiveFlag.Name, "false")
if err != nil {
return err
}
}
if parent.BoolFlag.Action != nil {
return parent.BoolFlag.Action(ctx, cmd, parent.Value())
}
return nil
}
// Initialize creates a new BoolFlag that has an inverse flag
//
// consider a bool flag `--env`, there is no way to set it to false
// this function allows you to set `--env` or `--no-env` and in the command action
// it can be determined that BoolWithInverseFlag.IsSet().
func (parent *BoolWithInverseFlag) initialize() {
child := parent.BoolFlag
parent.negDest = new(bool)
if child.Destination != nil {
parent.posDest = child.Destination
} else {
parent.posDest = new(bool)
}
if child.Config.Count != nil {
parent.posCount = child.Config.Count
} else {
parent.posCount = new(int)
}
parent.positiveFlag = child
parent.positiveFlag.Destination = parent.posDest
parent.positiveFlag.Config.Count = parent.posCount
parent.negativeFlag = &BoolFlag{
Category: child.Category,
DefaultText: child.DefaultText,
Sources: NewValueSourceChain(child.Sources.Chain...),
Usage: child.Usage,
Required: child.Required,
Hidden: child.Hidden,
Persistent: child.Persistent,
Value: child.Value,
Destination: parent.negDest,
TakesFile: child.TakesFile,
OnlyOnce: child.OnlyOnce,
hasBeenSet: child.hasBeenSet,
applied: child.applied,
creator: boolValue{},
value: child.value,
}
// Set inverse names ex: --env => --no-env
parent.negativeFlag.Name = parent.inverseName()
parent.negativeFlag.Aliases = parent.inverseAliases()
if len(child.Sources.EnvKeys()) > 0 {
sources := []ValueSource{}
for _, envVar := range child.GetEnvVars() {
sources = append(sources, &envVarValueSource{Key: strings.ToUpper(parent.InversePrefix) + envVar})
}
parent.negativeFlag.Sources = NewValueSourceChain(sources...)
}
}
func (parent *BoolWithInverseFlag) inverseName() string {
if parent.InversePrefix == "" {
parent.InversePrefix = DefaultInverseBoolPrefix
}
return parent.InversePrefix + parent.BoolFlag.Name
}
func (parent *BoolWithInverseFlag) inversePrefix() string {
if parent.InversePrefix == "" {
return DefaultInverseBoolPrefix
}
return parent.InversePrefix
}
func (parent *BoolWithInverseFlag) inverseAliases() (aliases []string) {
if len(parent.BoolFlag.Aliases) > 0 {
aliases = make([]string, len(parent.BoolFlag.Aliases))
for idx, alias := range parent.BoolFlag.Aliases {
aliases[idx] = parent.InversePrefix + alias
}
}
return
}
func (parent *BoolWithInverseFlag) Apply(set *flag.FlagSet) error {
if parent.positiveFlag == nil {
parent.initialize()
}
if err := parent.positiveFlag.Apply(set); err != nil {
return err
}
if err := parent.negativeFlag.Apply(set); err != nil {
return err
}
return nil
}
func (parent *BoolWithInverseFlag) Names() []string {
// Get Names when flag has not been initialized
if parent.positiveFlag == nil {
return append(parent.BoolFlag.Names(), FlagNames(parent.inverseName(), parent.inverseAliases())...)
}
if *parent.negDest {
return parent.negativeFlag.Names()
}
if *parent.posDest {
return parent.positiveFlag.Names()
}
return append(parent.negativeFlag.Names(), parent.positiveFlag.Names()...)
}
// String implements the standard Stringer interface.
//
// Example for BoolFlag{Name: "env"}
// --[no-]env (default: false)
func (parent *BoolWithInverseFlag) String() string {
out := FlagStringer(parent)
i := strings.Index(out, "\t")
prefix := "--"
// single character flags are prefixed with `-` instead of `--`
if len(parent.Name) == 1 {
prefix = "-"
}
return fmt.Sprintf("%s[%s]%s%s", prefix, parent.inversePrefix(), parent.Name, out[i:])
}