Simplify stripFlags

This commit is contained in:
Albert Nigmatzianov 2017-05-13 20:30:58 +02:00
parent aac12021de
commit 04373829fc

View file

@ -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
} }
} }