Fix tests so they give correct args (#445)

* Fix tests so they give correct args

Shell already deletes all quotes and unite args under quotes, so we
don't need to test it.

* Simplify stripFlags

* Fix 'unused' and 'gosimple' complaints
This commit is contained in:
Albert Nigmatzianov
2017-05-14 14:51:04 +02:00
committed by Eric Paris
parent b5366273a6
commit 51b7cf57e1
2 changed files with 87 additions and 131 deletions

View File

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