Documentation
¶
Overview ¶
Package cli provides structure for command line applications with sub-commands.
This package uses a touch of reflection magic to dispatch to a method with named arguments. Commands help and version are implemented by default. The usage information is pretty printed in an opinionated format. That said, this package still attempts to embrace the standard library flag package.
This package assumes that any arguments will remain strings. Any non-string arguments are likely to be passed as optional flags in practice.
See the documentation of Rule for details and restrictions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Application ¶
type Application struct {
// contains filtered or unexported fields
}
An Application represents a command line application.
func New ¶
func New(name, version string) *Application
New creates a basic Application with help and version commands.
func (*Application) Rule ¶
func (a *Application) Rule(command command, name, arguments string) error
Rule registers a command with the Application.
The command being registered must meet the requirements of the fmt.Stringer interface. The command must also have a method Flags that accepts a new *flag.FlagSet. The Flags method is where you would define flags for this particular sub-command.
Additionally, the command must have a Run method. If the Run method has no return value, the program will end with a successful exit code. If the Run method has one or more return values, only the first is considered and must be of type int. The first return value will be used as the exit code.
The Run method may accept parameters of type string. If the Run method has more parameters than there are arguments, the extra parameters will just be empty strings. If the Run method has less parameters than there are arguments, they will silently be ignored. Optionally, the last parameter of the Run method can be of type []string. In this case, any extra parameters will be passed to the final argument.
func (*Application) Run ¶
func (a *Application) Run()
Run will parse flags and dispatch to the command.