forked from symfony-cli/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags_enhancement.go
417 lines (356 loc) · 11.1 KB
/
flags_enhancement.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Copyright (c) 2021-present Fabien Potencier <[email protected]>
*
* This file is part of Symfony CLI project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package console
import (
"flag"
"fmt"
"io"
"os"
"reflect"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/symfony-cli/terminal"
)
// FlagParsingMode defines how arguments and flags parsing is done.
// Three different values: FlagParsingNormal, FlagParsingSkipped and
// FlagParsingSkippedAfterFirstArg.
type FlagParsingMode int
const (
// FlagParsingNormal sets parsing to a normal mode, complete parsing of all
// flags found after command name
FlagParsingNormal FlagParsingMode = iota
// FlagParsingSkipped sets parsing to a mode where parsing stops just after
// the command name.
FlagParsingSkipped
// FlagParsingSkippedAfterFirstArg sets parsing to a hybrid mode where parsing continues
// after the command name until an argument is found.
// This for example allows usage like `blackfire -v=4 run --samples=2 php foo.php`
FlagParsingSkippedAfterFirstArg
)
func (mode FlagParsingMode) IsPostfix() bool {
return mode == FlagParsingNormal
}
func (mode FlagParsingMode) IsPrefix() bool {
return mode != FlagParsingNormal
}
func (app *Application) parseArgs(arguments []string) (*flag.FlagSet, error) {
fs, err := parseArgs(app.fixArgs(arguments), flagSet(app.Name, app.Flags))
if err != nil {
return fs, errors.WithStack(err)
}
parseFlagsFromEnv(app.FlagEnvPrefix, app.Flags, fs)
// We expand "~" for each provided string flag
fs.Visit(expandHomeInFlagsValues)
err = errors.WithStack(checkRequiredFlags(app.Flags, fs))
return fs, err
}
func (app *Application) fixArgs(args []string) []string {
return fixArgs(args, app.Flags, app.Commands, FlagParsingNormal, "")
}
func (c *Command) parseArgs(arguments []string, prefixes []string) (*flag.FlagSet, error) {
fs, err := parseArgs(c.fixArgs(arguments), flagSet(c.Name, c.Flags))
if err != nil {
return fs, errors.WithStack(err)
}
parseFlagsFromEnv(prefixes, c.Flags, fs)
// We expand "~" for each provided string flag
fs.Visit(expandHomeInFlagsValues)
err = errors.WithStack(checkRequiredFlags(c.Flags, fs))
return fs, err
}
func (c *Command) fixArgs(args []string) []string {
return fixArgs(args, c.Flags, nil, c.FlagParsing, "--")
}
func parseArgs(arguments []string, fs *flag.FlagSet) (*flag.FlagSet, error) {
fs.SetOutput(io.Discard)
err := errors.WithStack(fs.Parse(arguments))
if err != nil {
return fs, err
}
defer func() {
if e := recover(); e != nil {
err = errors.WithStack(e.(error))
}
}()
fs.Visit(func(f *flag.Flag) {
terminal.Logger.Trace().Msgf("Using CLI flags for '%s' configuration entry.\n", f.Name)
})
return fs, err
}
func parseFlagsFromEnv(prefixes []string, flags []Flag, fs *flag.FlagSet) {
definedFlags := make(map[string]bool)
fs.Visit(func(f *flag.Flag) {
definedFlags[f.Name] = true
})
for _, f := range flags {
fName := flagName(f)
// flags given on the CLI overrides environment values
if _, alreadyThere := definedFlags[fName]; alreadyThere {
continue
}
envVariableNames := flagStringSliceField(f, "EnvVars")
for _, prefix := range prefixes {
envVariableNames = append(envVariableNames, strings.ToUpper(strings.Replace(fmt.Sprintf("%s_%s", prefix, fName), "-", "_", -1)))
}
// reverse slice order
for i := len(envVariableNames)/2 - 1; i >= 0; i-- {
opp := len(envVariableNames) - 1 - i
envVariableNames[i], envVariableNames[opp] = envVariableNames[opp], envVariableNames[i]
}
for _, name := range envVariableNames {
val := os.Getenv(name)
if val == "" {
continue
}
terminal.Logger.Trace().Msgf("Using %s from ENV for '%s' configuration entry.\n", name, fName)
if err := fs.Set(fName, val); err != nil {
panic(errors.Errorf("Failed to set flag %s with value %s", fName, val))
}
}
}
}
// fixArgs fixes command lines arguments for them to be parsed.
// Examples:
// upload -slot=4 --v="4" file1 file2 will return:
// -slot=4 --v="4" upload file1 file2
//
// --config=$HOME/.blackfire.ini run --reference=1 php -n exception.php --config=foo will return:
// --config=$HOME/.blackfire.ini --reference=1 run php -n exception.php --config=foo
//
// Note in the latter example than this function needs to pay attention to be eager
// and not try to "fix" arguments belonging to a possible embedded command as run.
// For this purpose, you have three different FlagParsing modes available.
// See FlagParsingMode for more information.
func fixArgs(args []string, flagDefs []Flag, cmdDefs []*Command, defaultMode FlagParsingMode, defaultCommand string) []string {
var (
flags = make([]string, 0)
nonFlags = make([]string, 0)
command = defaultCommand
parsingMode = defaultMode
previousFlagNeedsValue = false
)
var isFlag = func(name string) bool {
return len(name) > 1 && name[0] == '-'
}
var cleanFlag = func(name string) string {
if index := strings.Index(name, "="); index != -1 {
name = name[:index]
}
name = strings.TrimLeft(name, "-")
return expandShortcut(flagDefs, name)
}
var translateShortcutFlags = func(name string) string {
twoDashes := (name[1] == '-')
name = strings.Trim(name, "-")
if index := strings.Index(name, "="); index != -1 {
name = expandShortcut(flagDefs, name[:index]) + name[index:]
} else {
name = expandShortcut(flagDefs, name)
}
if twoDashes {
name = "--" + name
} else {
name = "-" + name
}
return name
}
var findCommand = func(name string) *Command {
var matches []*Command
for _, c := range cmdDefs {
if c.HasName(name, true) {
c.UserName = name
return c
}
if c.HasName(name, false) {
matches = append(matches, c)
}
}
if len(matches) == 1 {
matches[0].UserName = name
return matches[0]
}
return nil
}
for _, arg := range args {
if parsingMode == FlagParsingSkipped {
nonFlags = append(nonFlags, arg)
continue
}
if arg == "--" {
parsingMode = FlagParsingSkipped
if arg != command {
nonFlags = append(nonFlags, arg)
}
continue
}
// argument is a flag
if isFlag(arg) {
cleanedFlag := cleanFlag(arg)
previousFlagNeedsValue = false
// and is present in our flags/shortcuts
// or special case when we are in command mode because we need to
// parse them to get validation errors
if flag := findFlag(flagDefs, cleanedFlag); flag != nil {
arg = translateShortcutFlags(arg)
equalPos := strings.Index(arg, "=")
// an equal sign at the end means we want to use the next arg
// as the value, but is not supported by flag parsing
if equalPos == len(arg)-1 {
// so we strip the sign and reset the position as equals is
// not here anymore
arg, equalPos = arg[:equalPos], -1
}
// no equals sign ...
if equalPos == -1 {
// ... and not a boolean flag nor a verbosity one
_, isBoolFlag := flag.(*BoolFlag)
_, isVerbosityFlag := flag.(*verbosityFlag)
if !isBoolFlag && !isVerbosityFlag {
// we keep information about the previousFlag.
previousFlagNeedsValue = true
}
}
// finally, we add to the flags
flags = append(flags, arg)
} else if defaultCommand == "--" && parsingMode == FlagParsingNormal {
trimmedFlag := strings.TrimSpace(arg)
flags = append(flags, arg)
previousFlagNeedsValue = trimmedFlag[len(trimmedFlag)-1] == '='
} else {
// we add to the non-flags and let the command deal with it later
nonFlags = append(nonFlags, arg)
}
continue
}
// If previous flag needs the arg as its value
if previousFlagNeedsValue {
// we just add it to the flags list as all processing as been
// done earlier
flags = append(flags, arg)
previousFlagNeedsValue = false
continue
}
// let's find the command if none is set yet
if command == "" {
if cmd := findCommand(arg); cmd != nil {
command = arg
previousFlagNeedsValue = false
parsingMode = cmd.FlagParsing
continue
}
}
// not a flag and command found or default was this, let's stop
// because the command wants everything else as args
if parsingMode == FlagParsingSkippedAfterFirstArg {
parsingMode = FlagParsingSkipped
}
nonFlags = append(nonFlags, arg)
}
if command != "" {
flags = append(flags, command)
}
return append(flags, nonFlags...)
}
func findFlag(flagDefs []Flag, name string) Flag {
for _, f := range flagDefs {
for _, n := range f.Names() {
if n == name {
return f
}
}
}
return nil
}
func expandShortcut(flagDefs []Flag, name string) string {
if f := findFlag(flagDefs, name); f != nil {
if _, isVerbosity := f.(*verbosityFlag); isVerbosity {
return name
}
return flagName(f)
}
return name
}
func expandHomeInFlagsValues(f *flag.Flag) {
// This is the safest right now
if reflect.ValueOf(f.Value).Elem().Kind() != reflect.String {
return
}
val := ExpandHome(f.Value.String())
if e := f.Value.Set(val); e != nil {
panic(errors.Errorf("Failed to set flag %s with value %s", f.Name, val))
}
}
func ExpandHome(path string) string {
if expandedPath, err := homedir.Expand(path); err == nil {
return expandedPath
}
return path
}
func checkFlagsUnicity(appFlags []Flag, cmdFlags []Flag, commandName string) {
appDefinedFlags := make(map[string]bool)
for _, f := range appFlags {
for _, name := range f.Names() {
appDefinedFlags[name] = true
}
}
for _, f := range cmdFlags {
canonicalName := flagName(f)
for _, name := range f.Names() {
if appDefinedFlags[name] {
msg := ""
if name == canonicalName {
msg = fmt.Sprintf("flag redefined by command %s: %s", commandName, name)
} else {
msg = fmt.Sprintf("flag redefined by command %s: %s (alias for %s)", commandName, name, canonicalName)
}
panic(msg) // Happens only if flags are declared with identical names
}
}
}
}
func checkRequiredFlags(flags []Flag, set *flag.FlagSet) error {
visited := make(map[string]bool)
set.Visit(func(f *flag.Flag) {
visited[f.Name] = true
})
for _, f := range flags {
if flagIsRequired(f) {
if !visited[flagName(f)] {
return errors.Errorf(`Required flag "%s" is not set`, flagName(f))
}
}
}
return nil
}
func checkFlagsValidity(flags []Flag, set *flag.FlagSet, c *Context) error {
visited := make(map[string]bool)
set.Visit(func(f *flag.Flag) {
visited[f.Name] = true
})
for _, f := range flags {
if !visited[flagName(f)] {
continue
}
if err := f.Validate(c); err != nil {
return errors.Wrapf(err, `invalid value for flag "%s"`, flagName(f))
}
}
return nil
}