Documentation ¶
Overview ¶
Package console contains the core code that ties everything together. This includes the Application type that actually runs the commands and reacts to input, and also functions for parsing and mapping input that use other sub-packages.
Index ¶
- func DescribeApplication(app *Application) string
- func DescribeCommand(app *Application, cmd *Command, path []string) string
- func DescribeCommands(commands []*Command) string
- func MapInput(definition *Definition, input *Input, env []string) error
- func MapInput2(definition *Definition, input Input, env []string) error
- type Application
- type ArgumentDefinition
- type Command
- type CommandContainer
- type ConfigureFunc
- type Definition
- type ExecuteFunc
- type Input
- type InputArgument
- type InputOption
- type OptionDefinition
- type Output
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DescribeApplication ¶
func DescribeApplication(app *Application) string
DescribeApplication describes an Application to provide usage information.
func DescribeCommand ¶
func DescribeCommand(app *Application, cmd *Command, path []string) string
DescribeCommand describes a Command on an Application to provide usage information.
func DescribeCommands ¶
DescribeCommands describes an array of Commands to provide usage information.
func MapInput ¶
func MapInput(definition *Definition, input *Input, env []string) error
MapInput maps the values of input to their corresponding reference values.
func MapInput2 ¶ added in v0.3.0
func MapInput2(definition *Definition, input Input, env []string) error
MapInput2 iterates over all defined possible input in the definition, and attempts to set the value defined in the input, or the environment. In the process of mapping, the input will also be validated (i.e. missing required params or values will be identified, and values of the wrong type in the input or env will be identified).
Types ¶
type Application ¶
type Application struct { // The name of the application. Name string // The name of the application printed in usage information, defaults to the binary's filename. UsageName string // The version of the application. Version string // Application logo, shown in help output Logo string // Help message for the application. Help string // Writer to write output to. Writer io.Writer // contains filtered or unexported fields }
Application represents the heart of the console application. It is what orchestrates running commands, initiates input parsing, mapping, and validation; and will handle failure for each of those tasks.
func NewApplication ¶
func NewApplication(name string, version string) *Application
NewApplication creates a new Application with some sane defaults.
func (*Application) AddCommand ¶
func (a *Application) AddCommand(command *Command)
AddCommand adds a command to the application.
func (*Application) AddCommands ¶
func (a *Application) AddCommands(commands []*Command)
AddCommands adds commands to the application.
func (*Application) AddGlobalOption ¶ added in v0.3.0
func (a *Application) AddGlobalOption(definition OptionDefinition)
func (*Application) Commands ¶
func (a *Application) Commands() []*Command
Commands gets the sub-commands on an application.
type ArgumentDefinition ¶
type ArgumentDefinition struct { // The value to reference. Value parameters.Value // The specification of the argument. Spec string // The description of the argument. Desc string }
ArgumentDefinition is a struct that represents the entire configuration of a CLI argument.
type Command ¶
type Command struct { // The name of the command. Name string // An optional alias for the name, usually a shortened name. Alias string // The description of the command. Description string // Help message for the command. Help string // Function to configure command-level parameters. Configure ConfigureFunc // Function to execute when this command is requested. Execute ExecuteFunc // contains filtered or unexported fields }
Command represents a command to run in an application.
func (*Command) AddCommand ¶
AddCommand adds a sub-command to the command.
func (*Command) AddCommands ¶
AddCommands adds sub-commands to the command.
type CommandContainer ¶
type CommandContainer interface { // Commands gets commands from an object. Commands() []*Command }
CommandContainer is the interface that provides a method to get commands on an object.
type ConfigureFunc ¶
type ConfigureFunc func(*Definition)
ConfigureFunc is a function to mutate the input definition to add arguments and options.
type Definition ¶
type Definition struct {
// contains filtered or unexported fields
}
Definition represents the collected and configured input parameters of an application.
func NewDefinition ¶
func NewDefinition() *Definition
NewDefinition creates a new Definition with sensible defaults.
func (*Definition) AddArgument ¶
func (d *Definition) AddArgument(definition ArgumentDefinition)
AddArgument creates a parameters.Argument and adds it to the Definition. Duplicate argument names will result in an error.
func (*Definition) AddOption ¶
func (d *Definition) AddOption(definition OptionDefinition)
AddOption creates a parameters.Option and adds it to the Definition. Duplicate option names will result in an error.
func (*Definition) Arguments ¶
func (d *Definition) Arguments() []parameters.Argument
Arguments gets all of the arguments in this Definition.
func (*Definition) Options ¶
func (d *Definition) Options() []parameters.Option
Options gets all of the options in this Definition.
type ExecuteFunc ¶
ExecuteFunc is a function to perform whatever task this command does.
type Input ¶
type Input struct { Arguments []InputArgument Options []InputOption }
Input represents the raw application input, in a slightly more organised way, and provides helpers for retrieving that information. This allows commands to get application-wide input, instead of only the command-specific input.
func ParseInput ¶
ParseInput takes an array of strings (typically arguments to the application), and parses them into the raw Input type.
func ParseInput2 ¶ added in v0.3.0
func ParseInput2(definition *Definition, args []string) *Input
ParseInput2 takes the raw input, and regardless of what is actually defined in the definition, categorising the input as either arguments or options. In other words, the raw input is iterated over, not the definition's parameters. The definition is used so that we can identify options that should have values and consume the next argument as it's value.
func (*Input) GetOptionValue ¶
GetOptionValue gets the an option with one of the given names' value./
type InputArgument ¶
type InputArgument struct {
Value string
}
InputArgument represents the raw data parsed as arguments, really this is just the value.
type InputOption ¶
InputOption represents the raw data parsed as options. This includes it's name and it's value. The value can be "" if no value is given (i.e. if the option is a flag).
type OptionDefinition ¶
type OptionDefinition struct { // The value to reference. Value parameters.Value // The specification of the option. Spec string // The description of the option. Desc string // The name of an environment variable to read an option value from. EnvVar string }
OptionDefinition is a struct that represents the entire configuration of a CLI option.
type Output ¶
Output abstracts application output. This is mainly useful for testing, as a different writer can be passed to capture output in an easy to test manner.
func (*Output) Print ¶
Print uses the fmt package's Print with a pre-set writer. Spaces are always added between operands. It returns the number of bytes written and any write error encountered.
func (*Output) Printf ¶
Printf uses the fmt package's Printf with a pre-set writer. It returns the number of bytes written and any write error encountered.
func (*Output) Println ¶
Println uses the fmt package's Println with a pre-set writer. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.
func (*Output) SetExitCode ¶
SetExitCode sets the exit code to a specific int. By default, the exit code is set to 0.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package parameters contains everything related to creating typed parameters.
|
Package parameters contains everything related to creating typed parameters. |
Package specification is home to all parameter specification parsing.
|
Package specification is home to all parameter specification parsing. |