Documentation ¶
Overview ¶
Package command implements a command line interface for applications that host multiple children commands similar to go and git.
The common pattern of that applications is: APPNAME COMMAND --FLAG(s) ARGUMENT(s)
Each interaction with the application is implemented as a command. A command usually runs an action, or alternatively it can provide multiple children commands, or a help topic. Flags can be given to modify the command's action and arguments usually are the objects in which the actions are executed.
No constructor is required to create a new command. Just initialize the usage and documentation fields. To define the command's action implement the Run function. To define the flags used by the command implement SetFlag function and use method Flags to retrieve the current flag set of the command. To add children commands use the Add method.
To run a command with a given set of arguments, use the method Execute.
Index ¶
- type Command
- func (c *Command) Add(child *Command)
- func (c *Command) Execute(args []string) error
- func (c *Command) Flags() *flag.FlagSet
- func (c *Command) Main()
- func (c *Command) SetStderr(w io.Writer)
- func (c *Command) SetStdin(r io.Reader)
- func (c *Command) SetStdout(w io.Writer)
- func (c *Command) Stderr() io.Writer
- func (c *Command) Stdin() io.Reader
- func (c *Command) Stdout() io.Writer
- func (c *Command) UsageError(msg string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Command ¶
type Command struct { // Usage is the usage message of the Command // including flags and arguments. // (do not include any parent Command). // // Recommended syntax is as follows: // [] indicates an optional flag or argument. // <> indicates a value to be set by the user, // for example <file>. // ... indicates that multiple values // of the previous argument can be specified. // // The first word of the usage message // is taken to be the Command's name. Usage string // Short is a short description // (on a single line) // of the Command. Short string // Long is a long description // of the Command. Long string // Run runs the Command. // The args are the unparsed arguments. Run func(c *Command, args []string) error // SetFlags is the function used // to define the flags specific to the command. // Use method Flags to retrieve // the FlagSet of the command. SetFlags func(c *Command) // contains filtered or unexported fields }
A Command is a command in an application like 'run' in 'go run'.
When creating a Command always set up the Command's usage field. To provide help messages define a short and long description of the Command.
func (*Command) Add ¶
Add adds a child command to a Command. This function panics if the child command is invalid:
- because it is nil
- because it does not have a name
- because there is a child command with the same name
- because the child already has a parent
- because the command is already a child of the child command
func (*Command) Main ¶
func (c *Command) Main()
Main executes a Command using the IS command line arguments. If an error happens when executing the Command, it will print the error in the programs' standard error, and finish the application.
Main will panic if the Command is not a root Command.
func (*Command) Stderr ¶
Stderr returns the Command's standard error. By default returns its parent stderr or os.Stderr if parent is nil.
func (*Command) Stdin ¶
Stdin returns the Command's standard input. By default returns its parent stdin or os.Stdin if parent is nil.
func (*Command) Stdout ¶
Stdout returns the Command's standard output. By default returns its parent stdout or os.Stdout if parent is nil.
func (*Command) UsageError ¶
UsageError should be returned by Run function when an error on an argument is found.