Documentation ¶
Overview ¶
Package gomcli provides a library to build interactive CLI interfaces in the style of msfconsole and riposte.
Index ¶
- Variables
- func Print(a ...interface{}) (n int, err error)
- func Printf(format string, a ...interface{}) (n int, err error)
- func Println(a ...interface{}) (n int, err error)
- type Command
- type Completer
- type ErrHandler
- type GomCLI
- func (c *GomCLI) AddCommand(cmd Command)
- func (c *GomCLI) Close()
- func (c *GomCLI) Commands() map[string]Command
- func (c *GomCLI) RemoveCommand(name string)
- func (c *GomCLI) SetCommands(cmds []Command)
- func (c *GomCLI) SetCtrlCAborts(aborts bool)
- func (c *GomCLI) SetExitOnCmdError(value bool)
- func (c *GomCLI) SetHistoryFile(path string)
- func (c *GomCLI) SetNotFoundHandler(function NotFoundHandler)
- func (c *GomCLI) SetPrompt(prompt string)
- func (c *GomCLI) Start() error
- func (c *GomCLI) StartWithInput(input string) error
- type NotFoundHandler
Constants ¶
This section is empty.
Variables ¶
var ErrCliCannotParseLine = errors.New("Cannot parse line")
ErrCliCannotParseLine is returned from Start or StartWithInput if the input provided could not be be parsed to form command and arguments.
var ErrCliCommandNotFound = errors.New("Command not found")
ErrCliCommandNotFound is passed to the notFoundHandler function if the input provided does not match any known command.
var ErrCliPromptAborted = errors.New("Prompt aborted")
ErrCliPromptAborted is returned from Start or StartWithInput when the user presses Ctrl-C, if CtrlCAborts was set to true in the Conf struct.
var ErrCmdArgOverflow = errors.New("Value too big")
ErrCmdArgOverflow is passed to ErrHandler when the value provided via CLI overflows the type of the corresponding argument for its defined Function.
var ErrCmdArgUnsupportedKind = errors.New("Unsupported Kind")
ErrCmdArgUnsupportedKind is passed to ErrHandler when the Kind of a Function's argument is not supported.
var ErrCmdInvalidArgs = errors.New("Invalid arguments")
ErrCmdInvalidArgs is passed to ErrHandler when the arguments provided via CLI for a Command cannot be converted to the argument types for its defined Function.
var ErrCmdMissingArgs = errors.New("Missing arguments")
ErrCmdMissingArgs is passed to ErrHandler when the number of arguments provided via CLI for a Command is less than the number of arguments for its defined Function.
Functions ¶
Types ¶
type Command ¶
type Command struct { Name string Function interface{} ErrHandler ErrHandler Completer Completer }
Command represents a function that can be executed via the CLI. Name defines the string that needs to be provided via the CLI to execute the Function. ErrHandler allows to handle errors when converting the input to arguments for the Function. Completer allows to provide completions for subcommands.
type Completer ¶
Completer takes a string and returns a list of completion candidates. It can be set for a given Command to indicate gomcli how to complete subcommands.
type ErrHandler ¶
ErrHandler takes a Command, an input string and a given error when parsing said Command, and returns an error to be propagated to GomCLI.Start, if needed. Otherwise, this is the point where the errors from CLI input for a Command are to be gracefully handled.
type GomCLI ¶
type GomCLI struct {
// contains filtered or unexported fields
}
GomCLI represents the state of the command-line interface, and is the main object to interact with within your program.
func New ¶
func New() *GomCLI
New initializes a new *GomCLI with sane defaults. Further configuration is to be performed via the setters. The terminal is set to raw mode by Liner's action, therefore to restore the terminal to its previous state, GomCLI.Close() needs to be called.
func (*GomCLI) AddCommand ¶
AddCommand adds a single Command to the CLI.
func (*GomCLI) Close ¶
func (c *GomCLI) Close()
Close stops the CLI processing, updating the history file if applicable and resetting the terminal into its previous mode.
func (*GomCLI) RemoveCommand ¶
RemoveCommand removes a specific Command from the CLI by name.
func (*GomCLI) SetCommands ¶
SetCommands replaces the current CLI set of Commands by a new slice.
func (*GomCLI) SetCtrlCAborts ¶
SetCtrlCAborts sets whether Start will return an ErrPromptAborted when Ctrl-C is pressed. The default is false (will not return when Ctrl-C is pressed).
func (*GomCLI) SetExitOnCmdError ¶
SetExitOnCmdError sets whether Start shall be interrupted and return the error as soon as an error unhandled in the Command.ErrHandler is propagated.
func (*GomCLI) SetHistoryFile ¶
SetHistoryFile sets the path for the command history file. If not set, no history file will be used. The history file has a fixed limit of 1000 entries.
func (*GomCLI) SetNotFoundHandler ¶
func (c *GomCLI) SetNotFoundHandler(function NotFoundHandler)
SetNotFoundHandler sets the function that will be called when the provided input does not match any known Command.
func (*GomCLI) SetPrompt ¶
SetPrompt sets the prompt for the CLI. Note that due to Liner's multi-platform nature, colored prompts are not supported.
func (*GomCLI) Start ¶
Start starts the CLI, iteratively displaying the prompt and handling user input until Close is called or an error is returned during user input processing.
func (*GomCLI) StartWithInput ¶
StartWithInput starts the CLI by providing initial input that will be split into lines and, if applicable, into commands.
type NotFoundHandler ¶
NotFoundHandler is a function that indicates gomcli how to handle input that does not match any known Command. If not set, default action is to ignore it. An error can be returned, that will be propagated so that it is returned by Start, terminating the CLI.