cobra/README.md

157 lines
4.2 KiB
Markdown
Raw Normal View History

2013-09-04 02:45:49 +04:00
# Cobra
A Commander for modern go CLI interactions
## Overview
Cobra provides a simple interface to create powerful modern CLI
interfaces similar to git & go tools.
Cobra was inspired by go, go-Commander, gh and subcommand
## Concepts
There are 3 different core objects to become familiar with to use Cobra.
To help illustrate these 3 items better use the following as an example:
hugo server --port=1313
### Commander
The Commander is the head of your application. It holds the configuration
for your application. It also is responsible for all global flags.
In the example above 'hugo' is the commander.
### Command
Command is the central point of the application. Each interaction that
the application supports will be contained in a Command. A command can
have children commands and optionally run an action.
In the example above 'server' is the command
### Flags
A flag is a way to modify the behavior of an command. Cobra supports
fully posix compliant flags as well as remaining consistent with
the go flag package. A Cobra command has can define flags that
persist through to children commands and flags that are only available
to that command.
In the example above 'port' is the flag.
## Usage
### Implementing Cobra
Using Cobra is easy. First use go get to install the latest version
of the library.
$ go get github.com/spf13/cobra
Next include cobra in your application.
import "github.com/spf13/cobra"
While it may be counter intuitive, You define your commands first,
assign flags to them, add them to the commander and lastly
execute the commander.
### Examples
## Simple Example
Import(
"github.com/spf13/cobra"
"fmt"
"strings"
)
func main() {
var echoTimes int
var cmdPrint = &cobra.Command{
Use: "print [string to print]",
Short: "Print anything to the screen",
2013-09-25 00:45:20 +04:00
Long: `print is for printing anything back to the screen.
For many years people have printed back to the screen.
`,
2013-09-04 02:45:49 +04:00
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Print: " + strings.Join(args, " "))
},
}
2013-09-25 00:45:20 +04:00
var cmdEcho = &Command{
2013-09-04 02:45:49 +04:00
Use: "echo [string to echo]",
Short: "Echo anything to the screen",
2013-09-25 00:45:20 +04:00
Long: `echo is for echoing anything back.
Echo works a lot like print, except it has a child command.
`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Print: " + strings.Join(args, " "))
},
}
var cmdTimes = &Command{
Use: "times [# times] [string to echo]",
Short: "Echo anything to the screen more times",
Long: `echo things multiple times back to the user by providing
a count and a string.`,
2013-09-04 02:45:49 +04:00
Run: func(cmd *cobra.Command, args []string) {
for i:=0; i < echoTimes; i++ {
fmt.Println("Echo: " + strings.Join(args, " "))
}
},
}
2013-09-25 00:45:20 +04:00
cmdTimes().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input")
2013-09-04 02:45:49 +04:00
var commander = cobra.Commander()
2013-09-25 00:45:20 +04:00
commander.SetName("CobraExample")
2013-09-04 02:45:49 +04:00
commander.AddCommand(cmdPrint, cmdEcho)
2013-09-25 00:45:20 +04:00
cmdEcho.AddCommand(cmdTimes)
2013-09-04 02:45:49 +04:00
commander.Execute()
}
## Release Notes
2013-09-25 00:45:20 +04:00
* **0.7.0** Sept 24, 2013
* Needs more eyes
* Test suite
* Support for automatic error messages
* Support for help command
* Support for printing to any io.Writer instead of os.Stderr
* Support for persistent flags which cascade down tree
* Ready for integration into Hugo
2013-09-04 02:45:49 +04:00
* **0.1.0** Sept 3, 2013
* Implement first draft
2013-09-25 00:45:20 +04:00
## ToDo
* More testing of non-runnable
* More testing
* Launch proper documentation site
2013-09-04 02:45:49 +04:00
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
## Contributors
Names in no particular order:
* [spf13](https://github.com/spf13)
## License
nitro is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/spf13/nitro/blob/master/LICENSE.txt)