From 04373829fc4c28fe535754898d55f18ed110c397 Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Sat, 13 May 2017 20:30:58 +0200 Subject: [PATCH] Simplify stripFlags --- command.go | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/command.go b/command.go index 3be55544..01d9683e 100644 --- a/command.go +++ b/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) } }