Rewrote readme
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by spf13 · committed by spf13 · parent 79bdde5 · Browse files · 1 file·+242 added−35 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+242 added−35 removed
- other:+242 added−35 removed
Provenance
- Commit
7216b06- Tree
0440e75- Author
spf13 <steve.francia@gmail.com>- Committer
spf13 <steve.francia@gmail.com>- Parent
79bdde5
1 file changed+242 added−35 removed
modified:README.md+242 added-35 removed
3 unmodified lines4 5 [](https://travis-ci.org/spf13/cobra)6 7 ## Overview8-Cobra provides a simple interface to create powerful modern CLI9-interfaces similar to git & go tools.8 11-Cobra was inspired by go, go-Commander, gh and subcommand9+Cobra is a commander providing a simple interface to create powerful modern CLI10+interfaces similar to git & go tools. In addition to providing an iterface, Cobra11+simultaneously provides a controller to organize your application code.12 13+Inspired by go, go-Commander, gh and subcommand, Cobra improves on these by14+providing **fully posix compliant flags** (including short & long versions),15+**nesting commands**, and the ability to **define your own help and usage** for any or16+all commands.17 18+Cobra has an exceptionally clean interface and simple design without needless19+constructors or initialization methods.20+21 ## Concepts22 16-There are 3 different core objects to become familiar with to use Cobra.17-To help illustrate these 3 items better use the following as an example:23+Cobra is built on a structure of commands & flags.24 19- hugo server --port=131325+**Commands** represent actions and **Flags** are modifiers for those actions.26 21-### Commander27+In the following example 'server' is a command and 'port' is a flag.28 23-The Commander is the head of your application. It holds the configuration24-for your application. It also is responsible for all global flags.29+ hugo server --port=131330 26-In the example above 'hugo' is the commander.31+### Commands32 28-29-### Command30-33 Command is the central point of the application. Each interaction that34 the application supports will be contained in a Command. A command can35 have children commands and optionally run an action.36 37 In the example above 'server' is the command38 39+A Command has the following structure:40 41+ type Command struct {42+ Use string // The one-line usage message.43+ Short string // The short description shown in the 'help' output.44+ Long string // The long message shown in the 'help <this-command>' output.45+ Run func(cmd *Command, args []string) // Run runs the command.46+ }47+48 ### Flags49 40-A flag is a way to modify the behavior of an command. Cobra supports41-fully posix compliant flags as well as remaining consistent with42-the go flag package. A Cobra command has can define flags that 43-persist through to children commands and flags that are only available44-to that command.50+A Flag is a way to modify the behavior of an command. Cobra supports51+fully posix compliant flags as well as the go flag package. 52+A Cobra command has can define flags that persist through to children commands53+and flags that are only available to that command.54 55 In the example above 'port' is the flag.56 57+Flag functionality is provided by the [pflag58+libary](https://github.com/ogier/pflag), a fork of the flag standard library59+which maintains the same interface while adding posix compliance.60+61 ## Usage62 50-### Implementing Cobra63+Cobra works by creating a set of commands and then organizing them into a tree.64+The tree defines the structure of the application.65 66+Once each command is defined with it's corresponding flags, then the67+tree is assigned to the commander which is finally executed.68+69+### Installing70 Using Cobra is easy. First use go get to install the latest version71 of the library.72 73 $ go get github.com/spf13/cobra1 unmodified line75 Next include cobra in your application.76 77 import "github.com/spf13/cobra"78 61-Now you are ready to implement Cobra. 79+### Create the root command80 63-Cobra works by creating a set of commands and then organizing them into a tree.64-The tree defines the structure of the application.81+The root command represents your binary itself.82 66-Once each command is defined with it's corresponding flags, then the67-tree is assigned to the commander which is finally executed.83+Cobra doesn't require any special constructors. Simply create your commands.84 69-In the example below we have defined three commands. Two are at the top70-level and one (cmdTimes) is a child of one of the top commands.85+ var HugoCmd = &cobra.Command{86+ Use: "hugo",87+ Short: "Hugo is a very fast static site generator",88+ Long: `A Fast and Flexible Static Site Generator built with89+ love by spf13 and friends in Go.90+ Complete documentation is available at http://hugo.spf13.com`,91+ Run: func(cmd *cobra.Command, args []string) {92+ // Do Stuff Here93+ },94+ }95 72-We have only defined one flag for a single command.96+### Create additional commands97 74-More documentation about flags is available at https://github.com/spf13/pflag98+Additional commands can be defined.99 100+ var versionCmd = &cobra.Command{101+ Use: "version",102+ Short: "Print the version number of Hugo",103+ Long: `All software has versions. This is Hugo's`,104+ Run: func(cmd *cobra.Command, args []string) {105+ fmt.Println("Hugo Static Site Generator v0.9 -- HEAD")106+ },107+ }108+109+### Attach command to it's parent110+In this example we are attaching it to the root, but commands can be attached at any level.111+112+ HugoCmd.AddCommand(versionCmd)113+114+### Assign flags to a command115+116+Since the flags are defined and used in different locations, we need to define a variable outside with the correct scope to assign the flag to work with.117+118+ var Verbose bool119+ var Source string120+121+There are two different approaches to assign a flag.122+123+#### Persistent Flags124+125+A flag can be 'persistent' meaning that this flag will be available to the126+command it's assigned to as well as every command under that command. For127+global flags assign a flag as a persistent flag on the root.128+129+ HugoCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")130+131+#### Local Flags132+133+A flag can also be assigned locally which will only apply to that specific command.134+135+ HugoCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")136+137+### Once all commands and flags are defined, Execute the commands138+139+Execute should be run on the root for clarity, though it can be called on any command.140+141+ HugoCmd.Execute()142+143 ## Example144 145+In the example below we have defined three commands. Two are at the top level146+and one (cmdTimes) is a child of one of the top commands. In this case the root147+is not executible meaning that a subcommand is required. This is accomplished148+by not providing a 'Run' for the 'rootCmd'.149+150+We have only defined one flag for a single command.151+152+More documentation about flags is available at https://github.com/spf13/pflag153+154 Import(155 "github.com/spf13/cobra"156 "fmt"157 "strings"36 unmodified lines194 }195 },196 }197 122-198 cmdTimes().Flags().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input")199 125- var commander = cobra.NewCommander()126- commander.SetName("CobraExample")127- commander.AddCommand(cmdPrint, cmdEcho)200+ var rootCmd = &cobra.Command{Use: "app"}201+ rootCmd.AddCommand(cmdPrint, cmdEcho)202 cmdEcho.AddCommand(cmdTimes)129- commander.Execute()203+ rootCmd.Execute()204 }205 206+For a more complete example of a larger application, please checkout [Hugo](http://hugo.spf13.com)207+208+## The Help Command209+210+Cobra automatically adds a help command to your application.211+This will be called when a user runs 'app help'. Additionally help will also212+support all other commands as input. Say for instance you have a command called213+'create' without any additional configuration cobra will work when 'app help214+create' is called.215+216+### Example217+218+The following output is automatically generated by cobra. Nothing beyond the219+command and flag definitions are needed.220+221+ > hugo help222+223+ A Fast and Flexible Static Site Generator built with224+ love by spf13 and friends in Go.225+226+ Complete documentation is available at http://hugo.spf13.com227+228+ Usage:229+ hugo [flags]230+ hugo [command]231+232+ Available Commands:233+ server :: Hugo runs it's own a webserver to render the files234+ version :: Print the version number of Hugo235+ check :: Check content in the source directory236+ benchmark :: Benchmark hugo by building a site a number of times237+ help [command] :: Help about any command238+239+ Available Flags:240+ -b, --base-url="": hostname (and path) to the root eg. http://spf13.com/241+ -D, --build-drafts=false: include content marked as draft242+ --config="": config file (default is path/config.yaml|json|toml)243+ -d, --destination="": filesystem path to write files to244+ -s, --source="": filesystem path to read files relative from245+ --stepAnalysis=false: display memory and timing of different steps of the program246+ --uglyurls=false: if true, use /filename.html instead of /filename/247+ -v, --verbose=false: verbose output248+ -w, --watch=false: watch filesystem for changes and recreate as needed249+250+ Use "hugo help [command]" for more information about that command.251+252+253+254+Help is just a command like any other. There is no special logic or behavior255+around it. In fact you can provide your own if you want.256+257+### Defining your own help258+259+You can provide your own Help command or you own template for the default command to use.260+261+The default help command is 262+263+ func (c *Command) initHelp() {264+ if c.helpCommand == nil {265+ c.helpCommand = &Command{266+ Use: "help [command]",267+ Short: "Help about any command",268+ Long: `Help provides help for any command in the application.269+ Simply type ` + c.Name() + ` help [path to command] for full details.`,270+ Run: c.HelpFunc(),271+ }272+ }273+ c.AddCommand(c.helpCommand)274+ }275+276+You can provide your own command, function or template through the following methods.277+278+ command.SetHelpCommand(cmd *Command)279+280+ command.SetHelpFunc(f func(*Command, []string))281+282+ command.SetHelpTemplate(s string)283+284+The latter two will also apply to any children commands.285+286+287+## Usage288+289+When the user provides an invalid flag or invalid command Cobra responds by290+showing the user the 'usage'291+292+### Example293+You may recognize this from the help above. That's because the default help 294+embeds the usage as part of it's output.295+296+ Usage:297+ hugo [flags]298+ hugo [command]299+300+ Available Commands:301+ server :: Hugo runs it's own a webserver to render the files302+ version :: Print the version number of Hugo303+ check :: Check content in the source directory304+ benchmark :: Benchmark hugo by building a site a number of times305+ help [command] :: Help about any command306+307+ Available Flags:308+ -b, --base-url="": hostname (and path) to the root eg. http://spf13.com/309+ -D, --build-drafts=false: include content marked as draft310+ --config="": config file (default is path/config.yaml|json|toml)311+ -d, --destination="": filesystem path to write files to312+ -s, --source="": filesystem path to read files relative from313+ --stepAnalysis=false: display memory and timing of different steps of the program314+ --uglyurls=false: if true, use /filename.html instead of /filename/315+ -v, --verbose=false: verbose output316+ -w, --watch=false: watch filesystem for changes and recreate as needed317+318+### Defining your own usage319+You can provide your own usage function or template for cobra to use.320+321+The default usage function is322+323+ return func(c *Command) error {324+ err := tmpl(c.Out(), c.UsageTemplate(), c)325+ return err326+ }327+328+Like help the function and template are over ridable through public methods.329+330+ command.SetUsageFunc(f func(*Command) error)331+332+ command.SetUsageTemplate(s string)333+334+335 ## Release Notes336+* **0.8.0** Nov 5, 2013337+ * Reworked interface to remove commander completely338+ * Command now primary structure339+ * No initialization needed340+ * Usage & Help templates & functions definable at any level341+ * Updated Readme342 * **0.7.0** Sept 24, 2013343 * Needs more eyes344 * Test suite345 * Support for automatic error messages4 unmodified lines350 * **0.1.0** Sept 3, 2013351 * Implement first draft352 353 ## ToDo145-* More testing of non-runnable146-* More testing354 * Launch proper documentation site355 356 ## Contributing357 15 unmodified lines