Improve site formatting (#2183)

* 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 is contained in:
Nir Soffer 2024-10-12 18:35:01 +03:00 committed by GitHub
parent 11ab62158a
commit ff7c561cf7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 76 additions and 66 deletions

View File

@ -3,18 +3,20 @@
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. 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, For example,
```
bash-5.1$ helm repo add [tab] ```console
$ helm repo add [tab]
You must choose a name for the repo you are adding. 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 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/ 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. **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 ## Supported shells
Active Help is currently only supported for the following shells: Active Help is currently only supported for the following shells:
@ -52,22 +54,26 @@ cmd := &cobra.Command{
}, },
} }
``` ```
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: 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 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 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 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. **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 ### 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: 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 ```go
_ = cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { _ = cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 2 { if len(args) != 2 {
@ -77,11 +83,12 @@ _ = cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []st
}) })
``` ```
The example above prints an Active Help message when not enough information was given by the user to complete the `--version` flag. 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 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 2.0.1 2.0.2 2.0.3
``` ```
@ -103,6 +110,7 @@ Active Help configuration using the `cobra.GetActiveHelpConfig(cmd)` function an
should or should not be added (instead of reading the environment variable directly). should or should not be added (instead of reading the environment variable directly).
For example: For example:
```go ```go
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
activeHelpLevel := cobra.GetActiveHelpConfig(cmd) activeHelpLevel := cobra.GetActiveHelpConfig(cmd)
@ -124,11 +132,13 @@ ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([
return comps, cobra.ShellCompDirectiveNoFileComp 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 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 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. **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 ## Active Help with Cobra's default completion command
Cobra provides a default `completion` command for programs that wish to use it. Cobra provides a default `completion` command for programs that wish to use it.
@ -141,7 +151,8 @@ details for your users.
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. 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: 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> $ HELM_ACTIVE_HELP=1 bin/helm __complete install wordpress bitnami/h<ENTER>
bitnami/haproxy bitnami/haproxy
bitnami/harbor bitnami/harbor

View File

@ -3,7 +3,7 @@
While you are welcome to provide your own organization, typically a Cobra-based While you are welcome to provide your own organization, typically a Cobra-based
application will follow the following organizational structure: application will follow the following organizational structure:
```test ```console
▾ appName/ ▾ appName/
▾ cmd/ ▾ cmd/
add.go add.go
@ -18,9 +18,7 @@ In a Cobra app, typically the main.go file is very bare. It serves one purpose:
```go ```go
package main package main
import ( import "{pathToYourApp}/cmd"
"{pathToYourApp}/cmd"
)
func main() { func main() {
cmd.Execute() cmd.Execute()
@ -148,9 +146,7 @@ In a Cobra app, typically the main.go file is very bare. It serves one purpose:
```go ```go
package main package main
import ( import "{pathToYourApp}/cmd"
"{pathToYourApp}/cmd"
)
func main() { func main() {
cmd.Execute() cmd.Execute()
@ -197,7 +193,7 @@ its own go package.
The suggested approach is for the parent command to use `AddCommand` to add its most immediate The suggested approach is for the parent command to use `AddCommand` to add its most immediate
subcommands. For example, consider the following directory structure: subcommands. For example, consider the following directory structure:
```text ```console
├── cmd ├── cmd
│   ├── root.go │   ├── root.go
│   └── sub1 │   └── sub1
@ -508,6 +504,7 @@ create' is called. Every command will automatically have the '--help' flag adde
The following output is automatically generated by Cobra. Nothing beyond the The following output is automatically generated by Cobra. Nothing beyond the
command and flag definitions are needed. command and flag definitions are needed.
```console
$ cobra-cli help $ cobra-cli help
Cobra is a CLI library for Go that empowers applications. Cobra is a CLI library for Go that empowers applications.
@ -531,7 +528,7 @@ command and flag definitions are needed.
--viper use Viper for configuration --viper use Viper for configuration
Use "cobra-cli [command] --help" for more information about a command. Use "cobra-cli [command] --help" for more information about a command.
```
Help is just a command like any other. There is no special logic or behavior Help is just a command like any other. There is no special logic or behavior
around it. In fact, you can provide your own if you want. around it. In fact, you can provide your own if you want.
@ -566,6 +563,7 @@ showing the user the 'usage'.
You may recognize this from the help above. That's because the default help You may recognize this from the help above. That's because the default help
embeds the usage as part of its output. embeds the usage as part of its output.
```console
$ cobra-cli --invalid $ cobra-cli --invalid
Error: unknown flag: --invalid Error: unknown flag: --invalid
Usage: Usage:
@ -585,6 +583,7 @@ embeds the usage as part of its output.
--viper use Viper for configuration --viper use Viper for configuration
Use "cobra [command] --help" for more information about a command. Use "cobra [command] --help" for more information about a command.
```
### Defining your own usage ### Defining your own usage
You can provide your own usage function or template for Cobra to use. You can provide your own usage function or template for Cobra to use.
@ -701,7 +700,7 @@ Set `EnableTraverseRunHooks` global variable to `true` if you want to execute al
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: 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:
``` ```console
$ hugo srever $ hugo srever
Error: unknown command "srever" for "hugo" Error: unknown command "srever" for "hugo"
@ -728,7 +727,7 @@ command.SuggestionsMinimumDistance = 1
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 which 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 which
you don't want aliases. Example: you don't want aliases. Example:
```bash ```console
$ kubectl remove $ kubectl remove
Error: unknown command "remove" for "kubectl" Error: unknown command "remove" for "kubectl"
@ -793,7 +792,7 @@ func main() {
Example run as a kubectl plugin: Example run as a kubectl plugin:
```bash ```console
$ kubectl myplugin $ kubectl myplugin
Usage: Usage:
kubectl myplugin [command] kubectl myplugin [command]