Skip to content
Myr

Updating generator documentation and links

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

Merging the updated documentation from the user_guide into the cobra/README.md. Adding links as appropriate to both guides.

authored by Steve Francia · committed by Steve Francia · parent c97b7ec · Browse files · 3 files+76 added−82 removed
This commit

State

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

Diff stats

3 files+76 added−82 removed

  • other:+76 added−82 removed

3 files+76 added−82 removed

modified:cobra/README.md+69 added-19 removed
modified:README.md+6 added-2 removed
64 unmodified lines
65have children commands and optionally run an action.6667In the example above, 'server' is the command.6869[More about cobra.Command](https://godoc.org/github.com/spf13/cobra#Command)69[More about cobra.Command](https://pkg.go.dev/github.com/spf13/cobra#Command)7071## Flags7273A flag is a way to modify the behavior of a command. Cobra supports
20 unmodified lines
94import "github.com/spf13/cobra"95```9697# Usage98Cobra provides its own program that will create your application and add any99commands you want. It's the easiest way to incorporate Cobra into your application.10099See [User Guide](user_guide.md).101For complete details on using the Cobra generator, please read [The Cobra Generator README](https://github.com/spf13/cobra/blob/master/cobra/README.md)102103For complete details on using the Cobra library, please read the [The Cobra User Guide](user_guide.md).104105# License106107Cobra is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/spf13/cobra/blob/master/LICENSE.txt)
modified:user_guide.md+1 added-61 removed
30 unmodified lines
3132Cobra provides its own program that will create your application and add any33commands you want. It's the easiest way to incorporate Cobra into your application.3435Install the cobra generator with the command `go install github.com/spf13/cobra/cobra`. 36Go will automatically install it in your $GOPATH/bin directory which should be in your $PATH. 35For complete details on using the Cobra generator, please read [The Cobra Generator README](https://github.com/spf13/cobra/blob/master/cobra/README.md)3638Once installed you should have the `cobra` command available. Confirm by typing `cobra` at a 39command line. 4041There are only two operations currently supported by Cobra generator. 4243### 1. Initializing a new project4445The Cobra generator works from within a Go module. 4647If you haven't yet setup your project as a Go module: 4849 1. Create a new directory 50 2. `cd` into that directory51 3. run `go mod init <MODNAME>`5253From within a Go module run `cobra init`. This will create a new barebones project54for you to edit. 5556You should be able to run you new application immediately. Try it with 57`go run main.go`. 5859You will want to open up and edit 'cmd/root.go' and provide your own description and logic. 6061#### Optional flags:62You can provide it your author name with the `--author` flag. 63e.g. `cobra init --author "Steve Francia spf@spf13.com"`6465You can provide a license to use with `--license` 66e.g. `cobra init --license apache`6768Use the `--viper` flag to automatically setup [viper](https://github.com/spf13/viper)6970Viper is a companion to Cobra intended to provide easy handling of environment variables and config files71and seamlessly connecting them to the application flags.7273### 2. Add a command to a project7475Once a cobra application is initialized you can continue to use cobra generator to 76add additional commands to your application. The command to do this is `cobra add`. 7778As an example, if I was designing a todo application I would want to have my base `todo` command list the items. 79I would then add additional commands to display, create, mark complete and delete items.8081To add a command to an existing application, make sure you are in the directory with the main.go file and run:82`cobra add <cmdname>`. 8384#### Optional flags: 8586`cobra add` supports all the same optional flags as `cobra init` does.8788Additionally you can provide a parent command for your new command. This defaults to rootCmd if not provided.89If you want to place your command under a different command, just provide the name of the command.9091A todo is a bit too simple to really need a sub sub command. So let's use git as an example. 9293If I wanted to create a new git stash command I would do the following:94`cobra add stash`95`cobra add pop --parent=stash`9637## Using the Cobra Library3839To manually implement Cobra you need to create a bare main.go file and a rootCmd file.40You will optionally provide additional commands as you see fit.
597 unmodified lines