mirror of
https://github.com/spf13/cobra
synced 2025-05-05 21:07:24 +00:00
Simplify stripFlags
This commit is contained in:
parent
aac12021de
commit
04373829fc
1 changed files with 15 additions and 24 deletions
39
command.go
39
command.go
|
@ -430,37 +430,28 @@ func stripFlags(args []string, c *Command) []string {
|
|||
c.mergePersistentFlags()
|
||||
|
||||
commands := []string{}
|
||||
inQuote := false
|
||||
flags := c.Flags()
|
||||
|
||||
Loop:
|
||||
for len(args) > 0 {
|
||||
s := args[0]
|
||||
args = args[1:]
|
||||
if !inQuote {
|
||||
switch {
|
||||
case strings.HasPrefix(s, "\"") || strings.Contains(s, "=\""):
|
||||
inQuote = true
|
||||
case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
|
||||
// If '--flag arg' then
|
||||
// delete arg from args.
|
||||
fallthrough // (do the same as below)
|
||||
case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
|
||||
// If '-f arg' then
|
||||
// delete 'arg' from args or break the loop if len(args) <= 1.
|
||||
if len(args) <= 1 {
|
||||
break Loop
|
||||
} else {
|
||||
args = args[1:]
|
||||
continue
|
||||
}
|
||||
case s != "" && !strings.HasPrefix(s, "-"):
|
||||
commands = append(commands, s)
|
||||
switch {
|
||||
case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
|
||||
// If '--flag arg' then
|
||||
// delete arg from args.
|
||||
fallthrough // (do the same as below)
|
||||
case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
|
||||
// If '-f arg' then
|
||||
// delete 'arg' from args or break the loop if len(args) <= 1.
|
||||
if len(args) <= 1 {
|
||||
break Loop
|
||||
} else {
|
||||
args = args[1:]
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasSuffix(s, "\"") && !strings.HasSuffix(s, "\\\"") {
|
||||
inQuote = false
|
||||
case s != "" && !strings.HasPrefix(s, "-"):
|
||||
commands = append(commands, s)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue