Do not return error when flag parsing should be skipped

Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
This commit is contained in:
Nathan LeClaire 2015-09-25 14:13:36 -07:00 committed by Jesse Szwedko
parent 70e3fa51eb
commit c538c376c9
2 changed files with 28 additions and 0 deletions

View File

@ -102,6 +102,12 @@ func (c Command) Run(ctx *Context) error {
err = set.Parse(append(flagArgs, regularArgs...))
} else {
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 {

View File

@ -45,3 +45,25 @@ func TestCommandIgnoreFlags(t *testing.T) {
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)
}