Code Monkey home page Code Monkey logo

ishell's Introduction

ishell

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

Documentation

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.Register("greet", func(args ...string) (string, error) {
        name := "Stranger"
        if len(args) > 0 {
            name = strings.Join(args, " ")
        }
		return "Hello "+name, nil
	})

	// start shell
	shell.Start()
}

Execution

Sample Interactive Shell
>>> help
Commands:
exit help greet
>>> greet Someone Somewhere
Hello Someone Somewhere
>>> exit
$
Reading input.
// simulate an authentication
shell.Register("login", func(args ...string) (string, error) {
	// disable the '>>>' for cleaner same line input.
	shell.ShowPrompt(false)
	defer shell.ShowPrompt(true) // yes, revert after login.

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

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

	... // do something with username and password

    return "Authentication Successful.", nil
})

Execution

>>> login
Username: someusername
Password:
Authentication Successful.
How about 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.Register("multi", func(args ...string) (string, error) {
	shell.Println("Input some lines:")
	// read until a semicolon ';' is found
	// use shell.ReadMultiLinesFunc for more control.
	lines := shell.ReadMultiLines(";")
	shell.Println("You wrote:")
	return lines, nil
})

Execution

>>> multi
Input some lines:
>>> this is user defined 
... multiline input;
You wrote:
this is user defined
multiline input;
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.
  • Subcommands and help texts.
  • Handle ^C interrupts.
  • 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.

ishell's People

Contributors

abiosoft avatar dokipen avatar

Watchers

Brad Wasson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.