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
Provenance
- Commit
07445ea- Tree
77f95aa- Author
Anthony Fok <foka@debian.org>- Committer
Anthony Fok <foka@debian.org>- Parent
b73b344
4 files changed+38 added−38 removed
modified:bash_completions.md+1 added-1 removed
2 unmodified lines34567789101180 unmodified lines2 unmodified linesPlease refer to [Shell Completions](shell_completions.md) for details.
## Bash legacy dynamic completions
For backwards-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.
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:80 unmodified linesmodified:powershell_completions.go+1 added-1 removed
179 unmodified lines180 181 # store temporay because switch will overwrite $_182 $comp = $_183 184- # 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 linesmodified:README.md+1 added-1 removed
751 unmodified lines752 Cobra can generate documentation based on subcommands, flags, etc. Read more about it in the [docs generation documentation](doc/README.md).753 754 ## Generating shell completions755 756-Cobra 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).756+Cobra 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).757 758 # License759 760 Cobra 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 lines3 Cobra can generate shell completions for multiple shells.4 The currently supported shells are:5 - Bash6 - Zsh7-- Fish7+- fish8 - PowerShell9 10-If you are using the generator you can create a completion command by running10+If you are using the generator, you can create a completion command by running11 12 ```bash13 cobra add completion14 ```1 unmodified line16 (writing the shell script to stdout allows the most flexible use):17 18 ```go19 var 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:23 24 Bash:25 26-$ source <(yourprogram completion bash)26+ $ source <(yourprogram completion bash)27 28-# To load completions for each session, execute once:29-Linux:28+ # To load completions for each session, execute once:29+ # Linux:30 $ yourprogram completion bash > /etc/bash_completion.d/yourprogram31-MacOS:31+ # macOS:32 $ yourprogram completion bash > /usr/local/etc/bash_completion.d/yourprogram33 34 Zsh:35 36-# 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:38 39-$ echo "autoload -U compinit; compinit" >> ~/.zshrc39+ $ echo "autoload -U compinit; compinit" >> ~/.zshrc40 41-# 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"43 44-# 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.45 46-Fish:46+fish:47 48-$ yourprogram completion fish | source48+ $ yourprogram completion fish | source49 50-# 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.fish52 53-Powershell:53+PowerShell:54 55-PS> yourprogram completion powershell | Out-String | Invoke-Expression55+ PS> yourprogram completion powershell | Out-String | Invoke-Expression56 57-# To load completions for every new session, run:58-PS> 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 lines75 },76 }77 ```78 79-**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.80 81 # Customizing completions82 83 The 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 lines367 completion firstcommand secondcommand368 ```369 ### Bash legacy dynamic completions370 371-For backwards-compatibility, Cobra still supports its bash legacy dynamic completion solution.371+For backward compatibility, Cobra still supports its bash legacy dynamic completion solution.372 Please refer to [Bash Completions](bash_completions.md) for details.373 374 ## Zsh completions375 376-Cobra supports native Zsh completion generated from the root `cobra.Command`.376+Cobra supports native zsh completion generated from the root `cobra.Command`.377 The 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.379 380 Zsh supports descriptions for completions. Cobra will provide the description automatically,10 unmodified lines391 # Without descriptions392 $ helm s[tab]393 search 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`.396 397 ### Limitations398 399 * 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 lines402 * You should instead use `RegisterFlagCompletionFunc()`.403 404 ### Zsh completions standardization405 406-Cobra 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.406+Cobra 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.407 Please refer to [Zsh Completions](zsh_completions.md) for details.408 409-## Fish completions409+## fish completions410 411-Cobra 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.411+Cobra 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]415 search (search for a keyword in charts) show (show information of a chart) status (displays the status of the named release)1 unmodified line417 # Without descriptions418 $ helm s[tab]419 search 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`.422 423 ### Limitations424 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).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 lines439 ## PowerShell completions440 441 Cobra 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.442 443-The script is designed to support all three Powershell completion modes:443+The script is designed to support all three PowerShell completion modes:444 445 * 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 lines467 ```468 469 ### Limitations470 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).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 lines479 * `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)