Skip to content
Myr

Copyedit shell-completion related documentation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Anthony Fok · committed by Anthony Fok · parent b73b344 · Browse files · 4 files+38 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+38 added−38 removed

  • other:+38 added−38 removed

4 files+38 added−38 removed

modified:bash_completions.md+1 added-1 removed
modified:powershell_completions.go+1 added-1 removed
179 unmodified lines
180181 # store temporay because switch will overwrite $_182 $comp = $_183184 # Powershell supports three different completion modes184 # PowerShell supports three different completion modes185 # - TabCompleteNext (default windows style - on each key press the next option is displayed)186 # - Complete (works like bash)187 # - MenuComplete (works like zsh)188 # You set the mode with Set-PSReadLineKeyHandler -Key Tab -Function <mode>
97 unmodified lines
modified:README.md+1 added-1 removed
751 unmodified lines
752Cobra can generate documentation based on subcommands, flags, etc. Read more about it in the [docs generation documentation](doc/README.md).753754## Generating shell completions755756Cobra can generate a shell-completion file for the following shells: Bash, Zsh, Fish, Powershell. If you add more information to your commands, these completions can be amazingly powerful and flexible. Read more about it in [Shell Completions](shell_completions.md).756Cobra can generate a shell-completion file for the following shells: bash, zsh, fish, PowerShell. If you add more information to your commands, these completions can be amazingly powerful and flexible. Read more about it in [Shell Completions](shell_completions.md).757758# License759760Cobra is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/spf13/cobra/blob/master/LICENSE.txt)
modified:shell_completions.md+35 added-35 removed
2 unmodified lines
3Cobra can generate shell completions for multiple shells.4The currently supported shells are:5- Bash6- Zsh7- Fish7- fish8- PowerShell910If you are using the generator you can create a completion command by running10If you are using the generator, you can create a completion command by running1112```bash13cobra add completion14```
1 unmodified line
16(writing the shell script to stdout allows the most flexible use):1718```go19var completionCmd = &cobra.Command{20 Use: "completion [bash|zsh|fish|powershell]",21 Short: "Generate completion script",20 Use: "completion [bash|zsh|fish|powershell]",21 Short: "Generate completion script",22 Long: `To load completions:2324Bash:2526$ source <(yourprogram completion bash)26 $ source <(yourprogram completion bash)2728# To load completions for each session, execute once:29Linux:28 # To load completions for each session, execute once:29 # Linux:30 $ yourprogram completion bash > /etc/bash_completion.d/yourprogram31MacOS:31 # macOS:32 $ yourprogram completion bash > /usr/local/etc/bash_completion.d/yourprogram3334Zsh:3536# If shell completion is not already enabled in your environment you will need37# to enable it. You can execute the following once:36 # If shell completion is not already enabled in your environment,37 # you will need to enable it. You can execute the following once:3839$ echo "autoload -U compinit; compinit" >> ~/.zshrc39 $ echo "autoload -U compinit; compinit" >> ~/.zshrc4041# To load completions for each session, execute once:42$ yourprogram completion zsh > "${fpath[1]}/_yourprogram"41 # To load completions for each session, execute once:42 $ yourprogram completion zsh > "${fpath[1]}/_yourprogram"4344# You will need to start a new shell for this setup to take effect.44 # You will need to start a new shell for this setup to take effect.4546Fish:46fish:4748$ yourprogram completion fish | source48 $ yourprogram completion fish | source4950# To load completions for each session, execute once:51$ yourprogram completion fish > ~/.config/fish/completions/yourprogram.fish50 # To load completions for each session, execute once:51 $ yourprogram completion fish > ~/.config/fish/completions/yourprogram.fish5253Powershell:53PowerShell:5455PS> yourprogram completion powershell | Out-String | Invoke-Expression55 PS> yourprogram completion powershell | Out-String | Invoke-Expression5657# To load completions for every new session, run:58PS> yourprogram completion powershell > yourprogram.ps159# and source this file from your powershell profile.57 # To load completions for every new session, run:58 PS> yourprogram completion powershell > yourprogram.ps159 # and source this file from your PowerShell profile.60`,61 DisableFlagsInUseLine: true,62 ValidArgs: []string{"bash", "zsh", "fish", "powershell"},63 Args: cobra.ExactValidArgs(1),
11 unmodified lines
75 },76}77```7879**Note:** The cobra generator may include messages printed to stdout for example if the config file is loaded, this will break the auto complete script so must be removed.79**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.8081# Customizing completions8283The 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.
283 unmodified lines
367completion firstcommand secondcommand368```369### Bash legacy dynamic completions370371For backwards-compatibility, Cobra still supports its bash legacy dynamic completion solution.371For backward compatibility, Cobra still supports its bash legacy dynamic completion solution.372Please refer to [Bash Completions](bash_completions.md) for details.373374## Zsh completions375376Cobra supports native Zsh completion generated from the root `cobra.Command`.376Cobra supports native zsh completion generated from the root `cobra.Command`.377The generated completion script should be put somewhere in your `$fpath` and be named378`_<yourProgram>`. You will need to start a new shell for the completions to become available.379380Zsh supports descriptions for completions. Cobra will provide the description automatically,
10 unmodified lines
391# Without descriptions392$ helm s[tab]393search show status394```395*Note*: Because of backwards-compatibility requirements, we were forced to have a different API to disable completion descriptions between `Zsh` and `Fish`.395*Note*: Because of backward-compatibility requirements, we were forced to have a different API to disable completion descriptions between `zsh` and `fish`.396397### Limitations398399* Custom completions implemented in Bash scripting (legacy) are not supported and will be ignored for `zsh` (including the use of the `BashCompCustom` flag annotation).
2 unmodified lines
402 * You should instead use `RegisterFlagCompletionFunc()`.403404### Zsh completions standardization405406Cobra 1.1 standardized its zsh completion support to align it with its other shell completions. Although the API was kept backwards-compatible, some small changes in behavior were introduced.406Cobra 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.407Please refer to [Zsh Completions](zsh_completions.md) for details.408409## Fish completions409## fish completions410411Cobra 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.411Cobra 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.412```413# With descriptions414$ helm s[tab]415search (search for a keyword in charts) show (show information of a chart) status (displays the status of the named release)
1 unmodified line
417# Without descriptions418$ helm s[tab]419search show status420```421*Note*: Because of backwards-compatibility requirements, we were forced to have a different API to disable completion descriptions between `Zsh` and `Fish`.421*Note*: Because of backward-compatibility requirements, we were forced to have a different API to disable completion descriptions between `zsh` and `fish`.422423### Limitations424425* Custom completions implemented in Bash scripting (legacy) are not supported and will be ignored for `fish` (including the use of the `BashCompCustom` flag annotation).425* Custom completions implemented in bash scripting (legacy) are not supported and will be ignored for `fish` (including the use of the `BashCompCustom` flag annotation).426 * You should instead use `ValidArgsFunction` and `RegisterFlagCompletionFunc()` which are portable to the different shells (`bash`, `zsh`, `fish`, `powershell`).427* The function `MarkFlagCustom()` is not supported and will be ignored for `fish`.428 * You should instead use `RegisterFlagCompletionFunc()`.429* The following flag completion annotations are not supported and will be ignored for `fish`:
9 unmodified lines
439## PowerShell completions440441Cobra 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.442443The script is designed to support all three Powershell completion modes:443The script is designed to support all three PowerShell completion modes:444445* TabCompleteNext (default windows style - on each key press the next option is displayed)446* Complete (works like bash)447* MenuComplete (works like zsh)
19 unmodified lines
467```468469### Limitations470471* Custom completions implemented in Bash scripting (legacy) are not supported and will be ignored for `powershell` (including the use of the `BashCompCustom` flag annotation).471* Custom completions implemented in bash scripting (legacy) are not supported and will be ignored for `powershell` (including the use of the `BashCompCustom` flag annotation).472 * You should instead use `ValidArgsFunction` and `RegisterFlagCompletionFunc()` which are portable to the different shells (`bash`, `zsh`, `fish`, `powershell`).473* The function `MarkFlagCustom()` is not supported and will be ignored for `powershell`.474 * You should instead use `RegisterFlagCompletionFunc()`.475* The following flag completion annotations are not supported and will be ignored for `powershell`:
3 unmodified lines
479 * `MarkFlagFilename()` and `MarkPersistentFlagFilename()` (filtering by file extension)480 * `MarkFlagDirname()` and `MarkPersistentFlagDirname()` (filtering by directory)481* Similarly, the following completion directives are not supported and will be ignored for `powershell`:482 * `ShellCompDirectiveFilterFileExt` (filtering by file extension)483 * `ShellCompDirectiveFilterDirs` (filtering by directory)483 * `ShellCompDirectiveFilterDirs` (filtering by directory)