Documentation
¶
Overview ¶
Package cli provides a clean, minimal and simple mechanism for constructing CLI commands.
Index ¶
- type ArgOption
- type Builder
- type Command
- type FlagOption
- type Option
- func Arg[T arg.Argable](p *T, name, usage string, options ...ArgOption[T]) Option
- func BuildDate(date string) Option
- func Commit(commit string) Option
- func Example(comment, command string) Option
- func Flag[T flag.Flaggable](target *T, name string, short rune, usage string, options ...FlagOption[T]) Option
- func Long(long string) Option
- func NoColour(noColour bool) Option
- func OverrideArgs(args []string) Option
- func Run(run func(ctx context.Context, cmd *Command) error) Option
- func Short(short string) Option
- func Stderr(stderr io.Writer) Option
- func Stdin(stdin io.Reader) Option
- func Stdout(stdout io.Writer) Option
- func SubCommands(builders ...Builder) Option
- func Version(version string) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArgOption ¶ added in v0.17.0
ArgOption is a functional option for configuring an Arg.
func ArgDefault ¶ added in v0.17.0
ArgDefault is a cli.ArgOption that sets the default value for a positional argument.
By default, positional arguments are required, but by providing a default value via this option, you mark the argument as not required.
If a default is given and the argument is not provided via the command line, the default is used in its place.
type Builder ¶
Builder is a function that constructs and returns a Command, it makes constructing complex command trees easier as they can be passed directly to the SubCommands option.
type Command ¶
type Command struct {
// contains filtered or unexported fields
}
Command represents a CLI command.
Commands in cli are recursive, that means that the root command is not different or special compared to any of its subcommands, this structure makes defining complex command trees as simple as creating a single command.
In the command line 'git commit -m "Message"' both 'git' and 'commit' would be a clio command, '-m' would be a Flag taking a string argument.
Commands are constructed with the New function and customised by providing a number of functional options to layer different settings and functionality.
func New ¶
New builds and returns a new Command.
The command can be customised by passing in a number of options enabling you to do things like configure stderr and stdout, add or customise help or version output add subcommands and run functions etc.
Without any options passed, the default implementation returns a Command with no subcommands, a -v/--version and a -h/--help flag, hooked up to os.Stdin, os.Stdout and os.Stderr and accepting arbitrary positional arguments from os.Args (with the command path stripped, equivalent to os.Args[1:]).
Options will validate their inputs where possible and return errors which will be bubbled up through New to aid debugging invalid configuration.
func (*Command) Args ¶ added in v0.17.0
Args returns the positional arguments passed to the command.
func (*Command) Execute ¶
Execute parses the flags and arguments, and invokes the Command's Run function, returning any error.
If the flags fail to parse, an error will be returned and the Run function will not be called.
func (*Command) ExtraArgs ¶
ExtraArgs returns any additional arguments following a "--", and a boolean indicating whether or not they were present. This is useful for when you want to implement argument pass through in your commands.
If there were no extra arguments, it will return nil, false.
type FlagOption ¶ added in v0.18.0
FlagOption is a functional option for configuring a Flag.
func FlagDefault ¶ added in v0.18.0
func FlagDefault[T flag.Flaggable](value T) FlagOption[T]
FlagDefault is a cli.FlagOption that sets the default value for command line flag.
By default, a flag's default value is the zero value for its type. But using this option, you may set a non-zero default value that the flag should inherit if not provided on the command line.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is a functional option for configuring a Command.
func Arg ¶ added in v0.17.0
Arg is an Option that adds a typed argument to a Command, storing its value in a variable via its pointer 'target'.
The variable is set when the argument is parsed during command execution.
Args linked to slice values (e.g. []string) must be defined last as they eagerly consume all remaining command line arguments.
The argument may be given a default value with the ArgDefault option. Without this option the argument will be required, i.e. failing to provide it on the command line is an error, but when a default is given and the value omitted on the command line, the default is used in its place.
// Add an int arg that defaults to 1
var number int
cli.New("add", cli.Arg(&number, "number", "Add a number", cli.ArgDefault(1)))
func BuildDate ¶
BuildDate is an Option that sets the build date for a binary built with CLI. It is particularly useful for embedding rich version info into a binary using ldflags
Without this option, the build date is simply omitted from the version info shown when -v/--version is called.
If set to a non empty string, the build date will be shown.
cli.New("test", cli.BuildDate("2024-07-06T10:37:30Z"))
func Commit ¶
Commit is an Option that sets the commit hash for a binary built with CLI. It is particularly useful for embedding rich version info into a binary using ldflags.
Without this option, the commit hash is simply omitted from the version info shown when -v/--version is called.
If set to a non empty string, the commit hash will be shown.
cli.New("test", cli.Commit("b43fd2c"))
func Example ¶
Example is an Option that adds an example to a Command.
Examples take the form of an explanatory comment and a command showing the command to the CLI, these will show up in the help text.
For example, a program called "myrm" that deletes files and directories might have an example declared as follows:
cli.Example("Delete a folder recursively without confirmation", "myrm ./dir --recursive --force")
Which would show up in the help text like so:
Examples: # Delete a folder recursively without confirmation $ myrm ./dir --recursive --force
An arbitrary number of examples can be added to a Command, and calls to Example are additive.
func Flag ¶
func Flag[T flag.Flaggable](target *T, name string, short rune, usage string, options ...FlagOption[T]) Option
Flag is an Option that adds a typed flag to a Command, storing its value in a variable via its pointer 'target'.
The variable is set when the flag is parsed during command execution. By default, the flag will assume the zero value for its type, the default may be provided explicitly using the FlagDefault option.
If the default value is not the zero value for the type T, the flags usage message will show the default value in the commands help text.
To add a long flag only (e.g. --delete with no -d option), pass [NoShortHand] for "short".
Flags linked to slice values (e.g. []string) work by appending the passed values to the slice so multiple values may be given by repeat usage of the flag e.g. --items "one" --items "two".
// Add a force flag
var force bool
cli.New("rm", cli.Flag(&force, "force", 'f', "Force deletion without confirmation"))
func Long ¶
Long is an Option that sets the full description for a Command.
The long description will appear in the help text for a command. Users are responsible for wrapping the text at a sensible width.
For consistency of formatting, all leading and trailing whitespace is stripped.
Successive calls will simply overwrite any previous calls.
cli.New("rm", cli.Long("... lots of text here"))
func NoColour ¶
NoColour is an Option that disables all colour output from the Command.
CLI respects the values of $NO_COLOR and $FORCE_COLOR automatically so this need not be set for most applications.
Setting this option takes precedence over all other colour configuration.
func OverrideArgs ¶
OverrideArgs is an Option that sets the arguments for a Command, overriding any arguments parsed from the command line.
Without this option, the command will default to os.Args[1:], this option is particularly useful for testing.
Successive calls override previous ones.
// Override arguments for testing
cli.New("test", cli.OverrideArgs([]string{"test", "me"}))
func Run ¶
Run is an Option that sets the run function for a Command.
The run function is the actual implementation of your command i.e. what you want it to do when invoked.
Successive calls overwrite previous ones.
func Short ¶
Short is an Option that sets the one line usage summary for a Command.
The one line usage will appear in the help text as well as alongside subcommands when they are listed.
For consistency of formatting, all leading and trailing whitespace is stripped.
Successive calls will simply overwrite any previous calls.
cli.New("rm", cli.Short("Delete files and directories"))
func Stderr ¶
Stderr is an Option that sets the Stderr for a Command.
Successive calls will simply overwrite any previous calls. Without this option the command will default to os.Stderr.
// Set stderr to a temporary buffer
buf := &bytes.Buffer{}
cli.New("test", cli.Stderr(buf))
func Stdin ¶
Stdin is an Option that sets the Stdin for a Command.
Successive calls will simply overwrite any previous calls. Without this option the command will default to os.Stdin.
// Set stdin to os.Stdin (the default anyway)
cli.New("test", cli.Stdin(os.Stdin))
func Stdout ¶
Stdout is an Option that sets the Stdout for a Command.
Successive calls will simply overwrite any previous calls. Without this option the command will default to os.Stdout.
// Set stdout to a temporary buffer
buf := &bytes.Buffer{}
cli.New("test", cli.Stdout(buf))
func SubCommands ¶
SubCommands is an Option that attaches 1 or more subcommands to the command being configured.
Sub commands must have unique names, any duplicates will result in an error.
This option is additive and can be called as many times as desired, subcommands are effectively appended on every call.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package arg provides mechanisms for defining and configuring command line arguments.
|
Package arg provides mechanisms for defining and configuring command line arguments. |
|
examples
|
|
|
cancel
command
Package cancel demonstrates how to handle CTRL+C and cancellation/timeouts easily with cli.
|
Package cancel demonstrates how to handle CTRL+C and cancellation/timeouts easily with cli. |
|
cover
command
Package cover is a simple CLI demonstrating the core features of this library.
|
Package cover is a simple CLI demonstrating the core features of this library. |
|
namedargs
command
Package namedargs demonstrates how to use named positional arguments in cli.
|
Package namedargs demonstrates how to use named positional arguments in cli. |
|
quickstart
command
Package quickstart demonstrates a quickstart example for cli.
|
Package quickstart demonstrates a quickstart example for cli. |
|
subcommands
command
Package subcommands demonstrates how to build a CLI with a full command structure.
|
Package subcommands demonstrates how to build a CLI with a full command structure. |
|
Package flag provides mechanisms for defining and configuring command line flags.
|
Package flag provides mechanisms for defining and configuring command line flags. |
|
internal
|
|
|
arg
Package arg provides a command line arg definition and parsing library.
|
Package arg provides a command line arg definition and parsing library. |
|
constraints
Package constraints provides generic constraints for cli.
|
Package constraints provides generic constraints for cli. |
|
flag
Package flag provides a command line flag definition and parsing library.
|
Package flag provides a command line flag definition and parsing library. |
|
format
Package format is the inverse of parse.
|
Package format is the inverse of parse. |
|
parse
Package parse provides functions to parse strings into Go types and produce detailed, consistent errors.
|
Package parse provides functions to parse strings into Go types and produce detailed, consistent errors. |
|
style
Package style simply provides a uniform terminal printing style via hue for use across the library.
|
Package style simply provides a uniform terminal printing style via hue for use across the library. |
