diff --git a/command.go b/command.go index f02d3589ff..db528ae680 100644 --- a/command.go +++ b/command.go @@ -234,7 +234,7 @@ func reorderArgs(commandFlags []Flag, args []string) []string { break // checks if this arg is a value that should be re-ordered next to its associated flag - } else if nextIndexMayContainValue && !strings.HasPrefix(arg, "-") { + } else if nextIndexMayContainValue && !argIsFlag(commandFlags, arg) { nextIndexMayContainValue = false reorderedArgs = append(reorderedArgs, arg) diff --git a/command_test.go b/command_test.go index a59d368c46..9e53ed3681 100644 --- a/command_test.go +++ b/command_test.go @@ -114,6 +114,78 @@ func TestParseAndRunShortOpts(t *testing.T) { } } +// test-case for https://github.com/urfave/cli/issues/1092 +func TestParseAndRunHyphenValues(t *testing.T) { + cases := []struct { + testArgs []string + expectedArgs []string + expectedOpt string + }{ + {[]string{"foo", "test", "arg1"}, []string{"arg1"}, ""}, + {[]string{"foo", "test", "arg1", "arg2"}, []string{"arg1", "arg2"}, ""}, + {[]string{"foo", "test", "--opt", "opt-value"}, []string{}, "opt-value"}, + {[]string{"foo", "test", "--opt", "opt-value", "arg1"}, []string{"arg1"}, "opt-value"}, + {[]string{"foo", "test", "--opt", "opt-value", "arg1", "arg2"}, []string{"arg1", "arg2"}, "opt-value"}, + {[]string{"foo", "test", "arg1", "--opt", "opt-value"}, []string{"arg1"}, "opt-value"}, + {[]string{"foo", "test", "arg1", "--opt", "opt-value", "arg2"}, []string{"arg1", "arg2"}, "opt-value"}, + + {[]string{"foo", "test", "--opt", "-opt-value"}, []string{}, "-opt-value"}, + {[]string{"foo", "test", "--opt", "-opt-value", "arg1"}, []string{"arg1"}, "-opt-value"}, + {[]string{"foo", "test", "--opt", "-opt-value", "arg1", "arg2"}, []string{"arg1", "arg2"}, "-opt-value"}, + {[]string{"foo", "test", "arg1", "--opt", "-opt-value"}, []string{"arg1"}, "-opt-value"}, + {[]string{"foo", "test", "arg1", "--opt", "-opt-value", "arg2"}, []string{"arg1", "arg2"}, "-opt-value"}, + + {[]string{"foo", "test", "--opt", "--opt-value"}, []string{}, "--opt-value"}, + {[]string{"foo", "test", "--opt", "--opt-value", "arg1"}, []string{"arg1"}, "--opt-value"}, + {[]string{"foo", "test", "--opt", "--opt-value", "arg1", "arg2"}, []string{"arg1", "arg2"}, "--opt-value"}, + {[]string{"foo", "test", "arg1", "--opt", "--opt-value"}, []string{"arg1"}, "--opt-value"}, + {[]string{"foo", "test", "arg1", "--opt", "--opt-value", "arg2"}, []string{"arg1", "arg2"}, "--opt-value"}, + + {[]string{"foo", "test", "--", "arg1", "--opt", "--opt-value", "arg2"}, []string{""}, ""}, + {[]string{"foo", "test", "arg1", "--", "--opt", "--opt-value", "arg2"}, []string{"arg1"}, ""}, + {[]string{"foo", "test", "arg1", "--opt", "--", "--opt-value", "arg2"}, []string{"arg1"}, ""}, + {[]string{"foo", "test", "arg1", "--opt", "--opt-value", "--", "arg2"}, []string{"arg1"}, "--opt-value"}, + {[]string{"foo", "test", "arg1", "--opt", "--opt-value", "arg2", "--"}, []string{"arg1", "arg2"}, "--opt-value"}, + } + + for _, tc := range cases { + tc := tc + t.Run(strings.Join(tc.testArgs, "_"), func(t *testing.T){ + var ( + args []string + opt string + ) + + cmd := Command{ + Name: "test", + Usage: "this is for testing", + Description: "testing", + Action: func(c *Context) error { + args = c.Args() + opt = c.String("opt") + return nil + }, + Flags: []Flag{StringFlag{ + Name: "opt", + Usage: "opt; set '-1' to disable", + }}, + } + + app := NewApp() + app.Writer = ioutil.Discard + app.ErrWriter = ioutil.Discard + app.Commands = []Command{cmd} + + err := app.Run(tc.testArgs) + + expect(t, err, nil) + expect(t, args, tc.expectedArgs) + expect(t, opt, tc.expectedOpt) + }) + + } +} + func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) { app := NewApp() app.Commands = []Command{