active_help.md
This file
Path
site/content/active_help.md
168 lines · 8.4 KiB · Markdown
Last changed by
feat: add CompletionWithDesc helper (#2231)ccoVeillebefore adoption8cb30f9
History of this fileTake it
Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 1–4why, one step back1# Active Help23Active Help is a framework provided by Cobra which allows a program to define messages (hints, warnings, etc) that will be printed during program usage. It aims to make it easier for your users to learn how to use your program. If configured by the program, Active Help is printed when the user triggers shell completion.4Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
lines 5–8why, one step back5For example,67```console8$ helm repo add [tab]Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 9–10why, one step back9You must choose a name for the repo you are adding.10Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 11why, one step back11$ bin/helm package [tab]Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 12–13why, one step back12Please specify the path to the chart to package13Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 14why, one step back14$ bin/helm package [tab][tab]Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 15–18why, one step back15bin/ internal/ scripts/ pkg/ testdata/16```1718**Hint**: A good place to use Active Help messages is when the normal completion system does not provide any suggestions. In such cases, Active Help nicely supplements the normal shell completions to guide the user in knowing what is expected by the program.Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 19why, one step back19Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 20–22why, one step back20## Supported shells2122Active Help is currently only supported for the following shells:Fix broken links in active_help.md (#2202)Vui Lambefore adoption02326d5
line 23why, one step back23- Bash (using [bash completion V2](completions/_index.md#bash-completion-v2) only). Note that bash 4.4 or higher is required for the prompt to appear when an Active Help message is printed.Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 24–27why, one step back24- Zsh2526## Adding Active Help messages27Fix broken links in active_help.md (#2202)Vui Lambefore adoption02326d5
line 28why, one step back28As Active Help uses the shell completion system, the implementation of Active Help messages is done by enhancing custom dynamic completions. If you are not familiar with dynamic completions, please refer to [Shell Completions](completions/_index.md).Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 29–43why, one step back2930Adding Active Help is done through the use of the `cobra.AppendActiveHelp(...)` function, where the program repeatedly adds Active Help messages to the list of completions. Keep reading for details.3132### Active Help for nouns3334Adding Active Help when completing a noun is done within the `ValidArgsFunction(...)` of a command. Please notice the use of `cobra.AppendActiveHelp(...)` in the following example:3536```go37cmd := &cobra.Command{38 Use: "add [NAME] [URL]",39 Short: "add a chart repository",40 Args: require.ExactArgs(2),41 RunE: func(cmd *cobra.Command, args []string) error {42 return addRepo(args)43 },feat: add CompletionWithDesc helper (#2231)ccoVeillebefore adoption8cb30f9
lines 44–45why, one step back44 ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {45 var comps []cobra.CompletionMove documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 46–56why, one step back46 if len(args) == 0 {47 comps = cobra.AppendActiveHelp(comps, "You must choose a name for the repo you are adding")48 } else if len(args) == 1 {49 comps = cobra.AppendActiveHelp(comps, "You must specify the URL for the repo you are adding")50 } else {51 comps = cobra.AppendActiveHelp(comps, "This command does not take any more arguments")52 }53 return comps, cobra.ShellCompDirectiveNoFileComp54 },55}56```Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 57why, one step back57Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
line 58why, one step back58The example above defines the completions (none, in this specific example) as well as the Active Help messages for the `helm repo add` command. It yields the following behavior:Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
lines 59–61why, one step back5960```console61$ helm repo add [tab]Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 62–63why, one step back62You must choose a name for the repo you are adding63Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 64why, one step back64$ helm repo add grafana [tab]Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 65–66why, one step back65You must specify the URL for the repo you are adding66Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 67why, one step back67$ helm repo add grafana https://grafana.github.io/helm-charts [tab]Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 68–69why, one step back68This command does not take any more arguments69```Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 70why, one step back70Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 71–75why, one step back71**Hint**: As can be seen in the above example, a good place to use Active Help messages is when the normal completion system does not provide any suggestions. In such cases, Active Help nicely supplements the normal shell completions.7273### Active Help for flags7475Providing Active Help for flags is done in the same fashion as for nouns, but using the completion function registered for the flag. For example:Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 76why, one step back76Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
line 77why, one step back77```gofeat: add CompletionWithDesc helper (#2231)ccoVeillebefore adoption8cb30f9
line 78why, one step back78_ = cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 79–85why, one step back79 if len(args) != 2 {80 return cobra.AppendActiveHelp(nil, "You must first specify the chart to install before the --version flag can be completed"), cobra.ShellCompDirectiveNoFileComp81 }82 return compVersionFlag(args[1], toComplete)83 })84```85The example above prints an Active Help message when not enough information was given by the user to complete the `--version` flag.Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
lines 86–88why, one step back8687```console88$ bin/helm install myrelease --version 2.0.[tab]Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 89–90why, one step back89You must first specify the chart to install before the --version flag can be completed90Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 91why, one step back91$ bin/helm install myrelease bitnami/solr --version 2.0.[tab][tab]Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 92–101why, one step back922.0.1 2.0.2 2.0.393```9495## User control of Active Help9697You may want to allow your users to disable Active Help or choose between different levels of Active Help. It is entirely up to the program to define the type of configurability of Active Help that it wants to offer, if any.98Allowing to configure Active Help is entirely optional; you can use Active Help in your program without doing anything about Active Help configuration.99100The way to configure Active Help is to use the program's Active Help environment101variable. That variable is named `<PROGRAM>_ACTIVE_HELP` where `<PROGRAM>` is the name of your Replace all non-alphanumerics in active help env var program prefix (#1940)Ville Skyttäbefore adoption22953d8
line 102why, one step back102program in uppercase with any non-ASCII-alphanumeric characters replaced by an `_`. The variable should be set by the user to whateverMove documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 103–112why, one step back103Active Help configuration values are supported by the program.104105For example, say `helm` has chosen to support three levels for Active Help: `on`, `off`, `local`. Then a user106would set the desired behavior to `local` by doing `export HELM_ACTIVE_HELP=local` in their shell.107108For simplicity, when in `cmd.ValidArgsFunction(...)` or a flag's completion function, the program should read the109Active Help configuration using the `cobra.GetActiveHelpConfig(cmd)` function and select what Active Help messages110should or should not be added (instead of reading the environment variable directly).111112For example:Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 113why, one step back113Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
line 114why, one step back114```gofeat: add CompletionWithDesc helper (#2231)ccoVeillebefore adoption8cb30f9
line 115why, one step back115ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 116–117why, one step back116 activeHelpLevel := cobra.GetActiveHelpConfig(cmd)117feat: add CompletionWithDesc helper (#2231)ccoVeillebefore adoption8cb30f9
line 118why, one step back118 var comps []cobra.CompletionMove documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 119–134why, one step back119 if len(args) == 0 {120 if activeHelpLevel != "off" {121 comps = cobra.AppendActiveHelp(comps, "You must choose a name for the repo you are adding")122 }123 } else if len(args) == 1 {124 if activeHelpLevel != "off" {125 comps = cobra.AppendActiveHelp(comps, "You must specify the URL for the repo you are adding")126 }127 } else {128 if activeHelpLevel == "local" {129 comps = cobra.AppendActiveHelp(comps, "This command does not take any more arguments")130 }131 }132 return comps, cobra.ShellCompDirectiveNoFileComp133},134```Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 135why, one step back135Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 136–140why, one step back136**Note 1**: If the `<PROGRAM>_ACTIVE_HELP` environment variable is set to the string "0", Cobra will automatically disable all Active Help output (even if some output was specified by the program using the `cobra.AppendActiveHelp(...)` function). Using "0" can simplify your code in situations where you want to blindly disable Active Help without having to call `cobra.GetActiveHelpConfig(cmd)` explicitly.137138**Note 2**: If a user wants to disable Active Help for every single program based on Cobra, she can set the environment variable `COBRA_ACTIVE_HELP` to "0". In this case `cobra.GetActiveHelpConfig(cmd)` will return "0" no matter what the variable `<PROGRAM>_ACTIVE_HELP` is set to.139140**Note 3**: If the user does not set `<PROGRAM>_ACTIVE_HELP` or `COBRA_ACTIVE_HELP` (which will be a common case), the default value for the Active Help configuration returned by `cobra.GetActiveHelpConfig(cmd)` will be the empty string. Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
line 141why, one step back141Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 142–150why, one step back142## Active Help with Cobra's default completion command143144Cobra provides a default `completion` command for programs that wish to use it.145When using the default `completion` command, Active Help is configurable in the same146fashion as described above using environment variables. You may wish to document this in more147details for your users.148149## Debugging Active Help150Fix broken links in active_help.md (#2202)Vui Lambefore adoption02326d5
line 151why, one step back151Debugging your Active Help code is done in the same way as debugging your dynamic completion code, which is with Cobra's hidden `__complete` command. Please refer to [debugging shell completion](completions/_index.md#debugging) for details.Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
line 152why, one step back152Replace all non-alphanumerics in active help env var program prefix (#1940)Ville Skyttäbefore adoption22953d8
line 153why, one step back153When debugging with the `__complete` command, if you want to specify different Active Help configurations, you should use the active help environment variable. That variable is named `<PROGRAM>_ACTIVE_HELP` where any non-ASCII-alphanumeric characters are replaced by an `_`. For example, we can test deactivating some Active Help as shown below:Improve site formatting (#2183)Nir Sofferbefore adoptionff7c561
lines 154–155why, one step back154155```consoleMove documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a
lines 156–168why, one step back156$ HELM_ACTIVE_HELP=1 bin/helm __complete install wordpress bitnami/h<ENTER>157bitnami/haproxy158bitnami/harbor159_activeHelp_ WARNING: cannot re-use a name that is still in use160:0161Completion ended with directive: ShellCompDirectiveDefault162163$ HELM_ACTIVE_HELP=0 bin/helm __complete install wordpress bitnami/h<ENTER>164bitnami/haproxy165bitnami/harbor166:0167Completion ended with directive: ShellCompDirectiveDefault168```