Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix args reordering when bool flags are present #712

Merged
merged 1 commit into from
Feb 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,39 @@ func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
expect(t, firstArg, "my-arg")
}

func TestApp_CommandWithArgBeforeBoolFlags(t *testing.T) {
var parsedOption, parsedSecondOption, firstArg string
var parsedBool, parsedSecondBool bool

app := NewApp()
command := Command{
Name: "cmd",
Flags: []Flag{
StringFlag{Name: "option", Value: "", Usage: "some option"},
StringFlag{Name: "secondOption", Value: "", Usage: "another option"},
BoolFlag{Name: "boolflag", Usage: "some bool"},
BoolFlag{Name: "b", Usage: "another bool"},
},
Action: func(c *Context) error {
parsedOption = c.String("option")
parsedSecondOption = c.String("secondOption")
parsedBool = c.Bool("boolflag")
parsedSecondBool = c.Bool("b")
firstArg = c.Args().First()
return nil
},
}
app.Commands = []Command{command}

app.Run([]string{"", "cmd", "my-arg", "--boolflag", "--option", "my-option", "-b", "--secondOption", "fancy-option"})

expect(t, parsedOption, "my-option")
expect(t, parsedSecondOption, "fancy-option")
expect(t, parsedBool, true)
expect(t, parsedSecondBool, true)
expect(t, firstArg, "my-arg")
}

func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
var context *Context

Expand Down
3 changes: 2 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,12 @@ func reorderArgs(args []string) []string {
break
}

if readFlagValue {
if readFlagValue && !strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") {
readFlagValue = false
flags = append(flags, arg)
continue
}
readFlagValue = false

if arg != "-" && strings.HasPrefix(arg, "-") {
flags = append(flags, arg)
Expand Down