Go to file
2013-09-24 16:45:20 -04:00
.gitignore ignoring compiled test 2013-09-03 18:57:14 -04:00
.travis.yml Adding support for travis ci 2013-09-24 12:39:05 -04:00
cobra_test.go Add support for help command 2013-09-24 16:03:22 -04:00
cobra.go Organize the files a bit better 2013-09-24 12:47:29 -04:00
command.go Add support for help command 2013-09-24 16:03:22 -04:00
commander.go Add support for help command 2013-09-24 16:03:22 -04:00
LICENSE.txt adding license 2013-09-03 18:46:13 -04:00
README.md more complete readme 2013-09-24 16:45:20 -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",
        Long:  `print is for printing anything back to the screen.
        For many years people have printed back to the screen.
        `,
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Println("Print: " + strings.Join(args, " "))
        },
    }

    var cmdEcho = &Command{
        Use:   "echo [string to echo]",
        Short: "Echo anything to the screen",
        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.`,
        Run: func(cmd *cobra.Command, args []string) {
            for i:=0; i < echoTimes; i++ {
                fmt.Println("Echo: " + strings.Join(args, " "))
            }
        },
    }


    cmdTimes().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input")

    var commander = cobra.Commander()
    commander.SetName("CobraExample")
    commander.AddCommand(cmdPrint, cmdEcho)
    cmdEcho.AddCommand(cmdTimes)
    commander.Execute()
}

Release Notes

  • 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
  • 0.1.0 Sept 3, 2013
    • Implement first draft

ToDo

  • More testing of non-runnable
  • More testing
  • Launch proper documentation site

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:

License

nitro is released under the Apache 2.0 license. See LICENSE.txt