Merge pull request from hpcloud/master

Fixed mishandling of a "-"(dash)-argument causing reordering of cli n…
This commit is contained in:
Jesse Szwedko 2016-01-29 19:19:57 -08:00
commit cf1f63a727
2 changed files with 21 additions and 0 deletions

View File

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

View File

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