Skip to content

Latest commit

 

History

History
177 lines (150 loc) · 3.55 KB

README.md

File metadata and controls

177 lines (150 loc) · 3.55 KB

ishell

ishell is an interactive shell library for creating interactive cli applications.

Documentation

Older version

The current master is not backward compatible with older version. Kindly change your import path to gopkg.in/abiosoft/ishell.v1.

Older version of this library is still available at https://gopkg.in/abiosoft/ishell.v1.

Usage

import "strings"
import "github.com/abiosoft/ishell"

func main(){
    // create new shell.
    // by default, new shell includes 'exit', 'help' and 'clear' commands.
    shell := ishell.New()

	// display welcome info.
	shell.Println("Sample Interactive Shell")

	// register a function for "greet" command.
    shell.AddCmd(&ishell.Cmd{
		Name: "greet",
		Help: "greet user",
		Func: func(c *ishell.Context) {
			name := "Stranger"
			if len(c.Args) > 0 {
				name = strings.Join(c.Args, " ")
			}
			c.Println("Hello", name)
		},
	})

	// start shell
	shell.Start()
}

Execution

Sample Interactive Shell
>>> help

Commands:
  clear      clear the screen
  greet      greet user
  exit       exit the program
  help       display help

>>> greet Someone Somewhere
Hello Someone Somewhere
>>> exit
$

Reading input.

// simulate an authentication
shell.AddCmd(&ishell.Cmd{
	Name: "login",
	Help: "simulate a login",
	Func: func(c *ishell.Context) {
		// disable the '>>>' for cleaner same line input.
		c.ShowPrompt(false)
		defer c.ShowPrompt(true) // yes, revert after login.

		 // get username
		c.Print("Username: ")
		username := c.ReadLine()

		// get password.
		c.Print("Password: ")
		password := c.ReadPassword()

		... // do something with username and password

		c.Println("Authentication Successful.")
	},
})

Execution

>>> login
Username: someusername
Password:
Authentication Successful.

Multiline input.

Builtin support for multiple lines.

>>> This is \
... multi line

>>> Cool that << EOF
... everything here goes
... as a single argument. 
... EOF

User defined

shell.AddCmd(&ishell.Cmd{
	Name: "multi",
	Help: "input in multiple lines",
	Func: func(c *ishell.Context) {
		c.Println("Input multiple lines and end with semicolon ';'.")
		lines := c.ReadMultiLines(";")
		c.Println("Done reading. You wrote:")
		c.Println(lines)
	},
})

Execution

>>> multi
Input some lines:
>>> this is user defined 
... multiline input;
You wrote:
this is user defined
multiline input;

Keyboard interrupt.

Builtin interrupt handler.

>>> ^C
Input Ctrl-C once more to exit
>>> ^C
Interrupted
exit status 1

Custom

shell.Interrupt(func(c *ishell.Context) { ... })

Durable history.

// Read and write history to $HOME/.ishell_history
shell.SetHomeHistoryPath(".ishell_history")

Check example code for more.

Supported Platforms

  • Linux
  • OSX
  • Windows

Note

ishell is in active development and can still change significantly.

Roadmap (in no particular order)

  • Support multiline inputs.
  • Command history.
  • Tab completion.
  • Handle ^C interrupts.
  • Subcommands and help texts.
  • Coloured outputs.
  • Testing, testing, testing.

Contribution

  1. Create an issue to discuss it.
  2. Send in Pull Request.

License

MIT

Credits

Library Use
github.com/flynn/go-shlex splitting input into command and args.
gopkg.in/readline.v1 history, tab completion and reading passwords.