mirror of
https://github.com/spf13/cobra
synced 2025-05-05 12:57:22 +00:00
Use bash compV2 for the default completion command
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
This commit is contained in:
parent
6d86d9185a
commit
5359c17d36
4 changed files with 9 additions and 12 deletions
|
@ -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.
|
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.
|
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:
|
Some code that works in kubernetes:
|
||||||
|
|
|
@ -592,9 +592,12 @@ You will need to start a new shell for this setup to take effect.
|
||||||
DisableFlagsInUseLine: true,
|
DisableFlagsInUseLine: true,
|
||||||
ValidArgsFunction: NoFileCompletions,
|
ValidArgsFunction: NoFileCompletions,
|
||||||
RunE: func(cmd *Command, args []string) error {
|
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{
|
zsh := &Command{
|
||||||
Use: "zsh",
|
Use: "zsh",
|
||||||
|
|
|
@ -2097,9 +2097,9 @@ func TestDefaultCompletionCmd(t *testing.T) {
|
||||||
removeCompCmd(rootCmd)
|
removeCompCmd(rootCmd)
|
||||||
|
|
||||||
var compCmd *Command
|
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())
|
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 {
|
if compCmd, _, err = rootCmd.Find([]string{compCmdName, shell}); err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
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)
|
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
|
// Remove completion command for the next test
|
||||||
removeCompCmd(rootCmd)
|
removeCompCmd(rootCmd)
|
||||||
|
|
||||||
|
|
|
@ -429,7 +429,7 @@ show (show information of a chart)
|
||||||
$ helm s[tab][tab]
|
$ helm s[tab][tab]
|
||||||
search show status
|
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
|
## Zsh completions
|
||||||
|
|
||||||
Cobra supports native zsh completion generated from the root `cobra.Command`.
|
Cobra supports native zsh completion generated from the root `cobra.Command`.
|
||||||
|
|
Loading…
Add table
Reference in a new issue