Fixed mishandling of a "-"(dash)-argument causing reordering of cli non-flag arguments.

Added test demonstrating issue (PASS with fix, FAIL without).
main
Andreas Kupries 8 years ago
parent f9cc3001e0
commit bb4e78eb6a

@ -249,6 +249,24 @@ func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) {
expect(t, args[2], "--notARealFlag")
}
func TestApp_CommandWithDash(t *testing.T) {
var args []string
app := NewApp()
command := Command{
Name: "cmd",
Action: func(c *Context) {
args = c.Args()
},
}
app.Commands = []Command{command}
app.Run([]string{"", "cmd", "my-arg", "-"})
expect(t, args[0], "my-arg")
expect(t, args[1], "-")
}
func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
var args []string

@ -81,6 +81,9 @@ func (c Command) Run(ctx *Context) (err error) {
if arg == "--" {
terminatorIndex = index
break
} else if arg == "-" {
// Do nothing. A dash alone is not really a flag.
continue
} else if strings.HasPrefix(arg, "-") && firstFlagIndex == -1 {
firstFlagIndex = index
}

Loading…
Cancel
Save