Documentation ¶
Index ¶
- Constants
- type Args
- type CLI
- type Command
- type Config
- type HelpFunc
- type MockCommand
- type NamedCommand
- type Router
- func (r *Router) AddCommand(n string, cmd Command) error
- func (r *Router) AddMissingParents(genCmd func() Command)
- func (r *Router) GetCommand(n string) (Command, error)
- func (r *Router) GetLongestPrefix(s string) (string, interface{}, bool)
- func (r *Router) GetParent(n string) string
- func (r *Router) GetSubcommands(prefix string) map[string]Command
- type SimpleUI
- type UI
Constants ¶
const (
CommandReturnCodeHelp = -1234567
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Args ¶
type Args struct {
// contains filtered or unexported fields
}
Args contains parsed input to the CLI
type CLI ¶
type CLI struct {
// contains filtered or unexported fields
}
CLI contains the state necessary to run subcommands and parse the command line arguments.
CLI also supports nested subcommands, such as "cli foo bar". To use nested subcommands, the key in the Commands mapping below contains the full subcommand. In this example, it would be "foo bar".
If you use a CLI with nested subcommands, some semantics change due to ambiguities:
We use longest prefix matching to find a matching subcommand. This means if you register "foo bar" and the user executes "cli foo qux", the "foo" command will be executed with the arg "qux". It is up to you to handle these args. One option is to just return the special help return code `RunResultHelp` to display help and exit.
The help flag "-h" or "-help" will look at all args to determine the help function. For example: "otto apps list -h" will show the help for "apps list" but "otto apps -h" will show it for "apps". In the normal CLI, only the first subcommand is used.
The help flag will list any subcommands that a command takes as well as the command's help itself. If there are no subcommands, it will note this. If the CLI itself has no subcommands, this entire section is omitted.
Any parent commands that don't exist are automatically created as no-op commands that just show help for other subcommands. For example, if you only register "foo bar", then "foo" is automatically created.
type Command ¶
type Command interface { // Help returns detailed information about a command Help() string // Synopsis returns a one-line synopsis of the command // This should be less than 50 characters ideally. Synopsis() string // Run runs the actual command with the given CLI instance and // command-line arguments, returning an exit status Run(ctx context.Context, args []string) int }
A Command is a runnable sub-command of a CLI
type Config ¶
type Config struct { // Name of the CLI app Name string // Version of the CLI app Version string // Commands is a mapping of subcommand names to a Command implementation. // If there is a command with a blank string "", then it will be used as // the default command if no subcommand is specified. // // If the key has a space in it, this will create a nested subcommand. // For example, if the key is "foo bar", then to access it our CLI // must be accessed with "./cli foo bar". Commands map[string]Command // HelpFunc is the function called to generate the global help text. HelpFunc HelpFunc // HelpWriter is the Writer where the help text is outputted to. If // not specified, it will default to Stderr HelpWriter io.Writer }
CLI configurations
type HelpFunc ¶
HelpFunc is the type of the function that is responsible for generating the help output when the CLI must show the general help text
func DefaultHelpFunc ¶
DefaultHelpFunc is the default function for generating help output
type MockCommand ¶
type MockCommand struct { HelpText string SynopsisText string RunReturnCode int // Set by the command RunArgs []string RunCalled bool }
MockCommand is an implementation of Command that can be used for testing and for automatically populating missing parent commands.
func (*MockCommand) Help ¶
func (c *MockCommand) Help() string
func (*MockCommand) Synopsis ¶
func (c *MockCommand) Synopsis() string
type NamedCommand ¶
A NamedCommand is a runnable sub-command of a CLI with a name
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is used to resolve names into commands. It is automatically generated based when the CLI struct is created.
func (*Router) AddCommand ¶
AddCommand adds a new command to the router.
func (*Router) AddMissingParents ¶
AddMissingParents ensures that every registered command has a parent.
func (*Router) GetCommand ¶
GetCommand retrieves the command assosiated with the given name.
func (*Router) GetLongestPrefix ¶
GetLongestPrefix return the longest prefix match among all commands registered in the router, considering the query string passed as argument
type UI ¶
type UI interface { // Output is called for normal standard output. Output(string) // Info is called for information related to the previous output. // In general this may be the exact same as Output, but this gives // Ui implementors some flexibility with output formats. Info(string) // Error is used for any error messages that might appear on standard // error. Error(string) // Warn is used for any warning messages that might appear on standard // error. Warn(string) }
UI is an interface for interacting with the terminal, or "interface" of a CLI. This abstraction doesn't have to be used, but helps provide a simple, layerable way to manage user interactions.