diff --git a/command.go b/command.go index fac754d..7a1dcab 100644 --- a/command.go +++ b/command.go @@ -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 { diff --git a/command_test.go b/command_test.go index fd39548..bf7cc99 100644 --- a/command_test.go +++ b/command_test.go @@ -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) +}