Use bash compV2 for the default completion command

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
This commit is contained in:
Marc Khouzam 2021-03-06 16:19:54 -05:00
parent 6d86d9185a
commit 5359c17d36
4 changed files with 9 additions and 12 deletions

View file

@ -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:

View file

@ -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",

View file

@ -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)

View file

@ -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`.