mirror of https://github.com/spf13/cobra.git
Simplify stripFlags function
This commit is contained in:
parent
aea94819d2
commit
9890b7b2e9
48
command.go
48
command.go
|
@ -392,40 +392,48 @@ func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func stripFlags(args []string, c *Command) []string {
|
func stripFlags(args []string, c *Command) []string {
|
||||||
if len(args) < 1 {
|
if len(args) == 0 {
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
c.mergePersistentFlags()
|
c.mergePersistentFlags()
|
||||||
|
|
||||||
commands := []string{}
|
commands := []string{}
|
||||||
|
|
||||||
inQuote := false
|
inQuote := false
|
||||||
inFlag := false
|
|
||||||
for _, y := range args {
|
Loop:
|
||||||
|
for len(args) > 0 {
|
||||||
|
s := args[0]
|
||||||
if !inQuote {
|
if !inQuote {
|
||||||
switch {
|
switch {
|
||||||
case strings.HasPrefix(y, "\""):
|
case strings.HasPrefix(s, "\"") || strings.Contains(s, "=\""):
|
||||||
inQuote = true
|
inQuote = true
|
||||||
case strings.Contains(y, "=\""):
|
case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], c.Flags()):
|
||||||
inQuote = true
|
// If '--flag arg' then
|
||||||
case strings.HasPrefix(y, "--") && !strings.Contains(y, "="):
|
// delete two items from args.
|
||||||
// TODO: this isn't quite right, we should really check ahead for 'true' or 'false'
|
fallthrough // Do the same as below.
|
||||||
inFlag = !hasNoOptDefVal(y[2:], c.Flags())
|
case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], c.Flags()):
|
||||||
case strings.HasPrefix(y, "-") && !strings.Contains(y, "=") && len(y) == 2 && !shortHasNoOptDefVal(y[1:], c.Flags()):
|
// If '-f arg' then
|
||||||
inFlag = true
|
// delete two items from args.
|
||||||
case inFlag:
|
|
||||||
inFlag = false
|
// If there are only two elements in args or less,
|
||||||
case y == "":
|
// break loop, ...
|
||||||
// strip empty commands, as the go tests expect this to be ok....
|
if len(args) <= 2 {
|
||||||
case !strings.HasPrefix(y, "-"):
|
break Loop
|
||||||
commands = append(commands, y)
|
} else {
|
||||||
inFlag = false
|
// ... else delete first two items.
|
||||||
|
args = args[2:]
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
case s != "" && !strings.HasPrefix(s, "-"):
|
||||||
|
commands = append(commands, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasSuffix(y, "\"") && !strings.HasSuffix(y, "\\\"") {
|
if strings.HasSuffix(s, "\"") && !strings.HasSuffix(s, "\\\"") {
|
||||||
inQuote = false
|
inQuote = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
args = args[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
return commands
|
return commands
|
||||||
|
|
Loading…
Reference in New Issue