Documentation ¶
Overview ¶
Package subcmd works with the flags package to implement sub-commands in the manner of git and similar tools.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrHelp = errors.New("subcmd: help requested")
ErrHelp is the error returned if the first argument is "help", "-h", "-help", or "--help".
var Usage = func(cmds []Command) { defaultUsage(os.Args[0], cmds) }
Usage prints a help message listing the possible commands. The function is a variable that may be changed to point at a custom function.
Functions ¶
func PrintDefaults ¶
func PrintDefaults(cmds []Command)
PrintDefaults formats a list of commands. For each command, the output is
Name Description
func Run ¶
func Run(cmds []Command)
Run parses os.Args and dispatches to the correct subcommand given by cmds. It produces an error message listing the commands with their descriptions if a nonexistent subcommand is provided, or if the command is "help", "-h", "-help", or "--help". This error message may be customized by altering Usage. If the command provided isn't one of those in cmds, Run calls os.Exit(2) after printing the error message.
Run panics if any command is named "help", "-h", "-help", or "--help", or if any two commands have the same name.
Types ¶
type Command ¶
type Command struct { Name string // the command's one-word name Description string // a short description of the command Do func(args []string) // command implementation }
A Command specifies a sub-command for a program's command-line interface.
type Runner ¶ added in v1.1.0
type Runner struct { // Usage prints the runner's usage. // If Usage is nil, the package-level Usage is called instead. Usage func() // contains filtered or unexported fields }
A Runner runs sub-commands. To change Usage or ErrorHandling, alter these after creating a runner with New but before calling Runner.Run.
func New ¶ added in v1.1.0
func New(name string, cmds []Command, errorHandling flag.ErrorHandling) *Runner
New creates a Runner with the given name and command list. The error-handling behavior of Run is controlled by errorHandling and has the same semantics as for flag.FlagSet.
New panics if any command is named "help", "-h", "-help", or "--help", or if any two commands have the same name.
func (*Runner) Run ¶ added in v1.1.0
Run parses args and dispatches to the correct subcommand. It produces an error message listing the commands with their descriptions if a nonexistent subcommand is provided, or if the command is "help", "-h", "-help", or "--help". This error message may be customized by altering r.Usage.