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 default completion hidden if there are no subcmds #1450

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*p
// 2- c has no subcommands (to avoid creating one),
// 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 {
return
}

Expand All @@ -598,6 +598,7 @@ func (c *Command) initDefaultCompletionCmd() {
}

haveNoDescFlag := !c.CompletionOptions.DisableNoDescFlag && !c.CompletionOptions.DisableDescriptions
hidden := c.CompletionOptions.HiddenDefaultCmd || !c.HasSubCommands()

completionCmd := &Command{
Use: compCmdName,
Expand All @@ -607,7 +608,7 @@ See each sub-command's help for details on how to use the generated script.
`, c.Root().Name()),
Args: NoArgs,
ValidArgsFunction: NoFileCompletions,
Hidden: c.CompletionOptions.HiddenDefaultCmd,
Hidden: hidden,
}
c.AddCommand(completionCmd)

Expand Down
13 changes: 10 additions & 3 deletions completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2287,14 +2287,21 @@ func TestDefaultCompletionCmd(t *testing.T) {
Run: emptyRun,
}

// Test that no completion command is created if there are not other sub-commands
// Test that default completion command is created, hidden if there are no other sub-commands
assertNoErr(t, rootCmd.Execute())
found := false
for _, cmd := range rootCmd.commands {
if cmd.Name() == compCmdName {
t.Errorf("Should not have a 'completion' command when there are no other sub-commands of root")
found = true
if !cmd.Hidden {
t.Errorf("The 'completion' command should be hidden when there are no other sub-commands of root")
}
break
}
}
if !found {
t.Errorf("Should have a 'completion' command when there are no other sub-commands of root")
}

subCmd := &Command{
Use: "sub",
Expand All @@ -2303,7 +2310,7 @@ func TestDefaultCompletionCmd(t *testing.T) {
rootCmd.AddCommand(subCmd)

// Test that a completion command is created if there are other sub-commands
found := false
found = false
assertNoErr(t, rootCmd.Execute())
for _, cmd := range rootCmd.commands {
if cmd.Name() == compCmdName {
Expand Down
3 changes: 2 additions & 1 deletion shell_completions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ The currently supported shells are:
- PowerShell

Cobra will automatically provide your program with a fully functional `completion` command,
similarly to how it provides the `help` command.
similarly to how it provides the `help` command. If there are no other subcommands, the
default `completion` command will be hidden, but still functional.

## Creating your own completion command

Expand Down