diff --git a/bash_completions.md b/bash_completions.md index 130f99b9..52919b2f 100644 --- a/bash_completions.md +++ b/bash_completions.md @@ -6,6 +6,8 @@ Please refer to [Shell Completions](shell_completions.md) for details. For backward compatibility, Cobra still supports its legacy dynamic completion solution (described below). Unlike the `ValidArgsFunction` solution, the legacy solution will only work for Bash shell-completion and not for other shells. This legacy solution can be used along-side `ValidArgsFunction` and `RegisterFlagCompletionFunc()`, as long as both solutions are not used for the same command. This provides a path to gradually migrate from the legacy solution to the new solution. +**Note**: Cobra's default `completion` command uses bash completion V2. If you are currently using Cobra's legacy dynamic completion solution, you should not use the default `completion` command but continue using your own. + The legacy solution allows you to inject bash functions into the bash completion script. Those bash functions are responsible for providing the completion choices for your own completions. Some code that works in kubernetes: diff --git a/completions.go b/completions.go index 04dc71e4..6fb2810e 100644 --- a/completions.go +++ b/completions.go @@ -592,9 +592,12 @@ You will need to start a new shell for this setup to take effect. DisableFlagsInUseLine: true, ValidArgsFunction: NoFileCompletions, RunE: func(cmd *Command, args []string) error { - return cmd.Root().GenBashCompletion(out) + return cmd.Root().GenBashCompletionV2(out, !noDesc) }, } + if haveNoDescFlag { + bash.Flags().BoolVar(&noDesc, compCmdNoDescFlagName, compCmdNoDescFlagDefault, compCmdNoDescFlagDesc) + } zsh := &Command{ Use: "zsh", diff --git a/completions_test.go b/completions_test.go index 9bd6a410..db363ae9 100644 --- a/completions_test.go +++ b/completions_test.go @@ -2097,9 +2097,9 @@ func TestDefaultCompletionCmd(t *testing.T) { removeCompCmd(rootCmd) var compCmd *Command - // Test that the --no-descriptions flag is present for the relevant shells only + // Test that the --no-descriptions flag is present on all shells assertNoErr(t, rootCmd.Execute()) - for _, shell := range []string{"fish", "powershell", "zsh"} { + for _, shell := range []string{"bash", "fish", "powershell", "zsh"} { if compCmd, _, err = rootCmd.Find([]string{compCmdName, shell}); err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2107,14 +2107,6 @@ func TestDefaultCompletionCmd(t *testing.T) { t.Errorf("Missing --%s flag for %s shell", compCmdNoDescFlagName, shell) } } - for _, shell := range []string{"bash"} { - if compCmd, _, err = rootCmd.Find([]string{compCmdName, shell}); err != nil { - t.Errorf("Unexpected error: %v", err) - } - if flag := compCmd.Flags().Lookup(compCmdNoDescFlagName); flag != nil { - t.Errorf("Unexpected --%s flag for %s shell", compCmdNoDescFlagName, shell) - } - } // Remove completion command for the next test removeCompCmd(rootCmd) diff --git a/shell_completions.md b/shell_completions.md index d6e34ecc..b2b261d4 100644 --- a/shell_completions.md +++ b/shell_completions.md @@ -429,7 +429,7 @@ show (show information of a chart) $ helm s[tab][tab] search show status ``` - +**Note**: Cobra's default `completion` command uses bash completion V2. If for some reason you need to use bash completion V1, you will need to implement your own `completion` command. ## Zsh completions Cobra supports native zsh completion generated from the root `cobra.Command`.