subcommands: github.com/google/subcommands Index | Files

package subcommands

import "github.com/google/subcommands"

Package subcommands implements a simple way for a single command to have many subcommands, each of which takes arguments and so forth.

Index

Package Files

subcommands.go

func ImportantFlag Uses

func ImportantFlag(name string)

ImportantFlag marks a top-level flag as important, which means it will be printed out as part of the output of an ordinary "help" subcommand. (All flags, important or not, are printed by the "flags" subcommand.) It is a wrapper around DefaultCommander.ImportantFlag.

func Register Uses

func Register(cmd Command, group string)

Register adds a subcommand to the supported subcommands in the specified group. (Help output is sorted and arranged by group name.) The empty string is an acceptable group name; such subcommands are explained first before named groups. It is a wrapper around DefaultCommander.Register.

type Command Uses

type Command interface {
    // Name returns the name of the command.
    Name() string

    // Synopsis returns a short string (less than one line) describing the command.
    Synopsis() string

    // Usage returns a long string explaining the command and giving usage
    // information.
    Usage() string

    // SetFlags adds the flags for this command to the specified set.
    SetFlags(*flag.FlagSet)

    // Execute executes the command and returns an ExitStatus.
    Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) ExitStatus
}

A Command represents a single command.

func Alias Uses

func Alias(alias string, cmd Command) Command

Alias returns a Command alias which implements a "commands" subcommand.

func CommandsCommand Uses

func CommandsCommand() Command

CommandsCommand returns Command which implements a "commands" subcommand.

func FlagsCommand Uses

func FlagsCommand() Command

FlagsCommand returns a Command which implements "flags" for the DefaultCommander. Use Register(FlagsCommand(), <group>) for it to be recognized.

func HelpCommand Uses

func HelpCommand() Command

HelpCommand returns a Command which implements "help" for the DefaultCommander. Use Register(HelpCommand(), <group>) for it to be recognized.

type Commander Uses

type Commander struct {
    Output io.Writer // Output specifies where the commander should write its output (default: os.Stdout).
    Error  io.Writer // Error specifies where the commander should write its error (default: os.Stderr).
    // contains filtered or unexported fields
}

A Commander represents a set of commands.

var DefaultCommander *Commander

DefaultCommander is the default commander using flag.CommandLine for flags and os.Args[0] for the command name.

func NewCommander Uses

func NewCommander(topLevelFlags *flag.FlagSet, name string) *Commander

NewCommander returns a new commander with the specified top-level flags and command name. The Usage function for the topLevelFlags will be set as well.

func (*Commander) CommandsCommand Uses

func (cdr *Commander) CommandsCommand() Command

CommandsCommand returns Command which implements a "commands" subcommand.

func (*Commander) Execute Uses

func (cdr *Commander) Execute(ctx context.Context, args ...interface{}) ExitStatus

Execute should be called once the top-level-flags on a Commander have been initialized. It finds the correct subcommand and executes it, and returns an ExitStatus with the result. On a usage error, an appropriate message is printed to os.Stderr, and ExitUsageError is returned. The additional args are provided as-is to the Execute method of the selected Command.

func (*Commander) FlagsCommand Uses

func (cdr *Commander) FlagsCommand() Command

FlagsCommand returns a Command which implements a "flags" subcommand.

func (*Commander) HelpCommand Uses

func (cdr *Commander) HelpCommand() Command

HelpCommand returns a Command which implements a "help" subcommand.

func (*Commander) ImportantFlag Uses

func (cdr *Commander) ImportantFlag(name string)

ImportantFlag marks a top-level flag as important, which means it will be printed out as part of the output of an ordinary "help" subcommand. (All flags, important or not, are printed by the "flags" subcommand.)

func (*Commander) Register Uses

func (cdr *Commander) Register(cmd Command, group string)

Register adds a subcommand to the supported subcommands in the specified group. (Help output is sorted and arranged by group name.) The empty string is an acceptable group name; such subcommands are explained first before named groups.

type ExitStatus Uses

type ExitStatus int

An ExitStatus represents a Posix exit status that a subcommand expects to be returned to the shell.

const (
    ExitSuccess ExitStatus = iota
    ExitFailure
    ExitUsageError
)

func Execute Uses

func Execute(ctx context.Context, args ...interface{}) ExitStatus

Execute should be called once the default flags have been initialized by flag.Parse. It finds the correct subcommand and executes it, and returns an ExitStatus with the result. On a usage error, an appropriate message is printed to os.Stderr, and ExitUsageError is returned. The additional args are provided as-is to the Execute method of the selected Command. It is a wrapper around DefaultCommander.Execute.

Package subcommands imports 8 packages (graph) and is imported by 134 packages. Updated 2019-02-11. Refresh now. Tools for package owners.