Skip to content
Myr

shell_completions.md

This file

Path

shell_completions.md

548 lines · 24.2 KiB · Markdown

Last changed by

Fix typo sh completion docs (#1678)Daniel Nguyenbefore adoptiond8184d3

History of this file

Take it

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 1–6why, one step back
1# Generating shell completions
23Cobra can generate shell completions for multiple shells.4The currently supported shells are:5- Bash6- Zsh

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 7why, one step back
7- fish

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 8–9why, one step back
8- PowerShell
9

Create 'completion' command automatically (#1192)Marc Khouzambefore adoptionb312f0a

lines 10–18why, one step back
10Cobra will automatically provide your program with a fully functional `completion` command,
11similarly to how it provides the `help` command.1213## Creating your own completion command1415If you do not wish to use the default `completion` command, you can choose to16provide your own, which will take precedence over the default one. (This also provides17backwards-compatibility with programs that already have their own `completion` command.)18

Removes viper dependency by removing cobra/ CLI tool (#1604)John McBridebefore adoption5b2b9e9

lines 19–21why, one step back
19If you are using the `cobra-cli` generator,
20which can be found at [spf13/cobra-cli](https://github.com/spf13/cobra-cli),21you can create a completion command by running

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 22–23why, one step back
22
23```bash

Removes viper dependency by removing cobra/ CLI tool (#1604)John McBridebefore adoption5b2b9e9

line 24why, one step back
24cobra-cli add completion

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 25–30why, one step back
25```
26and then modifying the generated `cmd/completion.go` file to look something like this27(writing the shell script to stdout allows the most flexible use):2829```go30var completionCmd = &cobra.Command{

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

lines 31–32why, one step back
31 Use: "completion [bash|zsh|fish|powershell]",
32 Short: "Generate completion script",

Update shell_completions.md to have automatically rootcmd (#1372)Ron Greenbefore adoptiond298d79

line 33why, one step back
33 Long: fmt.Sprintf(`To load completions:

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 34–36why, one step back
34
35Bash:36

Update shell_completions.md to have automatically rootcmd (#1372)Ron Greenbefore adoptiond298d79

line 37why, one step back
37 $ source <(%[1]s completion bash)

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

line 38why, one step back
38

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

lines 39–40why, one step back
39 # To load completions for each session, execute once:
40 # Linux:

Update shell_completions.md to have automatically rootcmd (#1372)Ron Greenbefore adoptiond298d79

line 41why, one step back
41 $ %[1]s completion bash > /etc/bash_completion.d/%[1]s

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 42why, one step back
42 # macOS:

Update shell_completions.md to have automatically rootcmd (#1372)Ron Greenbefore adoptiond298d79

line 43why, one step back
43 $ %[1]s completion bash > /usr/local/etc/bash_completion.d/%[1]s

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 44–46why, one step back
44
45Zsh:46

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

lines 47–48why, one step back
47 # If shell completion is not already enabled in your environment,
48 # you will need to enable it. You can execute the following once:

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

line 49why, one step back
49

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 50why, one step back
50 $ echo "autoload -U compinit; compinit" >> ~/.zshrc

Improve zsh completion documentation (#1169)Marc Khouzambefore adoptionc6fe2d4

line 51why, one step back
51

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 52why, one step back
52 # To load completions for each session, execute once:

Update shell_completions.md to have automatically rootcmd (#1372)Ron Greenbefore adoptiond298d79

line 53why, one step back
53 $ %[1]s completion zsh > "${fpath[1]}/_%[1]s"

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

line 54why, one step back
54

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 55why, one step back
55 # You will need to start a new shell for this setup to take effect.

Improve zsh completion documentation (#1169)Marc Khouzambefore adoptionc6fe2d4

line 56why, one step back
56

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 57why, one step back
57fish:

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

line 58why, one step back
58

Update shell_completions.md to have automatically rootcmd (#1372)Ron Greenbefore adoptiond298d79

line 59why, one step back
59 $ %[1]s completion fish | source

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

line 60why, one step back
60

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 61why, one step back
61 # To load completions for each session, execute once:

Update shell_completions.md to have automatically rootcmd (#1372)Ron Greenbefore adoptiond298d79

line 62why, one step back
62 $ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish

powershell completion with custom comp (#1208)Paul Holzingerbefore adoptiona4ab3fa

line 63why, one step back
63

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 64why, one step back
64PowerShell:

powershell completion with custom comp (#1208)Paul Holzingerbefore adoptiona4ab3fa

line 65why, one step back
65

Update shell_completions.md to have automatically rootcmd (#1372)Ron Greenbefore adoptiond298d79

line 66why, one step back
66 PS> %[1]s completion powershell | Out-String | Invoke-Expression

powershell completion with custom comp (#1208)Paul Holzingerbefore adoptiona4ab3fa

line 67why, one step back
67

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 68why, one step back
68 # To load completions for every new session, run:

Update shell_completions.md to have automatically rootcmd (#1372)Ron Greenbefore adoptiond298d79

line 69why, one step back
69 PS> %[1]s completion powershell > %[1]s.ps1

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 70why, one step back
70 # and source this file from your PowerShell profile.

Update shell_completions.md to have automatically rootcmd (#1372)Ron Greenbefore adoptiond298d79

line 71why, one step back
71`,cmd.Root().Name()),

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 72–83why, one step back
72 DisableFlagsInUseLine: true,
73 ValidArgs: []string{"bash", "zsh", "fish", "powershell"},74 Args: cobra.ExactValidArgs(1),75 Run: func(cmd *cobra.Command, args []string) {76 switch args[0] {77 case "bash":78 cmd.Root().GenBashCompletion(os.Stdout)79 case "zsh":80 cmd.Root().GenZshCompletion(os.Stdout)81 case "fish":82 cmd.Root().GenFishCompletion(os.Stdout, true)83 case "powershell":

Create 'completion' command automatically (#1192)Marc Khouzambefore adoptionb312f0a

line 84why, one step back
84 cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 85–89why, one step back
85 }
86 },87}88```89

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 90why, one step back
90**Note:** The cobra generator may include messages printed to stdout, for example, if the config file is loaded; this will break the auto-completion script so must be removed.

Create 'completion' command automatically (#1192)Marc Khouzambefore adoptionb312f0a

lines 91–110why, one step back
91
92## Adapting the default completion command9394Cobra provides a few options for the default `completion` command. To configure such options you must set95the `CompletionOptions` field on the *root* command.9697To tell Cobra *not* to provide the default `completion` command:98```99rootCmd.CompletionOptions.DisableDefaultCmd = true100```101102To tell Cobra *not* to provide the user with the `--no-descriptions` flag to the completion sub-commands:103```104rootCmd.CompletionOptions.DisableNoDescFlag = true105```106107To tell Cobra to completely disable descriptions for completions:108```109rootCmd.CompletionOptions.DisableDescriptions = true110```

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 111–124why, one step back
111
112# Customizing completions113114The generated completion scripts will automatically handle completing commands and flags. However, you can make your completions much more powerful by providing information to complete your program's nouns and flag values.115116## Completion of nouns117118### Static completion of nouns119120Cobra allows you to provide a pre-defined list of completion choices for your nouns using the `ValidArgs` field.121For example, if you want `kubectl get [tab][tab]` to show a list of valid "nouns" you have to set them.122Some simplified code from `kubectl get` looks like:123124```go

Fix typo sh completion docs (#1678)Daniel Nguyenbefore adoptiond8184d3

line 125why, one step back
125validArgs = []string{ "pod", "node", "service", "replicationcontroller" }

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 126–132why, one step back
126
127cmd := &cobra.Command{128 Use: "get [(-o|--output=)json|yaml|template|...] (RESOURCE [NAME] | RESOURCE/NAME ...)",129 Short: "Display one or many resources",130 Long: get_long,131 Example: get_example,132 Run: func(cmd *cobra.Command, args []string) {

Use golangci-lint (#1044)Unai Martinez-Corralbefore adoption652c755

line 133why, one step back
133 cobra.CheckErr(RunGet(f, out, cmd, args))

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 134–150why, one step back
134 },
135 ValidArgs: validArgs,136}137```138139Notice we put the `ValidArgs` field on the `get` sub-command. Doing so will give results like:140141```bash142$ kubectl get [tab][tab]143node pod replicationcontroller service144```145146#### Aliases for nouns147148If your nouns have aliases, you can define them alongside `ValidArgs` using `ArgAliases`:149150```go

Fix typo sh completion docs (#1678)Daniel Nguyenbefore adoptiond8184d3

line 151why, one step back
151argAliases = []string { "pods", "nodes", "services", "svc", "replicationcontrollers", "rc" }

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 152–164why, one step back
152
153cmd := &cobra.Command{154 ...155 ValidArgs: validArgs,156 ArgAliases: argAliases157}158```159160The aliases are not shown to the user on tab completion, but they are accepted as valid nouns by161the completion algorithm if entered manually, e.g. in:162163```bash164$ kubectl get rc [tab][tab]

Use golangci-lint (#1044)Unai Martinez-Corralbefore adoption652c755

line 165why, one step back
165backend frontend database

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 166–286why, one step back
166```
167168Note that without declaring `rc` as an alias, the completion algorithm would not know to show the list of169replication controllers following `rc`.170171### Dynamic completion of nouns172173In some cases it is not possible to provide a list of completions in advance. Instead, the list of completions must be determined at execution-time. In a similar fashion as for static completions, you can use the `ValidArgsFunction` field to provide a Go function that Cobra will execute when it needs the list of completion choices for the nouns of a command. Note that either `ValidArgs` or `ValidArgsFunction` can be used for a single cobra command, but not both.174Simplified code from `helm status` looks like:175176```go177cmd := &cobra.Command{178 Use: "status RELEASE_NAME",179 Short: "Display the status of the named release",180 Long: status_long,181 RunE: func(cmd *cobra.Command, args []string) {182 RunGet(args[0])183 },184 ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {185 if len(args) != 0 {186 return nil, cobra.ShellCompDirectiveNoFileComp187 }188 return getReleasesFromCluster(toComplete), cobra.ShellCompDirectiveNoFileComp189 },190}191```192Where `getReleasesFromCluster()` is a Go function that obtains the list of current Helm releases running on the Kubernetes cluster.193Notice we put the `ValidArgsFunction` on the `status` sub-command. Let's assume the Helm releases on the cluster are: `harbor`, `notary`, `rook` and `thanos` then this dynamic completion will give results like:194195```bash196$ helm status [tab][tab]197harbor notary rook thanos198```199You may have noticed the use of `cobra.ShellCompDirective`. These directives are bit fields allowing to control some shell completion behaviors for your particular completion. You can combine them with the bit-or operator such as `cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp`200```go201// Indicates that the shell will perform its default behavior after completions202// have been provided (this implies none of the other directives).203ShellCompDirectiveDefault204205// Indicates an error occurred and completions should be ignored.206ShellCompDirectiveError207208// Indicates that the shell should not add a space after the completion,209// even if there is a single completion provided.210ShellCompDirectiveNoSpace211212// Indicates that the shell should not provide file completion even when213// no completion is provided.214ShellCompDirectiveNoFileComp215216// Indicates that the returned completions should be used as file extension filters.217// For example, to complete only files of the form *.json or *.yaml:218// return []string{"yaml", "json"}, ShellCompDirectiveFilterFileExt219// For flags, using MarkFlagFilename() and MarkPersistentFlagFilename()220// is a shortcut to using this directive explicitly.221//222ShellCompDirectiveFilterFileExt223224// Indicates that only directory names should be provided in file completion.225// For example:226// return nil, ShellCompDirectiveFilterDirs227// For flags, using MarkFlagDirname() is a shortcut to using this directive explicitly.228//229// To request directory names within another directory, the returned completions230// should specify a single directory name within which to search. For example,231// to complete directories within "themes/":232// return []string{"themes"}, ShellCompDirectiveFilterDirs233//234ShellCompDirectiveFilterDirs235```236237***Note***: When using the `ValidArgsFunction`, Cobra will call your registered function after having parsed all flags and arguments provided in the command-line. You therefore don't need to do this parsing yourself. For example, when a user calls `helm status --namespace my-rook-ns [tab][tab]`, Cobra will call your registered `ValidArgsFunction` after having parsed the `--namespace` flag, as it would have done when calling the `RunE` function.238239#### Debugging240241Cobra achieves dynamic completion through the use of a hidden command called by the completion script. To debug your Go completion code, you can call this hidden command directly:242```bash243$ helm __complete status har<ENTER>244harbor245:4246Completion ended with directive: ShellCompDirectiveNoFileComp # This is on stderr247```248***Important:*** If the noun to complete is empty (when the user has not yet typed any letters of that noun), you must pass an empty parameter to the `__complete` command:249```bash250$ helm __complete status ""<ENTER>251harbor252notary253rook254thanos255:4256Completion ended with directive: ShellCompDirectiveNoFileComp # This is on stderr257```258Calling the `__complete` command directly allows you to run the Go debugger to troubleshoot your code. You can also add printouts to your code; Cobra provides the following functions to use for printouts in Go completion code:259```go260// Prints to the completion script debug file (if BASH_COMP_DEBUG_FILE261// is set to a file path) and optionally prints to stderr.262cobra.CompDebug(msg string, printToStdErr bool) {263cobra.CompDebugln(msg string, printToStdErr bool)264265// Prints to the completion script debug file (if BASH_COMP_DEBUG_FILE266// is set to a file path) and to stderr.267cobra.CompError(msg string)268cobra.CompErrorln(msg string)269```270***Important:*** You should **not** leave traces that print directly to stdout in your completion code as they will be interpreted as completion choices by the completion script. Instead, use the cobra-provided debugging traces functions mentioned above.271272## Completions for flags273274### Mark flags as required275276Most of the time completions will only show sub-commands. But if a flag is required to make a sub-command work, you probably want it to show up when the user types [tab][tab]. You can mark a flag as 'Required' like so:277278```go279cmd.MarkFlagRequired("pod")280cmd.MarkFlagRequired("container")281```282283and you'll get something like284285```bash286$ kubectl exec [tab][tab]

Use golangci-lint (#1044)Unai Martinez-Corralbefore adoption652c755

line 287why, one step back
287-c --container= -p --pod=

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 288–356why, one step back
288```
289290### Specify dynamic flag completion291292As for nouns, Cobra provides a way of defining dynamic completion of flags. To provide a Go function that Cobra will execute when it needs the list of completion choices for a flag, you must register the function using the `command.RegisterFlagCompletionFunc()` function.293294```go295flagName := "output"296cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {297 return []string{"json", "table", "yaml"}, cobra.ShellCompDirectiveDefault298})299```300Notice that calling `RegisterFlagCompletionFunc()` is done through the `command` with which the flag is associated. In our example this dynamic completion will give results like so:301302```bash303$ helm status --output [tab][tab]304json table yaml305```306307#### Debugging308309You can also easily debug your Go completion code for flags:310```bash311$ helm __complete status --output ""312json313table314yaml315:4316Completion ended with directive: ShellCompDirectiveNoFileComp # This is on stderr317```318***Important:*** You should **not** leave traces that print to stdout in your completion code as they will be interpreted as completion choices by the completion script. Instead, use the cobra-provided debugging traces functions mentioned further above.319320### Specify valid filename extensions for flags that take a filename321322To limit completions of flag values to file names with certain extensions you can either use the different `MarkFlagFilename()` functions or a combination of `RegisterFlagCompletionFunc()` and `ShellCompDirectiveFilterFileExt`, like so:323```go324flagName := "output"325cmd.MarkFlagFilename(flagName, "yaml", "json")326```327or328```go329flagName := "output"330cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {331 return []string{"yaml", "json"}, ShellCompDirectiveFilterFileExt})332```333334### Limit flag completions to directory names335336To limit completions of flag values to directory names you can either use the `MarkFlagDirname()` functions or a combination of `RegisterFlagCompletionFunc()` and `ShellCompDirectiveFilterDirs`, like so:337```go338flagName := "output"339cmd.MarkFlagDirname(flagName)340```341or342```go343flagName := "output"344cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {345 return nil, cobra.ShellCompDirectiveFilterDirs346})347```348To limit completions of flag values to directory names *within another directory* you can use a combination of `RegisterFlagCompletionFunc()` and `ShellCompDirectiveFilterDirs` like so:349```go350flagName := "output"351cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {352 return []string{"themes"}, cobra.ShellCompDirectiveFilterDirs353})354```355### Descriptions for completions356

Fix documentation (#1434)Marc Khouzambefore adoption07861c8

lines 357–360why, one step back
357Cobra provides support for completion descriptions. Such descriptions are supported for each shell
358(however, for bash, it is only available in the [completion V2 version](#bash-completion-v2)).359For commands and flags, Cobra will provide the descriptions automatically, based on usage information.360For example, using zsh:

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 361–372why, one step back
361```
362$ helm s[tab]363search -- search for a keyword in charts364show -- show information of a chart365status -- displays the status of the named release366```367while using fish:368```369$ helm s[tab]370search (search for a keyword in charts) show (show information of a chart) status (displays the status of the named release)371```372

Fix documentation (#1434)Marc Khouzambefore adoption07861c8

line 373why, one step back
373Cobra allows you to add descriptions to your own completions. Simply add the description text after each completion, following a `\t` separator. This technique applies to completions returned by `ValidArgs`, `ValidArgsFunction` and `RegisterFlagCompletionFunc()`. For example:

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 374–404why, one step back
374```go
375ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {376 return []string{"harbor\tAn image registry", "thanos\tLong-term metrics"}, cobra.ShellCompDirectiveNoFileComp377}}378```379or380```go381ValidArgs: []string{"bash\tCompletions for bash", "zsh\tCompletions for zsh"}382```383## Bash completions384385### Dependencies386387The bash completion script generated by Cobra requires the `bash_completion` package. You should update the help text of your completion command to show how to install the `bash_completion` package ([Kubectl docs](https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion))388389### Aliases390391You can also configure `bash` aliases for your program and they will also support completions.392393```bash394alias aliasname=origcommand395complete -o default -F __start_origcommand aliasname396397# and now when you run `aliasname` completion will make398# suggestions as it did for `origcommand`.399400$ aliasname <tab><tab>401completion firstcommand secondcommand402```403### Bash legacy dynamic completions404

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 405why, one step back
405For backward compatibility, Cobra still supports its bash legacy dynamic completion solution.

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 406–407why, one step back
406Please refer to [Bash Completions](bash_completions.md) for details.
407

Bash completion V2 with completion descriptions (#1146)Marc Khouzambefore adoptionb361960

lines 408–413why, one step back
408### Bash completion V2
409410Cobra provides two versions for bash completion. The original bash completion (which started it all!) can be used by calling411`GenBashCompletion()` or `GenBashCompletionFile()`.412413A new V2 bash completion version is also available. This version can be used by calling `GenBashCompletionV2()` or

Fix documentation (#1434)Marc Khouzambefore adoption07861c8

lines 414–416why, one step back
414`GenBashCompletionFileV2()`. The V2 version does **not** support the legacy dynamic completion
415(see [Bash Completions](bash_completions.md)) but instead works only with the Go dynamic completion416solution described in this document.

Bash completion V2 with completion descriptions (#1146)Marc Khouzambefore adoptionb361960

lines 417–438why, one step back
417Unless your program already uses the legacy dynamic completion solution, it is recommended that you use the bash
418completion V2 solution which provides the following extra features:419- Supports completion descriptions (like the other shells)420- Small completion script of less than 300 lines (v1 generates scripts of thousands of lines; `kubectl` for example has a bash v1 completion script of over 13K lines)421- Streamlined user experience thanks to a completion behavior aligned with the other shells 422423`Bash` completion V2 supports descriptions for completions. When calling `GenBashCompletionV2()` or `GenBashCompletionFileV2()`424you must provide these functions with a parameter indicating if the completions should be annotated with a description; Cobra425will provide the description automatically based on usage information. You can choose to make this option configurable by426your users.427428```429# With descriptions430$ helm s[tab][tab]431search (search for a keyword in charts) status (display the status of the named release)432show (show information of a chart)433434# Without descriptions435$ helm s[tab][tab]436search show status437```438**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.

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 439–440why, one step back
439## Zsh completions
440

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 441why, one step back
441Cobra supports native zsh completion generated from the root `cobra.Command`.

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

line 442why, one step back
442The generated completion script should be put somewhere in your `$fpath` and be named

Improve zsh completion documentation (#1169)Marc Khouzambefore adoptionc6fe2d4

line 443why, one step back
443`_<yourProgram>`. You will need to start a new shell for the completions to become available.

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 444–459why, one step back
444
445Zsh supports descriptions for completions. Cobra will provide the description automatically,446based on usage information. Cobra provides a way to completely disable such descriptions by447using `GenZshCompletionNoDesc()` or `GenZshCompletionFileNoDesc()`. You can choose to make448this a configurable option to your users.449```450# With descriptions451$ helm s[tab]452search -- search for a keyword in charts453show -- show information of a chart454status -- displays the status of the named release455456# Without descriptions457$ helm s[tab]458search show status459```

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 460why, one step back
460*Note*: Because of backward-compatibility requirements, we were forced to have a different API to disable completion descriptions between `zsh` and `fish`.

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 461–464why, one step back
461
462### Limitations463464* Custom completions implemented in Bash scripting (legacy) are not supported and will be ignored for `zsh` (including the use of the `BashCompCustom` flag annotation).

powershell completion with custom comp (#1208)Paul Holzingerbefore adoptiona4ab3fa

line 465why, one step back
465 * You should instead use `ValidArgsFunction` and `RegisterFlagCompletionFunc()` which are portable to the different shells (`bash`, `zsh`, `fish`, `powershell`).

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 466–470why, one step back
466* The function `MarkFlagCustom()` is not supported and will be ignored for `zsh`.
467 * You should instead use `RegisterFlagCompletionFunc()`.468469### Zsh completions standardization470

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 471why, one step back
471Cobra 1.1 standardized its zsh completion support to align it with its other shell completions. Although the API was kept backward-compatible, some small changes in behavior were introduced.

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 472–473why, one step back
472Please refer to [Zsh Completions](zsh_completions.md) for details.
473

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 474why, one step back
474## fish completions

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

line 475why, one step back
475

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 476why, one step back
476Cobra supports native fish completions generated from the root `cobra.Command`. You can use the `command.GenFishCompletion()` or `command.GenFishCompletionFile()` functions. You must provide these functions with a parameter indicating if the completions should be annotated with a description; Cobra will provide the description automatically based on usage information. You can choose to make this option configurable by your users.

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 477–485why, one step back
477```
478# With descriptions479$ helm s[tab]480search (search for a keyword in charts) show (show information of a chart) status (displays the status of the named release)481482# Without descriptions483$ helm s[tab]484search show status485```

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 486why, one step back
486*Note*: Because of backward-compatibility requirements, we were forced to have a different API to disable completion descriptions between `zsh` and `fish`.

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 487–489why, one step back
487
488### Limitations489

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 490why, one step back
490* Custom completions implemented in bash scripting (legacy) are not supported and will be ignored for `fish` (including the use of the `BashCompCustom` flag annotation).

powershell completion with custom comp (#1208)Paul Holzingerbefore adoptiona4ab3fa

line 491why, one step back
491 * You should instead use `ValidArgsFunction` and `RegisterFlagCompletionFunc()` which are portable to the different shells (`bash`, `zsh`, `fish`, `powershell`).

Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3

lines 492–505why, one step back
492* The function `MarkFlagCustom()` is not supported and will be ignored for `fish`.
493 * You should instead use `RegisterFlagCompletionFunc()`.494* The following flag completion annotations are not supported and will be ignored for `fish`:495 * `BashCompFilenameExt` (filtering by file extension)496 * `BashCompSubdirsInDir` (filtering by directory)497* The functions corresponding to the above annotations are consequently not supported and will be ignored for `fish`:498 * `MarkFlagFilename()` and `MarkPersistentFlagFilename()` (filtering by file extension)499 * `MarkFlagDirname()` and `MarkPersistentFlagDirname()` (filtering by directory)500* Similarly, the following completion directives are not supported and will be ignored for `fish`:501 * `ShellCompDirectiveFilterFileExt` (filtering by file extension)502 * `ShellCompDirectiveFilterDirs` (filtering by directory)503504## PowerShell completions505

powershell completion with custom comp (#1208)Paul Holzingerbefore adoptiona4ab3fa

lines 506–507why, one step back
506Cobra supports native PowerShell completions generated from the root `cobra.Command`. You can use the `command.GenPowerShellCompletion()` or `command.GenPowerShellCompletionFile()` functions. To include descriptions use `command.GenPowerShellCompletionWithDesc()` and `command.GenPowerShellCompletionFileWithDesc()`. Cobra will provide the description automatically based on usage information. You can choose to make this option configurable by your users.
507

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 508why, one step back
508The script is designed to support all three PowerShell completion modes:

powershell completion with custom comp (#1208)Paul Holzingerbefore adoptiona4ab3fa

lines 509–535why, one step back
509
510* TabCompleteNext (default windows style - on each key press the next option is displayed)511* Complete (works like bash)512* MenuComplete (works like zsh)513514You set the mode with `Set-PSReadLineKeyHandler -Key Tab -Function <mode>`. Descriptions are only displayed when using the `Complete` or `MenuComplete` mode.515516Users need PowerShell version 5.0 or above, which comes with Windows 10 and can be downloaded separately for Windows 7 or 8.1. They can then write the completions to a file and source this file from their PowerShell profile, which is referenced by the `$Profile` environment variable. See `Get-Help about_Profiles` for more info about PowerShell profiles.517518```519# With descriptions and Mode 'Complete'520$ helm s[tab]521search (search for a keyword in charts) show (show information of a chart) status (displays the status of the named release)522523# With descriptions and Mode 'MenuComplete' The description of the current selected value will be displayed below the suggestions.524$ helm s[tab]525search show status 526527search for a keyword in charts528529# Without descriptions530$ helm s[tab]531search show status532```533534### Limitations535

Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea

line 536why, one step back
536* Custom completions implemented in bash scripting (legacy) are not supported and will be ignored for `powershell` (including the use of the `BashCompCustom` flag annotation).

powershell completion with custom comp (#1208)Paul Holzingerbefore adoptiona4ab3fa

lines 537–548why, one step back
537 * You should instead use `ValidArgsFunction` and `RegisterFlagCompletionFunc()` which are portable to the different shells (`bash`, `zsh`, `fish`, `powershell`).
538* The function `MarkFlagCustom()` is not supported and will be ignored for `powershell`.539 * You should instead use `RegisterFlagCompletionFunc()`.540* The following flag completion annotations are not supported and will be ignored for `powershell`:541 * `BashCompFilenameExt` (filtering by file extension)542 * `BashCompSubdirsInDir` (filtering by directory)543* The functions corresponding to the above annotations are consequently not supported and will be ignored for `powershell`:544 * `MarkFlagFilename()` and `MarkPersistentFlagFilename()` (filtering by file extension)545 * `MarkFlagDirname()` and `MarkPersistentFlagDirname()` (filtering by directory)546* Similarly, the following completion directives are not supported and will be ignored for `powershell`:547 * `ShellCompDirectiveFilterFileExt` (filtering by file extension)548 * `ShellCompDirectiveFilterDirs` (filtering by directory)