Improve site formatting (#2183)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Improve site formatting - Separate titles with blank lines - Separate code blocks with blank lines - Always use ``` blocks for examples - Use console for console (bash syntax highlighting does work well with example command output) - Start console examples with $ (highlight command and output differently and more friendly to other shells users) * Unify indentation in example project structure * Use single import line in the trivial examples When we want to show a minimal example with single import it looks cleaner and more minimal with a single line.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+76 added−66 removed
- other:+76 added−66 removed
Provenance
- Commit
ff7c561- Tree
5fb1d5c- Author
Nir Soffer <nsoffer@redhat.com>- Committer
GitHub <noreply@github.com>- Parent
11ab621
2 files changed+76 added−66 removed
modified:site/content/active_help.md+24 added-13 removed
1234567567891010111213131415161718192021222329 unmodified lines535455565758565759606162636064656663676869707172737475767778798081828384858680818788899084919293949513 unmodified lines10911011111211311411511611713 unmodified lines1311321331341351361371381391401411421431441454 unmodified lines1501511521531441541551561571581599 unmodified lines# Active Help
Active 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.
For example, ```bash-5.1$ helm repo add [tab]For example,
```console$ helm repo add [tab]You must choose a name for the repo you are adding.
bash-5.1$ bin/helm package [tab]$ bin/helm package [tab]Please specify the path to the chart to package
bash-5.1$ bin/helm package [tab][tab]$ bin/helm package [tab][tab]bin/ internal/ scripts/ pkg/ testdata/```
**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.
## Supported shells
Active Help is currently only supported for the following shells:- Bash (using [bash completion V2](shell_completions.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.29 unmodified lines return comps, cobra.ShellCompDirectiveNoFileComp },}```
The 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:```bash-5.1$ helm repo add [tab]
```console$ helm repo add [tab]You must choose a name for the repo you are adding
bash-5.1$ helm repo add grafana [tab]$ helm repo add grafana [tab]You must specify the URL for the repo you are adding
bash-5.1$ helm repo add grafana https://grafana.github.io/helm-charts [tab]$ helm repo add grafana https://grafana.github.io/helm-charts [tab]This command does not take any more arguments```
**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.
### Active Help for flags
Providing Active Help for flags is done in the same fashion as for nouns, but using the completion function registered for the flag. For example:
```go_ = cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) != 2 { return cobra.AppendActiveHelp(nil, "You must first specify the chart to install before the --version flag can be completed"), cobra.ShellCompDirectiveNoFileComp } return compVersionFlag(args[1], toComplete) })```The example above prints an Active Help message when not enough information was given by the user to complete the `--version` flag.
```bash-5.1$ bin/helm install myrelease --version 2.0.[tab]```console$ bin/helm install myrelease --version 2.0.[tab]You must first specify the chart to install before the --version flag can be completed
bash-5.1$ bin/helm install myrelease bitnami/solr --version 2.0.[tab][tab]$ bin/helm install myrelease bitnami/solr --version 2.0.[tab][tab]2.0.1 2.0.2 2.0.3```
## User control of Active Help13 unmodified linesActive Help configuration using the `cobra.GetActiveHelpConfig(cmd)` function and select what Active Help messagesshould or should not be added (instead of reading the environment variable directly).
For example:
```goValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { activeHelpLevel := cobra.GetActiveHelpConfig(cmd)
13 unmodified lines } return comps, cobra.ShellCompDirectiveNoFileComp},```
**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.
**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.
**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.
## Active Help with Cobra's default completion command
Cobra provides a default `completion` command for programs that wish to use it.When using the default `completion` command, Active Help is configurable in the same4 unmodified lines
Debugging 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](shell_completions.md#debugging) for details.
When 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:```
```console$ HELM_ACTIVE_HELP=1 bin/helm __complete install wordpress bitnami/h<ENTER>bitnami/haproxybitnami/harbor_activeHelp_ WARNING: cannot re-use a name that is still in use9 unmodified linesmodified:site/content/user_guide.md+52 added-53 removed
1 unmodified line2 3 While you are welcome to provide your own organization, typically a Cobra-based4 application will follow the following organizational structure:5 6-```test7- ▾ appName/8- ▾ cmd/9- add.go10- your.go11- commands.go12- here.go13- main.go6+```console7+▾ appName/8+ ▾ cmd/9+ add.go10+ your.go11+ commands.go12+ here.go13+ main.go14 ```15 16 In a Cobra app, typically the main.go file is very bare. It serves one purpose: initializing Cobra.17 18 ```go19 package main20 21-import (22- "{pathToYourApp}/cmd"21+import "{pathToYourApp}/cmd"23-)22 23 func main() {24 cmd.Execute()25 }119 unmodified lines145 146 ```go147 package main148 151-import (152- "{pathToYourApp}/cmd"149+import "{pathToYourApp}/cmd"153-)150 151 func main() {152 cmd.Execute()153 }38 unmodified lines192 193 The suggested approach is for the parent command to use `AddCommand` to add its most immediate194 subcommands. For example, consider the following directory structure:195 200-```text196+```console197 ├── cmd198 │ ├── root.go199 │ └── sub1200 │ ├── sub1.go302 unmodified lines503 504 The following output is automatically generated by Cobra. Nothing beyond the505 command and flag definitions are needed.506 507+```console511- $ cobra-cli help508+$ cobra-cli help509 513- Cobra is a CLI library for Go that empowers applications.514- This application is a tool to generate the needed files515- to quickly create a Cobra application.510+Cobra is a CLI library for Go that empowers applications.511+This application is a tool to generate the needed files512+to quickly create a Cobra application.513 517- Usage:518- cobra-cli [command]514+Usage:515+ cobra-cli [command]516 520- Available Commands:521- add Add a command to a Cobra Application522- completion Generate the autocompletion script for the specified shell523- help Help about any command524- init Initialize a Cobra Application517+Available Commands:518+ add Add a command to a Cobra Application519+ completion Generate the autocompletion script for the specified shell520+ help Help about any command521+ init Initialize a Cobra Application522 526- Flags:527- -a, --author string author name for copyright attribution (default "YOUR NAME")528- --config string config file (default is $HOME/.cobra.yaml)529- -h, --help help for cobra-cli530- -l, --license string name of license for the project531- --viper use Viper for configuration523+Flags:524+ -a, --author string author name for copyright attribution (default "YOUR NAME")525+ --config string config file (default is $HOME/.cobra.yaml)526+ -h, --help help for cobra-cli527+ -l, --license string name of license for the project528+ --viper use Viper for configuration529 533- Use "cobra-cli [command] --help" for more information about a command.530+Use "cobra-cli [command] --help" for more information about a command.531+```532 535-533 Help is just a command like any other. There is no special logic or behavior534 around it. In fact, you can provide your own if you want.535 536 ### Grouping commands in help25 unmodified lines562 ### Example563 You may recognize this from the help above. That's because the default help564 embeds the usage as part of its output.565 566+```console569- $ cobra-cli --invalid570- Error: unknown flag: --invalid571- Usage:572- cobra-cli [command]567+$ cobra-cli --invalid568+Error: unknown flag: --invalid569+Usage:570+ cobra-cli [command]571 574- Available Commands:575- add Add a command to a Cobra Application576- completion Generate the autocompletion script for the specified shell577- help Help about any command578- init Initialize a Cobra Application572+Available Commands:573+ add Add a command to a Cobra Application574+ completion Generate the autocompletion script for the specified shell575+ help Help about any command576+ init Initialize a Cobra Application577 580- Flags:581- -a, --author string author name for copyright attribution (default "YOUR NAME")582- --config string config file (default is $HOME/.cobra.yaml)583- -h, --help help for cobra-cli584- -l, --license string name of license for the project585- --viper use Viper for configuration578+Flags:579+ -a, --author string author name for copyright attribution (default "YOUR NAME")580+ --config string config file (default is $HOME/.cobra.yaml)581+ -h, --help help for cobra-cli582+ -l, --license string name of license for the project583+ --viper use Viper for configuration584 587- Use "cobra [command] --help" for more information about a command.585+Use "cobra [command] --help" for more information about a command.586+```587 588 ### Defining your own usage589 You can provide your own usage function or template for Cobra to use.590 Like help, the function and template are overridable through public methods:108 unmodified lines699 ## Suggestions when "unknown command" happens700 701 Cobra will print automatic suggestions when "unknown command" errors happen. This allows Cobra to behave similarly to the `git` command when a typo happens. For example:702 704-```703+```console704 $ hugo srever705 Error: unknown command "srever" for "hugo"706 707 Did you mean this?18 unmodified lines726 727 You can also explicitly set names for which a given command will be suggested using the `SuggestFor` attribute. This allows suggestions for strings that are not close in terms of string distance, but make sense in your set of commands but for which728 you don't want aliases. Example:729 731-```bash730+```console731 $ kubectl remove732 Error: unknown command "remove" for "kubectl"733 734 Did you mean this?56 unmodified lines791 ```792 793 Example run as a kubectl plugin:794 796-```bash795+```console796 $ kubectl myplugin797 Usage:798 kubectl myplugin [command]799 10 unmodified lines