Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion command_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func buildCompletionCommand(appName string) *Command {
Action: func(ctx context.Context, cmd *Command) error {
return printShellCompletion(ctx, cmd, appName)
},
isCompletionCommand: true,
}
}

Expand Down
6 changes: 6 additions & 0 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down