Copyedit README.md
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Anthony Fok · committed by Anthony Fok · parent f06ae19 · Browse files · 1 file·+198 added−120 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+198 added−120 removed
- other:+198 added−120 removed
Provenance
- Commit
9324f36- Tree
0bf1b22- Author
Anthony Fok <foka@debian.org>- Committer
Anthony Fok <foka@debian.org>- Parent
f06ae19
1 file changed+198 added−120 removed
modified:README.md+198 added-120 removed
15 unmodified lines16 * [ProjectAtomic (enterprise)](http://www.projectatomic.io/)17 * [Parse (CLI)](https://parse.com/)18 19 20-[](https://travis-ci.org/spf13/cobra)20+[](https://travis-ci.org/spf13/cobra) 21+[](https://circleci.com/gh/spf13/cobra)22 23 24 25 ## Overview1 unmodified line27 Cobra is a library providing a simple interface to create powerful modern CLI28 interfaces similar to git & go tools.29 30 Cobra is also an application that will generate your application scaffolding to rapidly30-develop a Cobra based application.31+develop a Cobra-based application.32 33 Cobra provides:33-* Easy sub-command based CLIs: `app server`, `app fetch`, etc.34-* Fully posix compliant flags (including short & long versions)35-* Nested sub commands34+* Easy subcommand-based CLIs: `app server`, `app fetch`, etc.35+* Fully POSIX-compliant flags (including short & long versions)36+* Nested subcommands37 * Global, local and cascading flags38 * Easy generation of applications & commands with `cobra create appname` & `cobra add cmdname`38-* Intelligent suggestions (`app srver`.. did you mean `app server`)39+* Intelligent suggestions (`app srver`... did you mean `app server`?)40 * Automatic help generation for commands and flags41 * Automatic detailed help for `app help [command]`42 * Automatic help flag recognition of `-h`, `--help`, etc.43 * Automatically generated bash autocomplete for your application44 * Automatically generated man pages for your application45 * Command aliases so you can change things without breaking them45-* The flexibilty to define your own help, usage, etc46-* Optional tight integration with [viper](http://github.com/spf13/viper) for 12 factor apps46+* The flexibilty to define your own help, usage, etc.47+* Optional tight integration with [viper](http://github.com/spf13/viper) for 12-factor apps48 49 Cobra has an exceptionally clean interface and simple design without needless50 constructors or initialization methods.51 51-Applications built with Cobra commands are designed to be as user friendly as52+Applications built with Cobra commands are designed to be as user-friendly as53 possible. Flags can be placed before or after the command (as long as a54 confusing space isn’t provided). Both short and long flags can be used. A55 command need not even be fully typed. The shortest unambiguous string will56 suffice. Help is automatically generated and available for the application or56-for a specific command using either the help command or the --help flag.57+for a specific command using either the help command or the `--help` flag.58 59 ## Concepts60 61 Cobra is built on a structure of commands & flags.62 63 **Commands** represent actions and **Flags** are modifiers for those actions.64 64-In the following example 'server' is a command and 'port' is a flag.65+In the following example, 'server' is a command, and 'port' is a flag:66 66- hugo server --port=131367+ > hugo server --port=131368 69 ### Commands70 71 Command is the central point of the application. Each interaction that72 the application supports will be contained in a Command. A command can73 have children commands and optionally run an action.74 74-In the example above 'server' is the command75+In the example above, 'server' is the command.76 77 A Command has the following structure:78 79+```go78- type Command struct {79- Use string // The one-line usage message.80- Short string // The short description shown in the 'help' output.81- Long string // The long message shown in the 'help <this-command>' output.82- Run func(cmd *Command, args []string) // Run runs the command.83- }80+type Command struct {81+ Use string // The one-line usage message.82+ Short string // The short description shown in the 'help' output.83+ Long string // The long message shown in the 'help <this-command>' output.84+ Run func(cmd *Command, args []string) // Run runs the command.85+}86+```87 88 ### Flags89 87-A Flag is a way to modify the behavior of an command. Cobra supports88-fully posix compliant flags as well as the go flag package.90+A Flag is a way to modify the behavior of a command. Cobra supports91+fully POSIX-compliant flags as well as the Go [flag package](https://golang.org/pkg/flag/).92 A Cobra command can define flags that persist through to children commands93 and flags that are only available to that command.94 92-In the example above 'port' is the flag.95+In the example above, 'port' is the flag.96 97 Flag functionality is provided by the [pflag98 library](https://github.com/ogier/pflag), a fork of the flag standard library96-which maintains the same interface while adding posix compliance.99+which maintains the same interface while adding POSIX compliance.100 101 ## Usage102 103 Cobra works by creating a set of commands and then organizing them into a tree.104 The tree defines the structure of the application.105 103-Once each command is defined with it's corresponding flags, then the106+Once each command is defined with its corresponding flags, then the107 tree is assigned to the commander which is finally executed.108 109 ### Installing107-Using Cobra is easy. First use go get to install the latest version108-of the library.110+Using Cobra is easy. First, use `go get` to install the latest version111+of the library:112 110- $ go get github.com/spf13/cobra113+ > go get -v github.com/spf13/cobra114 112-Next include cobra in your application.115+Next, include Cobra in your application:116 117+```go114- import "github.com/spf13/cobra"118+import "github.com/spf13/cobra"119+```120 121 ### Create the root command122 123 The root command represents your binary itself.30 unmodified lines154 155 ### Attach command to its parent156 In this example we are attaching it to the root, but commands can be attached at any level.157 158+```go153- HugoCmd.AddCommand(versionCmd)159+HugoCmd.AddCommand(versionCmd)160+```161 162 ### Assign flags to a command163 164 Since the flags are defined and used in different locations, we need to165 define a variable outside with the correct scope to assign the flag to166 work with.167 168+```go161- var Verbose bool162- var Source string169+var Verbose bool170+var Source string171+```172 173 There are two different approaches to assign a flag.174 175 #### Persistent Flags176 177 A flag can be 'persistent' meaning that this flag will be available to the178 command it's assigned to as well as every command under that command. For170-global flags assign a flag as a persistent flag on the root.179+global flags, assign a flag as a persistent flag on the root.180 181+```go172- HugoCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")182+HugoCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")183+```184 185 #### Local Flags186 187 A flag can also be assigned locally which will only apply to that specific command.188 189+```go178- HugoCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")190+HugoCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")191+```192 193 ### Remove a command from its parent194 182-Removing a command is not a common action in simple programs but it allows 3rd parties to customize an existing command tree.195+Removing a command is not a common action in simple programs, but it allows 3rd parties to customize an existing command tree.196 184-In this example, we remove the existing `VersionCmd` command of an existing root command, and we replace it by our own version.197+In this example, we remove the existing `VersionCmd` command of an existing root command, and we replace it with our own version:198 199+```go186- mainlib.RootCmd.RemoveCommand(mainlib.VersionCmd)187- mainlib.RootCmd.AddCommand(versionCmd)200+mainlib.RootCmd.RemoveCommand(mainlib.VersionCmd)201+mainlib.RootCmd.AddCommand(versionCmd)202+```203 204 ### Once all commands and flags are defined, Execute the commands205 206 Execute should be run on the root for clarity, though it can be called on any command.207 208+```go193- HugoCmd.Execute()209+HugoCmd.Execute()210+```211 212 ## Example213 197-In the example below we have defined three commands. Two are at the top level214+In the example below, we have defined three commands. Two are at the top level215 and one (cmdTimes) is a child of one of the top commands. In this case the root216 is not executable meaning that a subcommand is required. This is accomplished217 by not providing a 'Run' for the 'rootCmd'.218 219 We have only defined one flag for a single command.220 221 More documentation about flags is available at https://github.com/spf13/pflag222+223 ```go224 package main225 226 import (48 unmodified lines275 cmdEcho.AddCommand(cmdTimes)276 rootCmd.Execute()277 }278 ```261-For a more complete example of a larger application, please checkout [Hugo](http://hugo.spf13.com)279 280+For a more complete example of a larger application, please checkout [Hugo](http://gohugo.io/).281+282 ## The Help Command283 284 Cobra automatically adds a help command to your application when you have subcommands.266-This will be called when a user runs 'app help'. Additionally help will also267-support all other commands as input. Say for instance you have a command called268-'create' without any additional configuration cobra will work when 'app help285+This will be called when a user runs 'app help'. Additionally, help will also286+support all other commands as input. Say, for instance, you have a command called287+'create' without any additional configuration; Cobra will work when 'app help288 create' is called. Every command will automatically have the '--help' flag added.289 290 ### Example291 273-The following output is automatically generated by cobra. Nothing beyond the292+The following output is automatically generated by Cobra. Nothing beyond the293 command and flag definitions are needed.294 295 > hugo help277-278- A Fast and Flexible Static Site Generator built with279- love by spf13 and friends in Go.280-281- Complete documentation is available at http://hugo.spf13.com282-296+ 297+ hugo is the main command, used to build your Hugo site.298+ 299+ Hugo is a Fast and Flexible Static Site Generator300+ built with love by spf13 and friends in Go.301+ 302+ Complete documentation is available at http://gohugo.io/.303+ 304 Usage:305 hugo [flags]306 hugo [command]286-307+ 308 Available Commands:288- server :: Hugo runs it's own a webserver to render the files289- version :: Print the version number of Hugo290- check :: Check content in the source directory291- benchmark :: Benchmark hugo by building a site a number of times292- help [command] :: Help about any command293-294- Available Flags:295- -b, --base-url="": hostname (and path) to the root eg. http://spf13.com/296- -D, --build-drafts=false: include content marked as draft309+ server Hugo runs its own webserver to render the files310+ version Print the version number of Hugo311+ config Print the site configuration312+ check Check content in the source directory313+ benchmark Benchmark hugo by building a site a number of times.314+ convert Convert your content to different formats315+ new Create new content for your site316+ list Listing out various types of content317+ undraft Undraft changes the content's draft status from 'True' to 'False'318+ genautocomplete Generate shell autocompletion script for Hugo319+ gendoc Generate Markdown documentation for the Hugo CLI.320+ genman Generate man page for Hugo321+ import Import your site from others.322+ 323+ Flags:324+ -b, --baseURL="": hostname (and path) to the root, e.g. http://spf13.com/325+ -D, --buildDrafts[=false]: include content marked as draft326+ -F, --buildFuture[=false]: include content with publishdate in the future327+ --cacheDir="": filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/328+ --canonifyURLs[=false]: if true, all relative URLs will be canonicalized using baseURL329 --config="": config file (default is path/config.yaml|json|toml)330 -d, --destination="": filesystem path to write files to331+ --disableRSS[=false]: Do not build RSS files332+ --disableSitemap[=false]: Do not build Sitemap file333+ --editor="": edit new content with this editor, if provided334+ --ignoreCache[=false]: Ignores the cache directory for reading but still writes to it335+ --log[=false]: Enable Logging336+ --logFile="": Log File path (if set, logging enabled automatically)337+ --noTimes[=false]: Don't sync modification time of files338+ --pluralizeListTitles[=true]: Pluralize titles in lists using inflect339+ --preserveTaxonomyNames[=false]: Preserve taxonomy names as written ("Gérard Depardieu" vs "gerard-depardieu")340 -s, --source="": filesystem path to read files relative from300- --stepAnalysis=false: display memory and timing of different steps of the program301- --uglyurls=false: if true, use /filename.html instead of /filename/302- -v, --verbose=false: verbose output303- -w, --watch=false: watch filesystem for changes and recreate as needed341+ --stepAnalysis[=false]: display memory and timing of different steps of the program342+ -t, --theme="": theme to use (located in /themes/THEMENAME/)343+ --uglyURLs[=false]: if true, use /filename.html instead of /filename/344+ -v, --verbose[=false]: verbose output345+ --verboseLog[=false]: verbose logging346+ -w, --watch[=false]: watch filesystem for changes and recreate as needed347+ 348+ Use "hugo [command] --help" for more information about a command.349 305- Use "hugo help [command]" for more information about that command.350 307-308-351 Help is just a command like any other. There is no special logic or behavior310-around it. In fact you can provide your own if you want.352+around it. In fact, you can provide your own if you want.353 354 ### Defining your own help355 356 You can provide your own Help command or you own template for the default command to use.14 unmodified lines371 c.AddCommand(c.helpCommand)372 }373 ```374 333-You can provide your own command, function or template through the following methods.375+You can provide your own command, function or template through the following methods:376 377+```go335- command.SetHelpCommand(cmd *Command)378+command.SetHelpCommand(cmd *Command)379 337- command.SetHelpFunc(f func(*Command, []string))380+command.SetHelpFunc(f func(*Command, []string))381 339- command.SetHelpTemplate(s string)382+command.SetHelpTemplate(s string)383+```384 385 The latter two will also apply to any children commands.386 387 ## Usage388 345-When the user provides an invalid flag or invalid command Cobra responds by346-showing the user the 'usage'389+When the user provides an invalid flag or invalid command, Cobra responds by390+showing the user the 'usage'.391 392 ### Example393 You may recognize this from the help above. That's because the default help350-embeds the usage as part of it's output.394+embeds the usage as part of its output.395 396 Usage:397 hugo [flags]398 hugo [command]355-399+ 400 Available Commands:357- server Hugo runs it's own a webserver to render the files401+ server Hugo runs its own webserver to render the files402 version Print the version number of Hugo403+ config Print the site configuration404 check Check content in the source directory360- benchmark Benchmark hugo by building a site a number of times361- help [command] Help about any command362-363- Available Flags:364- -b, --base-url="": hostname (and path) to the root eg. http://spf13.com/365- -D, --build-drafts=false: include content marked as draft405+ benchmark Benchmark hugo by building a site a number of times.406+ convert Convert your content to different formats407+ new Create new content for your site408+ list Listing out various types of content409+ undraft Undraft changes the content's draft status from 'True' to 'False'410+ genautocomplete Generate shell autocompletion script for Hugo411+ gendoc Generate Markdown documentation for the Hugo CLI.412+ genman Generate man page for Hugo413+ import Import your site from others.414+ 415+ Flags:416+ -b, --baseURL="": hostname (and path) to the root, e.g. http://spf13.com/417+ -D, --buildDrafts[=false]: include content marked as draft418+ -F, --buildFuture[=false]: include content with publishdate in the future419+ --cacheDir="": filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/420+ --canonifyURLs[=false]: if true, all relative URLs will be canonicalized using baseURL421 --config="": config file (default is path/config.yaml|json|toml)422 -d, --destination="": filesystem path to write files to423+ --disableRSS[=false]: Do not build RSS files424+ --disableSitemap[=false]: Do not build Sitemap file425+ --editor="": edit new content with this editor, if provided426+ --ignoreCache[=false]: Ignores the cache directory for reading but still writes to it427+ --log[=false]: Enable Logging428+ --logFile="": Log File path (if set, logging enabled automatically)429+ --noTimes[=false]: Don't sync modification time of files430+ --pluralizeListTitles[=true]: Pluralize titles in lists using inflect431+ --preserveTaxonomyNames[=false]: Preserve taxonomy names as written ("Gérard Depardieu" vs "gerard-depardieu")432 -s, --source="": filesystem path to read files relative from369- --stepAnalysis=false: display memory and timing of different steps of the program370- --uglyurls=false: if true, use /filename.html instead of /filename/371- -v, --verbose=false: verbose output372- -w, --watch=false: watch filesystem for changes and recreate as needed433+ --stepAnalysis[=false]: display memory and timing of different steps of the program434+ -t, --theme="": theme to use (located in /themes/THEMENAME/)435+ --uglyURLs[=false]: if true, use /filename.html instead of /filename/436+ -v, --verbose[=false]: verbose output437+ --verboseLog[=false]: verbose logging438+ -w, --watch[=false]: watch filesystem for changes and recreate as needed439 440 ### Defining your own usage375-You can provide your own usage function or template for cobra to use.441+You can provide your own usage function or template for Cobra to use.442 377-The default usage function is443+The default usage function is:444 445+```go379- return func(c *Command) error {380- err := tmpl(c.Out(), c.UsageTemplate(), c)381- return err382- }446+return func(c *Command) error {447+ err := tmpl(c.Out(), c.UsageTemplate(), c)448+ return err449+}450+```451 384-Like help the function and template are over ridable through public methods.452+Like help, the function and template are overridable through public methods:453 454+```go386- command.SetUsageFunc(f func(*Command) error)455+command.SetUsageFunc(f func(*Command) error)456 388- command.SetUsageTemplate(s string)457+command.SetUsageTemplate(s string)458+```459 460 ## PreRun or PostRun Hooks461 462 It is possible to run functions before or after the main `Run` function of your command. The `PersistentPreRun` and `PreRun` functions will be executed before `Run`. `PersistentPostRun` and `PostRun` will be executed after `Run`. The `Persistent*Run` functions will be inherrited by children if they do not declare their own. These function are run in the following order:3 unmodified lines466 - `Run`467 - `PostRun`468 - `PersistentPostRun`469 400-And example of two commands which use all of these features is below. When the subcommand in executed it will run the root command's `PersistentPreRun` but not the root command's `PersistentPostRun`470+An example of two commands which use all of these features is below. When the subcommand is executed, it will run the root command's `PersistentPreRun` but not the root command's `PersistentPostRun`:471 472 ```go473 package main474 26 unmodified lines501 }502 503 var subCmd = &cobra.Command{504 Use: "sub [no options!]",435- Short: "My sub command",505+ Short: "My subcommand",506 PreRun: func(cmd *cobra.Command, args []string) {507 fmt.Printf("Inside subCmd PreRun with args: %v\n", args)508 },509 Run: func(cmd *cobra.Command, args []string) {19 unmodified lines529 530 531 ## Alternative Error Handling532 463-Cobra also has functions where the return signature is an error. This allows for errors to bubble up to the top, providing a way to handle the errors in one location. The current list of functions that return an error is:533+Cobra also has functions where the return signature is an error. This allows for errors to bubble up to the top, providing a way to handle the errors in one location. The current list of functions that return an error is:534 535 * PersistentPreRunE536 * PreRunE537 * RunE32 unmodified lines570 ```571 572 ## Suggestions when "unknown command" happens573 504-Cobra will print automatic suggestions when "unknown command" errors happen. This allows Cobra to behavior similarly to the `git` command when a typo happens. For example:574+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:575 576 ```577 $ hugo srever508-unknown command "srever" for "hugo"578+Error: unknown command "srever" for "hugo"579 580 Did you mean this?511- server581+ server582 583 Run 'hugo --help' for usage.584 ```585 516-Suggestions are automatic based on every subcommand registered and use an implementation of Levenshtein distance. Every registered command that matches a minimum distance of 2 (ignoring case) will be displayed as a suggestion.586+Suggestions are automatic based on every subcommand registered and use an implementation of [Levenshtein distance](http://en.wikipedia.org/wiki/Levenshtein_distance). Every registered command that matches a minimum distance of 2 (ignoring case) will be displayed as a suggestion.587 588 If you need to disable suggestions or tweak the string distance in your command, use:589 590+```go520- command.DisableSuggestions = true591+command.DisableSuggestions = true592+```593 594 or595 596+```go524- command.SuggestionsMinimumDistance = 1597+command.SuggestionsMinimumDistance = 1598+```599 600 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 makes sense in your set of commands and for some which you don't want aliases. Example:601 602 ```529-$ hugo delete530-unknown command "delete" for "hugo"603+$ kubectl remove604+Error: unknown command "remove" for "kubectl"605 606 Did you mean this?533- remove607+ delete608 535-Run 'hugo --help' for usage.609+Run 'kubectl help' for usage.610 ```611 538-## Generating markdown formatted documentation for your command612+## Generating Markdown-formatted documentation for your command613 540-Cobra can generate a markdown formatted document based on the subcommands, flags, etc. A simple example of how to do this for your command can be found in [Markdown Docs](md_docs.md)614+Cobra can generate a Markdown-formatted document based on the subcommands, flags, etc. A simple example of how to do this for your command can be found in [Markdown Docs](md_docs.md).615 616 ## Generating man pages for your command617 544-Cobra can generate a man page based on the subcommands, flags, etc. A simple example of how to do this for your command can be found in [Man Docs](man_docs.md)618+Cobra can generate a man page based on the subcommands, flags, etc. A simple example of how to do this for your command can be found in [Man Docs](man_docs.md).619 620 ## Generating bash completions for your command621 548-Cobra can generate a bash completions file. If you add more information to your command these completions can be amazingly powerful and flexible. Read more about [Bash Completions](bash_completions.md)622+Cobra can generate a bash-completion file. If you add more information to your command, these completions can be amazingly powerful and flexible. Read more about it in [Bash Completions](bash_completions.md).623 624 ## Debugging625 552-Cobra provides a ‘DebugFlags’ method on a command which when called will print553-out everything Cobra knows about the flags for each command626+Cobra provides a ‘DebugFlags’ method on a command which, when called, will print627+out everything Cobra knows about the flags for each command.628 629 ### Example630 631+```go557- command.DebugFlags()632+command.DebugFlags()633+```634 635 ## Release Notes636 * **0.9.0** June 17, 2014637 * flags can appears anywhere in the args (provided they are unambiguous)21 unmodified lines659 ## Extensions660 661 Libraries for extending Cobra:662 587-* [cmdns](https://github.com/gosuri/cmdns): Enables name spacing a command's immediate children. It provides an alternative way to structure sub commands, similar to `heroku apps:create` and `ovrclk clusters:launch`.663+* [cmdns](https://github.com/gosuri/cmdns): Enables name spacing a command's immediate children. It provides an alternative way to structure subcommands, similar to `heroku apps:create` and `ovrclk clusters:launch`.664 665 ## ToDo666 * Launch proper documentation site667 8 unmodified lines676 ## Contributors677 678 Names in no particular order:679 604-* [spf13](https://github.com/spf13)680+* [spf13](https://github.com/spf13), 681+[eparis](https://github.com/eparis), 682+[bep](https://github.com/bep), ...683 684 ## License685 686 Cobra is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/spf13/cobra/blob/master/LICENSE.txt)3 unmodified lines