bash_completions.md
This file
Path
bash_completions.md
93 lines · 3.7 KiB · Markdown
Last changed by
Bash completion V2 with completion descriptions (#1146)Marc Khouzambefore adoptionb361960
History of this fileTake it
The walk stopped after 200 commits: lines older than 618bc00 point at it.
Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3
line 1why, one step back1# Generating Bash Completions For Your cobra.CommandAllow for explicit output to err/stderrJuan Lenibefore adoption618bc00
line 2why, one step back2Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3
line 3why, one step back3Please refer to [Shell Completions](shell_completions.md) for details.Allow for explicit output to err/stderrJuan Lenibefore adoption618bc00
line 4why, one step back4Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3
line 5why, one step back5## Bash legacy dynamic completionsAllow for explicit output to err/stderrJuan Lenibefore adoption618bc00
line 6why, one step back6Copyedit shell-completion related documentationAnthony Fokbefore adoption07445ea
line 7why, one step back7For 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.Allow for explicit output to err/stderrJuan Lenibefore adoption618bc00
line 8why, one step back8Bash completion V2 with completion descriptions (#1146)Marc Khouzambefore adoptionb361960
lines 9–10why, one step back9**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.10Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3
line 11why, one step back11The 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.Allow for explicit output to err/stderrJuan Lenibefore adoption618bc00
line 12why, one step back12Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3
line 13why, one step back13Some code that works in kubernetes:Allow for explicit output to err/stderrJuan Lenibefore adoption618bc00
line 14why, one step back14Add support for custom completions in Go (#1035)Marc Khouzambefore adoptionb80aeb1
line 15why, one step back15```bashAllow for explicit output to err/stderrJuan Lenibefore adoption618bc00
lines 16–65why, one step back16const (17 bash_completion_func = `__kubectl_parse_get()18{19 local kubectl_output out20 if kubectl_output=$(kubectl get --no-headers "$1" 2>/dev/null); then21 out=($(echo "${kubectl_output}" | awk '{print $1}'))22 COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) )23 fi24}2526__kubectl_get_resource()27{28 if [[ ${#nouns[@]} -eq 0 ]]; then29 return 130 fi31 __kubectl_parse_get ${nouns[${#nouns[@]} -1]}32 if [[ $? -eq 0 ]]; then33 return 034 fi35}3637__kubectl_custom_func() {38 case ${last_command} in39 kubectl_get | kubectl_describe | kubectl_delete | kubectl_stop)40 __kubectl_get_resource41 return42 ;;43 *)44 ;;45 esac46}47`)48```4950And then I set that in my command definition:5152```go53cmds := &cobra.Command{54 Use: "kubectl",55 Short: "kubectl controls the Kubernetes cluster manager",56 Long: `kubectl controls the Kubernetes cluster manager.5758Find more information at https://github.com/GoogleCloudPlatform/kubernetes.`,59 Run: runHelp,60 BashCompletionFunction: bash_completion_func,61}62```6364The `BashCompletionFunction` option is really only valid/useful on the root command. Doing the above will cause `__kubectl_custom_func()` (`__<command-use>_custom_func()`) to be called when the built in processor was unable to find a solution. In the case of kubernetes a valid command might look something like `kubectl get pod [mypod]`. If you type `kubectl get pod [tab][tab]` the `__kubectl_customc_func()` will run because the cobra.Command only understood "kubectl" and "get." `__kubectl_custom_func()` will see that the cobra.Command is "kubectl_get" and will thus call another helper `__kubectl_get_resource()`. `__kubectl_get_resource` will look at the 'nouns' collected. In our example the only noun will be `pod`. So it will call `__kubectl_parse_get pod`. `__kubectl_parse_get` will actually call out to kubernetes and get any pods. It will then set `COMPREPLY` to valid pods!65Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3
line 66why, one step back66Similarly, for flags:Allow for explicit output to err/stderrJuan Lenibefore adoption618bc00
lines 67–79why, one step back6768```go69 annotation := make(map[string][]string)70 annotation[cobra.BashCompCustom] = []string{"__kubectl_get_namespaces"}7172 flag := &pflag.Flag{73 Name: "namespace",74 Usage: usage,75 Annotations: annotation,76 }77 cmd.Flags().AddFlag(flag)78```79Add support for custom completions in Go (#1035)Marc Khouzambefore adoptionb80aeb1
line 80why, one step back80In addition add the `__kubectl_get_namespaces` implementation in the `BashCompletionFunction`Allow for explicit output to err/stderrJuan Lenibefore adoption618bc00
lines 81–93why, one step back81value, e.g.:8283```bash84__kubectl_get_namespaces()85{86 local template87 template="{{ range .items }}{{ .metadata.name }} {{ end }}"88 local kubectl_out89 if kubectl_out=$(kubectl get -o template --template="${template}" namespace 2>/dev/null); then90 COMPREPLY=( $( compgen -W "${kubectl_out}[*]" -- "$cur" ) )91 fi92}93```