commander: Index | Files

package commander

import ""

commander helps creating command line programs whose arguments are flags, commands and subcommands.


Package Files



const (
    CommandsList = iota


var (
    ErrFlagError    = errors.New("unable to parse flags")
    ErrCommandError = errors.New("unable to parse command")
var Defaults = Command{
    UsageTemplate: "" /* 510 byte string literal not displayed */,

    HelpTemplate: "" /* 130 byte string literal not displayed */,

type Command Uses

type Command struct {

    // UsageLine is the short usage message.
    // The first word in the line is taken to be the command name.
    UsageLine string

    // Short is the short description line shown in command lists.
    Short string

    // Long is the long description shown in the 'help <this-command>' output.
    Long string

    // List reports which list to show this command in Usage and Help.
    // Choose between {CommandsList (default), HelpTopicsList, Unlisted}
    List Listing

    // Run runs the command.
    // The args are the arguments after the command name.
    Run func(cmd *Command, args []string) error

    // Flag is a set of flags specific to this command.
    Flag flag.FlagSet

    // CustomFlags indicates that the command will do its own
    // flag parsing.
    CustomFlags bool

    // Subcommands are dispatched from this command
    Subcommands []*Command

    // Parent command, nil for root.
    Parent *Command

    // UsageTemplate formats the usage (short) information displayed to the user
    // (leave empty for default)
    UsageTemplate string

    // HelpTemplate formats the help (long) information displayed to the user
    // (leave empty for default)
    HelpTemplate string

    // Stdout and Stderr by default are os.Stdout and os.Stderr, but you can
    // point them at any io.Writer
    Stdout io.Writer
    Stderr io.Writer
    // contains filtered or unexported fields

A Command is an implementation of a subcommand.

func (*Command) ColFormat Uses

func (c *Command) ColFormat() string

ColFormat returns the column header size format for printing in the template

func (*Command) Dispatch Uses

func (c *Command) Dispatch(args []string) error

Dispatch executes the command using the provided arguments. If a subcommand exists matching the first argument, it is dispatched. Otherwise, the command's Run function is called.

func (*Command) FlagOptions Uses

func (c *Command) FlagOptions() string

FlagOptions returns the flag's options as a string

func (*Command) FullName Uses

func (c *Command) FullName() string

FullName returns the full name of the command, prefixed with parent commands

func (*Command) FullSpacedName Uses

func (c *Command) FullSpacedName() string

FullSpacedName returns the full name of the command, with ' ' instead of '-'

func (*Command) MaxLen Uses

func (c *Command) MaxLen() (res int)

func (*Command) Name Uses

func (c *Command) Name() string

Name returns the command's name: the first word in the usage line.

func (*Command) ParseFlags Uses

func (c *Command) ParseFlags(args []string) (result *flag.FlagSet, argsNoFlags []string, err error)

ParseFlags parses flags in whole command subtree and returns resulting FlagSet

func (*Command) Runnable Uses

func (c *Command) Runnable() bool

Runnable reports whether the command can be run; otherwise it is a documentation pseudo-command such as importpath.

func (*Command) SortCommands Uses

func (c *Command) SortCommands()

Sort the commands

func (*Command) SubcommandList Uses

func (c *Command) SubcommandList(list Listing) []*Command

func (*Command) Usage Uses

func (c *Command) Usage()

Usage prints the usage details to the standard error output.

type CommandSlice Uses

type CommandSlice []*Command

Type to allow us to use sort.Sort on a slice of Commands

func (CommandSlice) Len Uses

func (c CommandSlice) Len() int

func (CommandSlice) Less Uses

func (c CommandSlice) Less(i, j int) bool

func (CommandSlice) Swap Uses

func (c CommandSlice) Swap(i, j int)

type Listing Uses

type Listing int

UsageSection differentiates between sections in the usage text.

Package commander imports 10 packages (graph) and is imported by 94 packages. Updated 2016-07-21. Refresh now. Tools for package owners.