gocli.v2: gopkg.in/tchap/gocli.v2 Index | Examples | Files

package gocli

import "gopkg.in/tchap/gocli.v2"

gocli is yet another package to aid with parsing command line arguments.

Unlike many other libraries, it focuses mainly on support of subcommands. Define as many subcommands as you want, they are handled by using FlagSets recursively. Simple yet powerful enough for many scenarios.

The help output format is inspired among others by codegangsta's cli library.



Package Files

app.go command.go doc.go help.go


var AppHelpTemplate = "" /* 323 byte string literal not displayed */

Default App help template.

var CommandHelpTemplate = "" /* 244 byte string literal not displayed */

Default Command help template.

type App Uses

type App struct {

    Name    string
    Version string

The root object that starts the chain of subcommands.


// Create the root App object.
app := NewApp("app")
app.Short = "my bloody gocli app"
app.Version = "1.2.3"
app.Long = `
  This is a long description of my super uber cool app.`

// A verbose switch flag.
var verbose bool

// Create a subcommand.
var subcmd = &Command{
    UsageLine: "subcmd [-v]",
    Short:     "some kind of subcommand, you name it",
    Long:      "Brb, too tired to write long descriptions.",
    Action: func(cmd *Command, args []string) {
        fmt.Printf("verbose mode set to %t\n", verbose)

// Set up the verbose switch. This can be as well called in init() or so.
subcmd.Flags.BoolVar(&verbose, "v", false, "print verbose output")

// Register the command with the parent command. Also suitable for init().

	Run the whole thing.

	app.Run([]string{}) would lead into:

	  app - my bloody gocli app

	  -h=false: print help and exit


	  This is a long description of my super uber cool app.

	  subcmd	 some kind of subcommand, you name it

	, app.Run([]string{"subcmd", "-h"}) into something similar.

app.Run([]string{"subcmd", "-v"})


verbose mode set to false
verbose mode set to true

func NewApp Uses

func NewApp(name string) *App

App constructor. Unfortunately App could not be written in a way that would allow to simply use a struct literal.

type Command Uses

type Command struct {
    // Fitting to be set in the Command struct literal.
    UsageLine string
    Short     string
    Long      string
    Action    func(cmd *Command, args []string)

    // Flag set for this subcommand.
    Flags flag.FlagSet

    // All subcommands registered. Although it can be accessed directly,
    // it is not supposed to be used like that. Use MustRegisterSubcmd().
    // This is exported only for the templates to be able to access it.
    Subcmds []*Command
    // contains filtered or unexported fields

Command represents a node in the chain of subcommands. Even App uses it, it is embedded there.

func (*Command) DefaultFlagsString Uses

func (cmd *Command) DefaultFlagsString() string

Returns the same as flag.FlagSet.PrintDefaults, but as a string instead of printing it directly into the output stream.

func (*Command) MustRegisterSubcommand Uses

func (cmd *Command) MustRegisterSubcommand(subcmd *Command)

Register a new subcommand with the command. Panic if something is wrong.

func (*Command) Name Uses

func (cmd *Command) Name() string

Get name from the usage line. The name is the first word on the usage line.

func (*Command) Run Uses

func (cmd *Command) Run(args []string)

Run the command with supplied arguments. This is called recursively if a subcommand is detected, just a prefix is cut off from the arguments.

func (*Command) Usage Uses

func (cmd *Command) Usage()

Print help and exit.

Package gocli imports 8 packages (graph) and is imported by 24 packages. Updated 2016-10-02. Refresh now. Tools for package owners.