Documentation ¶
Overview ¶
Package command implements helper functions for multi-command programs.
This package includes types and functions for easily defining multiple subcommands with different options. A "help" subcommand is also automatically generated, which might be used to list all the available subcommands or to view all the help about a specific subcommand.
Clients should just define a list of the command they implement and just call Run or RunOpts directly from main().
package main import ( "github.com/rainycape/command" ) var ( cmds = []*command.Cmd{ { Name: "awesome", Help: "do something awesome" Options: &awesomeOptions{Value:42}, }, } ) type awesomeOptions struct { Value int `name:"v" help:"Some arbitrary value"` } func awesomeCommand(args *command.Args, opts *AwesomeOptions) error { ... } func main() { command.Exit(command.Run(cmds)) }
Index ¶
Constants ¶
const ( // Setting CommandDumpHelpEnvVar to a non-empty // value causes any tool using command to dump // its help as JSON to the standard output when // it's run. It's intended to be used by 3rd party // tools to automatically generate documentation // for any tool using this package. CommandDumpHelpEnvVar = "COMMAND_DUMP_HELP" )
const ( // When this environment variable is non-empty, command won't // recover from a panic during a command, producing the full // stack trace. Useful for debugging problems with commands. LetCommandPanic = "LET_COMMAND_PANIC" )
Variables ¶
var ( // ErrNoCommand is returned from Run when no command // has been specified (i.e. there are no arguments). ErrNoCommand = errors.New("no command provided") // ErrHelp is returned from Run when the help is shown, // either the brief help or the detailed help for a // command. ErrHelp = errors.New("help has been shown") // ErrUnusedArguments is returned form Run when the command // does not accept any arguments, but the user has provided // some. ErrUnusedArguments = errors.New("arguments provided but not used") )
var ( // NoArgs is used to indicate that a command // receives no arguments. If the user provides any // additional arguments, it will return an error. NoArgs = []*Argument{nil} )
Functions ¶
func Exit ¶
func Exit(err error)
Exit exits with exit status zero when err is nil and with non-zero when err is non-nil.
func RunOpts ¶
RunOpts tries to run a command from the specified list using the given arguments, interpreting the first argument as the command name. If the returned error is non-nil, it will be one of:
- ErrNoCommand when no arguments are provided
- ErrHelp when the user has requested any help to be shown
- ErrUnusedArguments when the command doesn't accept any arguments, but the user has provided some
- An UnknownCommandError when the command (the first argument) does not exist
- Any error returned by Options.BeforeFunc or Options.Func
- Any error returned by the command handler
If args is nil, it will be set to os.Args[1:].
Any user error will be printed to os.Stderr by RunOpts, so callers don't need to print any error messages themselves.
Note that RunOpts will panic in case of a programming error. This usually happens when Func or Options don't match the required constraints. See the documentation on those fields in the Cmd type for more information.
Types ¶
type Args ¶
type Args struct {
// contains filtered or unexported fields
}
Type Args is used by command functions to receive their arguments.
func (*Args) Int ¶
Returns the argument with the given name as an int. If the argument does not exist or it can't be parsed as an int, it panics.
type Argument ¶
type Argument struct { // The name of the argument Name string // Help associated with the argument. Will be // displayed by the help command. Help string // Wheter the argument is optional Optional bool }
Type Argument holds a required argument for a command. Command arguments are processed in the same order they're defined. Optional arguments must always be the last ones.
type Cmd ¶
type Cmd struct { // Name is the name of the command, case sensitive. Name string // Help is a short, one line help string displayed by // the help command when listing all the commands. Help string // LongHelp is a long, potentially multi-line, help message // displayed when using the help command for a specific // command (e.g. myprogram help somecommand) LongHelp string // Usage is displayed when showing the help for a specific // command. The program name (os.Args[0]) and the command // name are prepended to it when displaying it to the user, // as well as any arguments defined in the Args field. // (e.g. Usage = <something> shows "usage: myprog subcmd <something>") Usage string // Args accepted by the command. If nil, no argument validation // is performed. To define a command which accepts no arguments and // errors when arguments are passed, set this field to NoArgs. // See the Argument and Args types for more information. Args []*Argument // Func is the handler function for the command. The function must take either // one or two arguments. The first one must be an *Args, which is // used to access non-flag arguments. // If the function accepts a second argument it must // be of the exact same type than the value provided in the Options field. // Handler functions might optionally return an error value. Func interface{} // Options might be either nil or a pointer to a struct type. Command flags // will be generated from this struct, in the same order as the fields are // defined. The current value of the field will be used as the default value // for the flag. Each field might also include two struct tags: // // - name: The name of the flag. If not present, it will default to the field name in lowercase. // - help: The short help shown the flag package for the given field. Options interface{} }
Cmd represents an available command.
type CommandHelp ¶
type CommandHelp struct { Name string `json:"name"` Help string `json:"help"` LongHelp string `json:"long_help"` Usage string `json:"usage"` Flags []*Flag `json:"flags"` }
CommandHelp is the help for a given command.
type CommandProvider ¶
The CommandProvider interface might be implemented by the type used in the Options field of the Options type. If implemented, its Commands function is called after Options.BeforeFunc and before Options.Func
type Flag ¶
type Flag struct { Name string `json:"name"` Help string `json:"help"` Type string `json:"type"` Default string `json:"default"` }
Flag represents a global or a command flag.
type Help ¶
type Help struct { Name string `json:"name"` Flags []*Flag `json:"flags"` Commands []*CommandHelp `json:"commands"` }
Help represents the help for a tool using this package. This structure is used when dumping the help in JSON, so other packages can use it to parse the help output from a command.
type Options ¶
type Options struct { // Options represents global options which the application // needs to handle before running any commands. If this field // is non-nil and Handler is also non-nil, its first argument // must match the type of this field. // // Optionally, the value in this field might implement the // CommandProvider interface. In that case, its Commands function // is called after BeforeFunc and before Func. Options interface{} // Func is called after the command to execute is determined but before // executing it. Func func(*Cmd, *Options) error // BeforeFunc must follow the same characteristics of Func, except it // can't take an optional *Cmd parameter. // // BeforeFunc is called before the command to execute is determined, so // it can be used to conditionally set up additional commands. BeforeFunc func(*Options) error }
Options are used to specify additional options when calling RunOpts
type UnknownCommandError ¶
type UnknownCommandError string
UnknownCommandError is returned from Run when the specified command does not exist.
func (UnknownCommandError) Error ¶
func (e UnknownCommandError) Error() string
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
command-tool
command-tool implements a command line application which generates documentation for tools using gopkgs.com/command.
|
command-tool implements a command line application which generates documentation for tools using gopkgs.com/command. |