cli package - github.com/hashicorp/cli - Go Packages (original) (raw)
- Constants
- Variables
- type BasicUi
- type CLI
- type ColoredUi
- type Command
- type CommandAutocomplete
- type CommandFactory
- type CommandHelpTemplate
- type ConcurrentUi
- type HelpFunc
- type MockCommand
- type MockCommandAutocomplete
- type MockCommandHelpTemplate
- type MockUi
- type PrefixedUi
- type Ui
- type UiColor
- type UiWriter
A list of colors that are useful. These are all non-bolded by default.
This section is empty.
BasicUi is an implementation of Ui that just outputs to the given writer. This UI is not threadsafe by default, but you can wrap it in a ConcurrentUi to make it safe.
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.
func NewCLI(app, version string) *CLI
NewClI returns a new CLI instance with sensible defaults.
IsHelp returns whether or not the help flag is present within the arguments.
func (c *CLI) IsVersion() bool
IsVersion returns whether or not the version flag is present within the arguments.
Run runs the actual CLI based on the arguments given.
func (*CLI) Subcommand ¶
Subcommand returns the subcommand that the CLI would execute. For example, a CLI from "--version version --help" would return a Subcommand of "version"
func (*CLI) SubcommandArgs ¶
func (c *CLI) SubcommandArgs() []string
SubcommandArgs returns the arguments that will be passed to the subcommand.
type ColoredUi struct { OutputColor UiColor InfoColor UiColor ErrorColor UiColor WarnColor UiColor Ui Ui }
ColoredUi is a Ui implementation that colors its output according to the given color schemes for the given type of output.
type Command ¶
A command is a runnable sub-command of a CLI.
type CommandAutocomplete ¶
CommandAutocomplete is an extension of Command that enables fine-grained autocompletion. Subcommand autocompletion will work even if this interface is not implemented. By implementing this interface, more advanced autocompletion is enabled.
type CommandFactory ¶
type CommandFactory func() (Command, error)
CommandFactory is a type of function that is a factory for commands. We need a factory because we may need to setup some state on the struct that implements the command itself.
type CommandHelpTemplate ¶
type CommandHelpTemplate interface {
HelpTemplate() [string](/builtin#string)}
CommandHelpTemplate is an extension of Command that also has a function for returning a template for the help rather than the help itself. In this scenario, both Help and HelpTemplate should be implemented.
If CommandHelpTemplate isn't implemented, the Help is output as-is.
type ConcurrentUi struct { Ui Ui
}
ConcurrentUi is a wrapper around a Ui interface (and implements that interface) making the underlying Ui concurrency safe.
HelpFunc is the type of the function that is responsible for generating the help output when the CLI must show the general help text.
BasicHelpFunc generates some basic help output that is usually good enough for most CLI applications.
func FilteredHelpFunc(include []string, f HelpFunc) HelpFunc
FilteredHelpFunc will filter the commands to only include the keys in the include parameter.
type MockCommand ¶
MockCommand is an implementation of Command that can be used for tests. It is publicly exported from this package in case you want to use it externally.
func (*MockCommand) Help ¶
func (*MockCommand) Run ¶
type MockCommandHelpTemplate ¶
type MockCommandHelpTemplate struct { MockCommand
HelpTemplateText [string](/builtin#string)}
MockCommandHelpTemplate is an implementation of CommandHelpTemplate.
type MockUi struct { InputReader io.Reader ErrorWriter *syncBuffer OutputWriter *syncBuffer
}
MockUi is a mock UI that is used for tests and is exported publicly for use in external tests if needed as well. Do not instantite this directly since the buffers will be initialized on the first write. If there is no write then you will get a nil panic. Please use the NewMockUi() constructor function instead. You can fix your code with
sed -i -e 's/new(cli.MockUi)/cli.NewMockUi()/g' *_test.go
NewMockUi returns a fully initialized MockUi instance which is safe for concurrent use.
func (u *MockUi) Output(message string)
PrefixedUi is an implementation of Ui that prefixes messages.
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.
type UiColor struct { Code int Bold bool }
UiColor is a posix shell color code to use.
type UiWriter struct { Ui Ui }
UiWriter is an io.Writer implementation that can be used with loggers that writes every line of log output data to a Ui at the Info level.