Skip to content
Myr

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.

authored by Nir Soffer · committed by GitHub · parent 11ab621 · Browse files · 2 files+76 added−66 removed
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

2 files+76 added−66 removed

modified:site/content/active_help.md+24 added-13 removed
modified:site/content/user_guide.md+52 added-53 removed
1 unmodified line
23While you are welcome to provide your own organization, typically a Cobra-based4application will follow the following organizational structure:56```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```1516In a Cobra app, typically the main.go file is very bare. It serves one purpose: initializing Cobra.1718```go19package main2021import (22 "{pathToYourApp}/cmd"21import "{pathToYourApp}/cmd"23)2223func main() {24 cmd.Execute()25}
119 unmodified lines
145146```go147package main148151import (152 "{pathToYourApp}/cmd"149import "{pathToYourApp}/cmd"153)150151func main() {152 cmd.Execute()153}
38 unmodified lines
192193The suggested approach is for the parent command to use `AddCommand` to add its most immediate194subcommands. For example, consider the following directory structure:195200```text196```console197├── cmd198│   ├── root.go199│   └── sub1200│   ├── sub1.go
302 unmodified lines
503504The following output is automatically generated by Cobra. Nothing beyond the505command and flag definitions are needed.506507```console511 $ cobra-cli help508$ cobra-cli help509513 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.510Cobra is a CLI library for Go that empowers applications.511This application is a tool to generate the needed files512to quickly create a Cobra application.513517 Usage:518 cobra-cli [command]514Usage:515 cobra-cli [command]516520 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 Application517Available 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 Application522526 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 configuration523Flags: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 configuration529533 Use "cobra-cli [command] --help" for more information about a command.530Use "cobra-cli [command] --help" for more information about a command.531```532535533Help is just a command like any other. There is no special logic or behavior534around it. In fact, you can provide your own if you want.535536### Grouping commands in help
25 unmodified lines
562### Example563You may recognize this from the help above. That's because the default help564embeds the usage as part of its output.565566```console569 $ cobra-cli --invalid570 Error: unknown flag: --invalid571 Usage:572 cobra-cli [command]567$ cobra-cli --invalid568Error: unknown flag: --invalid569Usage:570 cobra-cli [command]571574 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 Application572Available 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 Application577580 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 configuration578Flags: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 configuration584587 Use "cobra [command] --help" for more information about a command.585Use "cobra [command] --help" for more information about a command.586```587588### Defining your own usage589You can provide your own usage function or template for Cobra to use.590Like help, the function and template are overridable through public methods:
108 unmodified lines
699## Suggestions when "unknown command" happens700701Cobra 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:702704```703```console704$ hugo srever705Error: unknown command "srever" for "hugo"706707Did you mean this?
18 unmodified lines
726727You 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 which728you don't want aliases. Example:729731```bash730```console731$ kubectl remove732Error: unknown command "remove" for "kubectl"733734Did you mean this?
56 unmodified lines
791```792793Example run as a kubectl plugin:794796```bash795```console796$ kubectl myplugin797Usage:798 kubectl myplugin [command]799
10 unmodified lines