Documentation ¶
Overview ¶
Package ishell implements an interactive shell.
Index ¶
- type Actions
- type Cmd
- type Context
- type Shell
- func (s *Shell) Active() bool
- func (s *Shell) AddCmd(cmd *Cmd)
- func (s *Shell) AutoHelp(enable bool)
- func (s *Shell) CustomCompleter(completer readline.AutoCompleter)
- func (s *Shell) DeleteCmd(name string)
- func (s *Shell) IgnoreCase(ignore bool)
- func (s *Shell) Interrupt(f func(*Context))
- func (s *Shell) NotFound(f func(*Context))
- func (s *Shell) SetHistoryPath(path string) error
- func (s *Shell) SetHomeHistoryPath(path string)
- func (s *Shell) SetOut(writer io.Writer)
- func (s *Shell) Start()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Actions ¶
type Actions interface { // ReadLine reads a line from standard input. ReadLine() string // ReadPassword reads password from standard input without echoing the characters. // Note that this only works as expected when the standard input is a terminal. ReadPassword() string // ReadMultiLinesFunc reads multiple lines from standard input. It passes each read line to // f and stops reading when f returns false. ReadMultiLinesFunc(f func(string) bool) string // ReadMultiLines reads multiple lines from standard input. It stops reading when terminator // is encountered at the end of the line. It returns the lines read including terminator. // For more control, use ReadMultiLinesFunc. ReadMultiLines(terminator string) string // Println prints to output and ends with newline character. Println(val ...interface{}) // Print prints to output. Print(val ...interface{}) // Printf prints to output using string format. Printf(format string, val ...interface{}) // ShowPaged shows a paged text that is scrollable. // This leverages on "less" for unix and "more" for windows. ShowPaged(text string) error // SetPrompt sets the prompt string. The string to be displayed before the cursor. SetPrompt(prompt string) // SetMultiPrompt sets the prompt string used for multiple lines. The string to be displayed before // the cursor; starting from the second line of input. SetMultiPrompt(prompt string) // ShowPrompt sets whether prompt should show when requesting input for ReadLine and ReadPassword. // Defaults to true. ShowPrompt(show bool) // Cmds returns all the commands added to the shell. Cmds() []*Cmd // HelpText returns the computed help of top level commands. HelpText() string // ClearScreen clears the screen. Same behaviour as running 'clear' in unix terminal or 'cls' in windows cmd. ClearScreen() error // Stop stops the shell. This will stop the shell from auto reading inputs and calling // registered functions. A stopped shell is only inactive but totally functional. // Its functions can still be called. Stop() }
Actions are actions that can be performed by a shell.
type Cmd ¶
type Cmd struct { // Command name. Name string // Function to execute for the command. Func func(c *Context) // One liner help message for the command. Help string // More descriptive help message for the command. LongHelp string // Completer is custom autocomplete for command. // It takes in command arguments and returns // autocomplete options. // By default all commands get autocomplete of // subcommands. // A non-nil Completer overrides the default behaviour. Completer func(args []string) []string // contains filtered or unexported fields }
Cmd is a shell command handler.
type Context ¶
type Context struct { // Args is command arguments. Args []string // Cmd is the currently executing command. This is empty for NotFound and Interrupt. Cmd Cmd Actions // contains filtered or unexported fields }
Context is an ishell context. It embeds ishell.Actions.
type Shell ¶
type Shell struct { Actions // contains filtered or unexported fields }
Shell is an interactive cli shell.
func New ¶
func New() *Shell
New creates a new shell with default settings. Uses standard output and default prompt ">> ".
func (*Shell) AutoHelp ¶
AutoHelp sets if ishell should trigger help message if a command's arg is "help". Defaults to true.
This can be set to false for more control on how help is displayed.
func (*Shell) CustomCompleter ¶
func (s *Shell) CustomCompleter(completer readline.AutoCompleter)
CustomCompleter allows use of custom implementation of readline.Autocompleter.
func (*Shell) IgnoreCase ¶
IgnoreCase specifies whether commands should not be case sensitive. Defaults to false i.e. commands are case sensitive. If true, commands must be registered in lower cases. e.g. shell.Register("cmd", ...)
func (*Shell) NotFound ¶
NotFound adds a generic function for all inputs. It is called if the shell input could not be handled by any of the added commands.
func (*Shell) SetHistoryPath ¶
SetHistoryPath sets where readlines history file location. Use an empty string to disable history file. It is empty by default.
func (*Shell) SetHomeHistoryPath ¶
SetHomeHistoryPath is a convenience method that sets the history path in user's home directory.