Skip to content

Commit

Permalink
Merge pull request #697 from urfave/fix-skip-flag-parsing
Browse files Browse the repository at this point in the history
Fix regression of SkipFlagParsing behavior
  • Loading branch information
jszwedko committed Jan 6, 2018
2 parents 39908eb + e38e4ae commit 75104e9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c *Command) parseFlags(args Args) (*flag.FlagSet, error) {
set.SetOutput(ioutil.Discard)

if c.SkipFlagParsing {
return set, set.Parse(append([]string{c.Name, "--"}, args...))
return set, set.Parse(append([]string{"--"}, args...))
}

if c.UseShortOptionHandling {
Expand Down
36 changes: 36 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,39 @@ func TestCommandFlagReordering(t *testing.T) {
expect(t, args, c.expectedArgs)
}
}

func TestCommandSkipFlagParsing(t *testing.T) {
cases := []struct {
testArgs []string
expectedArgs []string
expectedErr error
}{
{[]string{"some-exec", "some-command", "some-arg", "--flag", "foo"}, []string{"some-arg", "--flag", "foo"}, nil},
{[]string{"some-exec", "some-command", "some-arg", "--flag=foo"}, []string{"some-arg", "--flag=foo"}, nil},
}

for _, c := range cases {
value := ""
args := []string{}
app := &App{
Commands: []Command{
{
SkipFlagParsing: true,
Name: "some-command",
Flags: []Flag{
StringFlag{Name: "flag"},
},
Action: func(c *Context) {
fmt.Printf("%+v\n", c.String("flag"))
value = c.String("flag")
args = c.Args()
},
},
},
}

err := app.Run(c.testArgs)
expect(t, err, c.expectedErr)
expect(t, args, c.expectedArgs)
}
}

0 comments on commit 75104e9

Please sign in to comment.