Skip to content

Commit

Permalink
feat(completion): add option to create default command alone
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Oct 23, 2021
1 parent c1973d3 commit be2d93c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
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 @@ -93,6 +93,9 @@ type CompletionOptions struct {
// DisableDescriptions turns off all completion descriptions for shells
// that support them
DisableDescriptions 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 @@ -571,10 +574,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

0 comments on commit be2d93c

Please sign in to comment.