Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(completion): add option to create default command alone #1392

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions command.go
Expand Up @@ -927,10 +927,10 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
preExecHookFn(c)
}

// initialize help at the last point to allow for user overriding
c.InitDefaultHelpCmd()
// initialize completion at the last point to allow for user overriding
c.initDefaultCompletionCmd()
// initialize help at the last point to allow for user overriding
c.InitDefaultHelpCmd()

args := c.args

Expand Down
7 changes: 5 additions & 2 deletions completions.go
Expand Up @@ -95,6 +95,9 @@ type CompletionOptions struct {
DisableDescriptions bool
// HiddenDefaultCmd makes the default 'completion' command hidden
HiddenDefaultCmd bool
// InitDefaultEvenIfHasNoSubCommands causes the default 'completion' command to be created
// even if there are no other commands defined
InitDefaultEvenIfHasNoSubCommands bool
}

// NoFileCompletions can be used to disable file completion for commands that should
Expand Down Expand Up @@ -583,10 +586,10 @@ func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*p
// initDefaultCompletionCmd adds a default 'completion' command to c.
// This function will do nothing if any of the following is true:
// 1- the feature has been explicitly disabled by the program,
// 2- c has no subcommands (to avoid creating one),
// 2- c has no subcommands (to avoid creating one) and not overridden with the InitDefaultEvenIfHasNoSubCommands completion option,
// 3- c already has a 'completion' command provided by the program.
func (c *Command) initDefaultCompletionCmd() {
if c.CompletionOptions.DisableDefaultCmd || !c.HasSubCommands() {
if c.CompletionOptions.DisableDefaultCmd || (!c.CompletionOptions.InitDefaultEvenIfHasNoSubCommands && !c.HasSubCommands()) {
return
}

Expand Down