Documentation ¶
Index ¶
- 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
Constants ¶
const ( // RunResultHelp is a value that can be returned from Run to signal // to the CLI to render the help output. RunResultHelp = -18511 )
Variables ¶
var ( UiColorNone UiColor = UiColor{noColor, false} UiColorRed = UiColor{int(color.FgHiRed), false} UiColorGreen = UiColor{int(color.FgHiGreen), false} UiColorYellow = UiColor{int(color.FgHiYellow), false} UiColorBlue = UiColor{int(color.FgHiBlue), false} UiColorMagenta = UiColor{int(color.FgHiMagenta), false} UiColorCyan = UiColor{int(color.FgHiCyan), false} )
A list of colors that are useful. These are all non-bolded by default.
Functions ¶
This section is empty.
Types ¶
type BasicUi ¶
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.
type CLI ¶
type CLI struct { // Args is the list of command-line arguments received excluding // the name of the app. For example, if the command "./cli foo bar" // was invoked, then Args should be []string{"foo", "bar"} Args []string // Commands is a mapping of subcommands names to a factory function // for creating that 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 ankh.CLI // instalnnce must be access with "./cli foo bar". See the docs for CLI for // notes on how this changes some other behavior of the CLI as well. // // The factory should be as cheap as possible, ideally only allocating // a struct. The factory may be called multiple times in the course // of a command execution and certain events such as help require the instantiation // of all commands. Expensive initiliazation should be deferred to function // calls within the interface implementation Commands map[string]CommandFactory // HiddenCommands is a list of commands that are "hidden". Hidden // commands are not given to the help function callback and do not show up // in autocomplete. The values in the slice should be equivalent // to the Keys in the command map. HiddenCommands []string // Name defines the name of the CLI. Name string // Version of the CLI Version string // Autocomplete enables or disables subcommand auto-completion support. // This is enabled by default when NewCLI is called. Otherwise, this // must be enabled explicitely. // // Autocomplete requires the "Name" option to be set on CLI. This name // should be set exactly to the binary name that is autocompleted. // // AutocompleteInstall and AutocompleteUninstall are the global flag // names for installing and uninstalling the autocompletion handlers // for the user's shell. The flag should omit the hyphen(s) in front of // the value. Both single and double hyphens will automatically be supported // for the flag name. These default to `autocompelete-install`and // `autocomplete-uninstall` respectively. // // AutocompleteNoDefaultFlags is a boolean which contronls if the default auto- // complete flags like -help and -version are added to the output. // // AutoCompleteGlobalFlags are mapping of global flags for autocompletion. // The help and version flags are automatically added. Autocomplete bool AutocompleteInstall string AutocompleteUninstall string AutocompleteNoDefaultFlags bool AutocompleteGlobalFlags complete.Flags // HelpFunc is the function called to generate the generic help // text that is show if help must be shown for the CLI that doe'sn' // pertain to a specific command. HelpFunc HelpFunc // HelpWriter is used to print help text and version when requested. // Defaults to os.Stderr for backwards compatibility. // It is recommended that you set HelpWriter to os.Stdout, and // ErrorWriter to os.Stderr HelpWriter io.Writer // ErrorWriter is used to output errors when a command can not be run. // Defaults to the value of HelpWriter for backward compatibility. // It is recommended that you set HelpWriter to os.Stdout, and // ErrorWriter to os.Stderr. ErrorWriter io.Writer // contains filtered or unexported fields }
Ankh contains the state necessary to run subcommands and parse the command line arguments.
Ankn also supports nested subcommands, such as "ankh foo bar". To use nested subcommands, the key in the Commands mapping below contains thee full subcommand. In this example, it would be "foo bar".
If you use a Ankh with nested 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 "ankh 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 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 ankh.CLI instance itself has no subcommands, this entire section is omitted.
Any parent commands that don't exists are automatically created as No-op commnds that just show help for other subcommands. For example, if you only register "foo bar", then "foo" is automatically created.
func (*CLI) IsVersion ¶
isVersion returns whethere or not the version flag is present within the arguments.
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 ¶
SubcommandArgs returns the arguments that will be passed to the subcommand.
type ColoredUi ¶
type ColoredUi struct { OutputColor UiColor InfoColor UiColor ErrorColor UiColor WarnColor UiColor Ui Ui }
ColoredUid is a Ui implementation that colors its output according to the given color schemes for the given types of output
type Command ¶
type Command interface { // Help should return long-form help text that includes the command-line // usage, a brief few sentences explainaing the function of the command // and the complete list of flags the command accepts. Help() string // Run should run the actual command with the given CLI instance and // command-line arguments. It should return the exit status when it is // finished. // // There are a handful of special exit codes this can return documented // above that change behavior. Run(args []string) int // Synopsi should return a one-line, short synopsis of the command. // This should be less than 50 characters ideally. Synopsis() string }
A command is a runnable sub-command of a CLI.
type CommandAutoComplete ¶
type CommandAutoComplete interface { // AutocompleteArgs return the argument predictor for this command. // If argument completion is not supported, this should return // complete. PredictNothing. AutocompleteArgs() complete.Predictor // AutoCompleteFlags returns a mapping of supported flags and autocomplete // options for this command. The map key for the Flags map should be the // complete flag such as "-foo" or "--foo". AutoCompleteFlags() complete.Flags }
CommandAutoComplete is an extension of Command that enables fine-grained autocompletion. Subcommand autocompletion will work even if tis interface is not implemented. By implementing this interface, more advanced autocompletion is enabled.
type CommandFactory ¶
CommandFactory is a type of function that is a factory for commands. We need a factory because we way need to setup some state on the struct that implements the command itself
type CommandHelpTemplate ¶
type CommandHelpTemplate interface { // HelpTemplate is the template in text/template format to use for // displaying the Help. The keys available are: // // * ", Help" - The help text itself // * ".Subcommands" // HelpTemplate() 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 ahdn HelpTemplate should be implemented.
If CommandHelpTemplate isn't implemented, the Help is output as-is.
type ConcurrentUi ¶
type ConcurrentUi struct { Ui Ui // contains filtered or unexported fields }
ConcurrentUi is a wrapper around a Ui interface (and implements) that interface) making the underlying Ui conncurrency safe.
func (*ConcurrentUi) Error ¶
func (u *ConcurrentUi) Error(message string)
func (*ConcurrentUi) Info ¶
func (u *ConcurrentUi) Info(message string)
func (*ConcurrentUi) Output ¶
func (u *ConcurrentUi) Output(message string)
func (*ConcurrentUi) Warn ¶
func (u *ConcurrentUi) Warn(message string)
type HelpFunc ¶
type HelpFunc func(map[string]CommandFactory) string
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 BasicHelpFunc ¶
BasicHelpFunc generates some basic help output that is usually good enough for most CLI applications.
func FilteredHelpFunc ¶
FilteredHelpFunc will filter the commands to only include the keys in the include parameter.
type MockCommand ¶
type MockCommand struct { // Settable HelpText string RunResult int SynopsisText string // Set by the command RunCalled bool RunArgs []string }
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 (c *MockCommand) Help() string
func (*MockCommand) Run ¶
func (c *MockCommand) Run(args []string) int
func (*MockCommand) Synopsis ¶
func (c *MockCommand) Synopsis() string
type MockCommandAutocomplete ¶
type MockCommandAutocomplete struct { MockCommand // Settable AutocompleteArgsValue complete.Predictor AutocompleteFlagsValue complete.Flags }
MockCommandAutoComplete is an implementation of CommandAutoComplete
func (*MockCommandAutocomplete) AutoCompleteArgs ¶
func (c *MockCommandAutocomplete) AutoCompleteArgs() complete.Predictor
func (*MockCommandAutocomplete) AutocompleteFlags ¶
func (c *MockCommandAutocomplete) AutocompleteFlags() complete.Flags
type MockCommandHelpTemplate ¶
type MockCommandHelpTemplate struct { MockCommand // Settable HelpTemplateText string }
MockCommandHelpTemplate is an implementation of CommandHelpTemplate
func (*MockCommandHelpTemplate) HelpTemplate ¶
func (c *MockCommandHelpTemplate) HelpTemplate() string
type MockUi ¶
type MockUi struct { InputReader io.Reader ErrorWriter *syncBuffer OutputWriter *syncBuffer // contains filtered or unexported fields }
MockUi is a mock UI taht is used for tests and is exported publicly for use in external tests if needed as well. Do not instantiate 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(ankh.MockUi)/ankh.NewMockUi()/g' *_test.o
type PrefixedUi ¶
type PrefixedUi struct { AskPrefix string AskSecretPrefix string OutputPrefix string InfoPrefix string ErrorPrefix string WarnPrefix string Ui Ui }
func (*PrefixedUi) Error ¶
func (u *PrefixedUi) Error(message string)
func (*PrefixedUi) Info ¶
func (u *PrefixedUi) Info(message string)
func (*PrefixedUi) Output ¶
func (u *PrefixedUi) Output(message string)
func (*PrefixedUi) Warn ¶
func (u *PrefixedUi) Warn(message string)
type Ui ¶
type Ui interface { // Ask the user for input using the given query. The response is // returned as the given string, or an error Ask(string) (string, error) // Ask secret asks the user for input using the given query, but // does not echo the keystrokes to the terminal AskSecret(string) (string, error) // 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 // the UI impementors 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 error 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 does'nt have to be used, but helps provide a simple, layerable way to manage user interactions