diff --git a/README.md b/README.md index 70f9b50..9d942c7 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Cobra provides: * Fully POSIX-compliant flags (including short & long versions) * Nested subcommands * Global, local and cascading flags -* Easy generation of applications & commands with `cobra init appname` & `cobra add cmdname` +* Easy generation of applications & commands with `cobra init` & `cobra add cmdname` * Intelligent suggestions (`app srver`... did you mean `app server`?) * Automatic help generation for commands and flags * Automatic help flag recognition of `-h`, `--help`, etc. @@ -32,7 +32,7 @@ Cobra provides: * Automatically generated man pages for your application * Command aliases so you can change things without breaking them * The flexibility to define your own help, usage, etc. -* Optional tight integration with [viper](http://github.com/spf13/viper) for 12-factor apps +* Optional seamless integration with [viper](http://github.com/spf13/viper) for 12-factor apps # Concepts diff --git a/user_guide.md b/user_guide.md index 311abce..ad47889 100644 --- a/user_guide.md +++ b/user_guide.md @@ -32,7 +32,67 @@ func main() { Cobra provides its own program that will create your application and add any commands you want. It's the easiest way to incorporate Cobra into your application. -[Here](https://github.com/spf13/cobra/blob/master/cobra/README.md) you can find more information about it. +Install the cobra generator with the command `go install github.com/spf13/cobra/cobra`. +Go will automatically install it in your $GOPATH/bin directory which should be in your $PATH. + +Once installed you should have the `cobra` command available. Confirm by typing `cobra` at a +command line. + +There are only two operations currently supported by Cobra generator. + +### 1. Initializing a new project + +The Cobra generator works from within a Go module. + +If you haven't yet setup your project as a Go module: + + 1. Create a new directory + 2. `cd` into that directory + 3. run `go mod init ` + +From within a Go module run `cobra init`. This will create a new barebones project +for you to edit. + +You should be able to run you new application immediately. Try it with +`go run main.go`. + +You will want to open up and edit 'cmd/root.go' and provide your own description and logic. + +#### Optional flags: +You can provide it your author name with the `--author` flag. +e.g. `cobra init --author "Steve Francia spf@spf13.com"` + +You can provide a license to use with `--license` +e.g. `cobra init --license apache` + +Use the `--viper` flag to automatically setup [viper](https://github.com/spf13/viper) + +Viper is a companion to Cobra intended to provide easy handling of environment variables and config files +and seamlessly connecting them to the application flags. + +### 2. Add a command to a project + +Once a cobra application is initialized you can continue to use cobra generator to +add additional commands to your application. The command to do this is `cobra add`. + +As an example, if I was designing a todo application I would want to have my base `todo` command list the items. +I would then add additional commands to display, create, mark complete and delete items. + +To add a command to an existing application, make sure you are in the directory with the main.go file and run: +`cobra add `. + +#### Optional flags: + +`cobra add` supports all the same optional flags as `cobra init` does. + +Additionally you can provide a parent command for your new command. This defaults to rootCmd if not provided. +If you want to place your command under a different command, just provide the name of the command. + +A todo is a bit too simple to really need a sub sub command. So let's use git as an example. + +If I wanted to create a new git stash command I would do the following: +`cobra add stash` +`cobra add pop --parent=stash` ## Using the Cobra Library