Skip to content
Myr

README.md Readability Improvements (#1228)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

I made some small changes to the README.md file to enhance its readability.

authored by zaataylor · committed by GitHub · parent 08c51e5 · Browse files · 1 file+10 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+10 added−10 removed

  • other:+10 added−10 removed

1 file+10 added−10 removed

modified:README.md+10 added-10 removed
60 unmodified lines
61Cobra is built on a structure of commands, arguments & flags.6263**Commands** represent actions, **Args** are things and **Flags** are modifiers for those actions.6465The best applications will read like sentences when used. Users will know how66to use the application because they will natively understand how to use it.65The best applications read like sentences when used, and as a result, users66intuitively know how to interact with them.6768The pattern to follow is69`APPNAME VERB NOUN --ADJECTIVE.`70 or
196 unmodified lines
267268With the root command you need to have your main function execute it.269Execute should be run on the root for clarity, though it can be called on any command.270271In a Cobra app, typically the main.go file is very bare. It serves, one purpose, to initialize Cobra.271In a Cobra app, typically the main.go file is very bare. It serves one purpose: to initialize Cobra.272273```go274package main275
86 unmodified lines
362There are two different approaches to assign a flag.363364### Persistent Flags365366A flag can be 'persistent' meaning that this flag will be available to the366A flag can be 'persistent', meaning that this flag will be available to the367command it's assigned to as well as every command under that command. For368global flags, assign a flag as a persistent flag on the root.369370```go
1 unmodified line
372```373374### Local Flags375376A flag can also be assigned locally which will only apply to that specific command.376A flag can also be assigned locally, which will only apply to that specific command.377378```go379localCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")380```381382### Local Flag on Parent Commands383384By default Cobra only parses local flags on the target command, any local flags on385parent commands are ignored. By enabling `Command.TraverseChildren` Cobra will384By default, Cobra only parses local flags on the target command, and any local flags on385parent commands are ignored. By enabling `Command.TraverseChildren`, Cobra will386parse local flags on each command before executing the target command.387388```go389command := cobra.Command{
13 unmodified lines
403 viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))404}405```406407In this example the persistent flag `author` is bound with `viper`.408**Note**, that the variable `author` will not be set to the value from config,407In this example, the persistent flag `author` is bound with `viper`.408**Note**: the variable `author` will not be set to the value from config,409when the `--author` flag is not provided by user.410411More in [viper documentation](https://github.com/spf13/viper#working-with-flags).412
51 unmodified lines
464## Example465466In the example below, we have defined three commands. Two are at the top level467and one (cmdTimes) is a child of one of the top commands. In this case the root468is not executable meaning that a subcommand is required. This is accomplished468is not executable, meaning that a subcommand is required. This is accomplished469by not providing a 'Run' for the 'rootCmd'.470471We have only defined one flag for a single command.472
294 unmodified lines