Do not return error when flag parsing should be skipped

Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
main
Nathan LeClaire 9 years ago committed by Jesse Szwedko
parent 70e3fa51eb
commit c538c376c9

@ -102,6 +102,12 @@ func (c Command) Run(ctx *Context) error {
err = set.Parse(append(flagArgs, regularArgs...)) err = set.Parse(append(flagArgs, regularArgs...))
} else { } else {
err = set.Parse(ctx.Args().Tail()) err = set.Parse(ctx.Args().Tail())
// Work around issue where if the first arg in ctx.Args.Tail()
// is a flag, set.Parse returns an error
if c.SkipFlagParsing {
err = nil
}
} }
if err != nil { if err != nil {

@ -45,3 +45,25 @@ func TestCommandIgnoreFlags(t *testing.T) {
expect(t, err, nil) expect(t, err, nil)
} }
// Fix bug with ignoring flag parsing that would still parse the first flag
func TestCommandIgnoreFlagsIncludingFirstArgument(t *testing.T) {
app := NewApp()
set := flag.NewFlagSet("test", 0)
test := []string{"blah", "-break"}
set.Parse(test)
c := NewContext(app, set, nil)
command := Command{
Name: "test-cmd",
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(_ *Context) {},
SkipFlagParsing: true,
}
err := command.Run(c)
expect(t, err, nil)
}

Loading…
Cancel
Save