Documentation ¶
Overview ¶
Package cmd provides a simple way to creates simple line-oriented interactive command interpreters. It's inspired by the cmd module from the Python standard library.
Example (Std) ¶
package main import ( "fmt" "github.com/kisielk/cmd" "os" "strings" ) func main() { hello := func(args []string) (string, error) { if len(args) == 0 { return "What's your name?\n", nil } return fmt.Sprintf("Hello, %s\n", strings.Join(args, " ")), nil } c := cmd.New(map[string]cmd.CmdFn{"hello": hello}, os.Stdin, os.Stdout) c.Loop() }
Output:
Example (Tcp) ¶
package main import ( "fmt" "github.com/kisielk/cmd" "log" "net" "strings" ) func main() { hello := func(args []string) (string, error) { if len(args) == 0 { return "What's your name?\n", nil } return fmt.Sprintf("Hello, %s\n", strings.Join(args, " ")), nil } ln, err := net.Listen("tcp", ":6000") if err != nil { log.Fatal("could not open port:", err) } for { conn, err := ln.Accept() if err != nil { log.Println("couldn't accept console:", err) continue } c := cmd.New(map[string]cmd.CmdFn{"hello": hello}, conn, conn) go c.Loop() } }
Output:
Index ¶
Examples ¶
Constants ¶
const DefaultPrompt = "> "
DefaultPrompt is the default value of Cmd.Prompt
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cmd ¶
type Cmd struct { // In receives input In io.Reader // Out transmits output Out io.Writer // Prompt is displayed on the console before every line of input. Prompt string // Commands is a map of command functions for valid commands. // If a command is not in this map then Default will be called. Commands map[string]CmdFn // Default is called when a command is received that does not match // any function in the Commands map. The line argument will contain // the full contents of the line received. // // The value of out is printed to the console. // If err is not nil then execution of the command loop is terminated. // // If Default is not set the behaviour is to print a message to the // console. Default func(line string) (out string, err error) // EmptyLine is called whenever a line containing no characters // other than whitespace or newline is received. // // The value of out is printed to the console. // If err is not nil then execution of the command loop is terminated. // // If EmptyLine is not set then the last command is repeated. EmptyLine func() (out string, err error) // Tokens is called for each line of input to generate the tokens. // // The first token is the name of the command that will be called, // while the rest of the tokens are passed as arguments to the command. // // If Tokens is not set then strings.Fields is used. Tokens func(line string) (tokens []string) // LastLine contains the last non-empty line received LastLine string }
Cmd is an interactive command interpreter. It's started by calling the Loop method. Instances of Cmd should be constructed with the New function.
func (*Cmd) Loop ¶
Loop starts the interpreter loop.
For each iteration it prints c.Prompt to c.Out and then waits for a line of input. The line is tokenized using c.Tokens and the first token is interpreted as the name of a command. The command is looked up in c.Commands and is called with the remaining tokens.
If the command is not found then c.Default is called with the entire line.
If the input line consists only of whitespace then c.EmptyLine is called.