diff --git a/command.go b/command.go index c369c0191c..8f73fd28d7 100644 --- a/command.go +++ b/command.go @@ -161,6 +161,8 @@ type Command struct { globaHelpFlagAdded bool // whether global version flag was added globaVersionFlagAdded bool + // whether this is a completion command + isCompletionCommand bool } // FullName returns the full name of the command. diff --git a/command_run.go b/command_run.go index 578683da4a..6195b4e5f6 100644 --- a/command_run.go +++ b/command_run.go @@ -140,13 +140,20 @@ func (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context } var rargs Args = &stringSliceArgs{v: osArgs} + var args Args = &stringSliceArgs{rargs.Tail()} + + if cmd.isCompletionCommand { + tracef("completion command detected, skipping pre-parse (cmd=%[1]q)", cmd.Name) + cmd.parsedArgs = args + return ctx, cmd.Action(ctx, cmd) + } + for _, f := range cmd.allFlags() { if err := f.PreParse(); err != nil { return ctx, err } } - var args Args = &stringSliceArgs{rargs.Tail()} var err error if cmd.SkipFlagParsing { diff --git a/completion.go b/completion.go index d97ade6e49..609b204375 100644 --- a/completion.go +++ b/completion.go @@ -65,6 +65,7 @@ func buildCompletionCommand(appName string) *Command { Action: func(ctx context.Context, cmd *Command) error { return printShellCompletion(ctx, cmd, appName) }, + isCompletionCommand: true, } } diff --git a/completion_test.go b/completion_test.go index 0323764e59..cf47381a87 100644 --- a/completion_test.go +++ b/completion_test.go @@ -20,6 +20,12 @@ func TestCompletionDisable(t *testing.T) { func TestCompletionEnable(t *testing.T) { cmd := &Command{ EnableShellCompletion: true, + Flags: []Flag{ + &BoolFlag{ + Name: "goo", + Required: true, + }, + }, } err := cmd.Run(buildTestContext(t), []string{"foo", completionCommandName})