mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +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()
|
c.mergePersistentFlags()
|
||||||
|
|
||||||
commands := []string{}
|
commands := []string{}
|
||||||
inQuote := false
|
|
||||||
flags := c.Flags()
|
flags := c.Flags()
|
||||||
|
|
||||||
Loop:
|
Loop:
|
||||||
for len(args) > 0 {
|
for len(args) > 0 {
|
||||||
s := args[0]
|
s := args[0]
|
||||||
args = args[1:]
|
args = args[1:]
|
||||||
if !inQuote {
|
switch {
|
||||||
switch {
|
case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
|
||||||
case strings.HasPrefix(s, "\"") || strings.Contains(s, "=\""):
|
// If '--flag arg' then
|
||||||
inQuote = true
|
// delete arg from args.
|
||||||
case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
|
fallthrough // (do the same as below)
|
||||||
// If '--flag arg' then
|
case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
|
||||||
// delete arg from args.
|
// If '-f arg' then
|
||||||
fallthrough // (do the same as below)
|
// delete 'arg' from args or break the loop if len(args) <= 1.
|
||||||
case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
|
if len(args) <= 1 {
|
||||||
// If '-f arg' then
|
break Loop
|
||||||
// delete 'arg' from args or break the loop if len(args) <= 1.
|
} else {
|
||||||
if len(args) <= 1 {
|
args = args[1:]
|
||||||
break Loop
|
continue
|
||||||
} else {
|
|
||||||
args = args[1:]
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
case s != "" && !strings.HasPrefix(s, "-"):
|
|
||||||
commands = append(commands, s)
|
|
||||||
}
|
}
|
||||||
}
|
case s != "" && !strings.HasPrefix(s, "-"):
|
||||||
|
commands = append(commands, s)
|
||||||
if strings.HasSuffix(s, "\"") && !strings.HasSuffix(s, "\\\"") {
|
|
||||||
inQuote = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue