Documentation
¶
Overview ¶
Package cliutils provides utilities for building command-line interfaces.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HelpRequested ¶
HelpRequested reads the argv and returns whether it contains one of `-h`, `--help`, in any position, or `help` as the first element in the vector. If this happens a subcommand should invoke its own help method to print help.
Types ¶
type Command ¶
type Command interface { // Help prints the help for the command on the stdout. Help(env Environment, argv ...string) error // Main executes the command main function. Main(ctx context.Context, env Environment, argv ...string) error }
Command is an rbmk command-line command.
type CommandWithSubCommands ¶
type CommandWithSubCommands struct {
// contains filtered or unexported fields
}
CommandWithSubCommands is a Command that contains subcommands.
It works as follows:
1. It automatically handles invocation with no arguments by printing help.
2. It handles `-h` and `--help` by printing help.
3. It handles `help [COMMAND...]` by printing help either for the command itself or for the selected subcommmand.
4. It handles `COMMAND...` by redirecting execution to the subcommand.
Construct using NewCommandWithSubCommands.
func NewCommandWithSubCommands ¶
func NewCommandWithSubCommands(name string, renderer LazyHelpRenderer, commands map[string]Command) CommandWithSubCommands
NewCommandWithSubCommands constructs a CommandWithSubCommands.
The name argument contains the full name of this command (e.g., `rbmk run`).
The renderer argument renders the help on demand.
The commands argument contains the implemented subcommands.
func (CommandWithSubCommands) Help ¶
func (c CommandWithSubCommands) Help(env Environment, argv ...string) error
Help implements Command.
func (CommandWithSubCommands) Main ¶
func (c CommandWithSubCommands) Main(ctx context.Context, env Environment, argv ...string) error
Main implements Command.
type Environment ¶ added in v0.10.0
type Environment interface { // FS returns the virtual filesystem to use. FS() fsx.FS // Stdin returns the stdin reader to use. Stdin() io.Reader // Stderr returns the stderr writer to use. Stderr() io.Writer // Stdout returns the stdout writer to use. Stdout() io.Writer }
Environment is the environment for executing a Command.
type LazyHelpRenderer ¶ added in v0.12.0
type LazyHelpRenderer interface {
Help() string
}
LazyHelpRenderer renders the help possibly adding colours and formatting.
type LazyHelpRendererFunc ¶ added in v0.12.0
type LazyHelpRendererFunc func() string
LazyHelpRendererFunc is a function that implements LazyHelpRenderer.
func (LazyHelpRendererFunc) Help ¶ added in v0.12.0
func (fx LazyHelpRendererFunc) Help() string
Help implements HelpRenderer.
type StandardEnvironment ¶ added in v0.10.0
type StandardEnvironment struct{}
StandardEnvironment is the standard implementation of Environment.
func (StandardEnvironment) FS ¶ added in v0.13.0
func (se StandardEnvironment) FS() fsx.FS
FS implements Environment.
func (StandardEnvironment) Stderr ¶ added in v0.10.0
func (se StandardEnvironment) Stderr() io.Writer
Stderr implements Environment.
func (StandardEnvironment) Stdin ¶ added in v0.11.0
func (se StandardEnvironment) Stdin() io.Reader
Stdin implements Environment.
func (StandardEnvironment) Stdout ¶ added in v0.10.0
func (se StandardEnvironment) Stdout() io.Writer
Stdout implements Environment.