Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cli ¶
type Cli struct { Logger // Description describes the application. Description string // The syntax description of the command. It appears in Usage after the // name of the program (os.Args[0]). If not specified, "[OPTIONS]..." // will be used instead. Syntax string // Flag is a set of flags to parse. Flags []*Flag // Args is a list of leftover arguments that were not parsed. Args []string // Sub contains sub-commands. Sub map[string]*Cli // Super contains the string of commands used to invoke this Cli. // For example, a command invoked using // <program> foo bar // Would have Super set to // "foo bar" // This is automatically filled out by Cli.AddSub. Super string }
Cli represents a command line interface. It is not tied to os.Args, and it is possible to create multiple Cli's to have so many sub-commands in one program.
func New ¶
New creates a new Cli from a command description and a set of flags. These flags should be created as variables before passing them to New(), so that their values can be extracted after Parse is called.
func (*Cli) AddSub ¶
AddSub adds a sub-command to this command. It automatically fills out the sub-command's Super field, and alters the prefix of its Logger to match.
func (*Cli) LongFlag ¶
LongFlag searches for and returns the flag with the given long form. If it was found, it returns true. If it was not found, it returns nil, false.
func (*Cli) Parse ¶
Parse parses the given set of command line arguments according to the flags defined within the Cli. The first element of the argument slice is always dropped, because it is assumed that it just contains the command name. If the second element matches up with the name of a sub-command, parsing is deferred to that sub-command instead. The sub-command will be given all but the first element of args. This method will return the address of the command that did the parsing.
func (*Cli) ParseOrExit ¶
ParseOrExit is like Parse, but if an error occurs while parsing the argument list, it prints out Usage and exits with error status 2.
type Flag ¶
type Flag struct { Short rune // The short form of the flag (-l) Long string // Long form of the flag (--long-form) Help string // Help text to display by the flag // Validate is an optional function that is called when an input is // passed to the flag. It checks whether or not the input is valid, and // returns an error if it isn't. If this function is nil, the flag is // assumed to have no input. Validate func(string) error // Value contains the input given to the flag, and is filled out when // the flags are parsed. If this is a non-input flag (if Validate is // nil), this will be set to true. If the flag was not specified, Value // will be unchanged. Value string // Found is an optional function that is called each time the flag is // found, and has a valid argument (if applicable). The value is passed // to the function, as well as the command that it was found in. Found func(*Cli, string) }
Flag is a command line option.
func NewHelp ¶
func NewHelp() *Flag
NewHelp creates a new help flag activated by --help or -h. It shows help information for the command that it is a part of, and causes the program to exit with a status of 2.
type Logger ¶
type Logger struct { // Writer specifies the writer to output to. If it is nil, the Logger // will output to os.Stderr. If you wish to silence the output, set it // to io.Discard. io.Writer // Debug determines whether or not debug messages will be logged. Debug bool // Prefix is printed before all messages. If this is an empty string, // os.Args[0] will be used. Prefix string }
Logger prints messages to an output writer.