cmd

package module
v0.0.0-...-151962e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2016 License: MIT Imports: 16 Imported by: 0

README

cmd

A library to create shell-like command processors, slightly inspired by the Python cmd/cmd2 package

Installation

$ go get github.com/gobs/cmd

Documentation

http://godoc.org/github.com/gobs/cmd

Example

import "github.com/gobs/cmd"

// return true to stop command loop
func Exit(line string) (stop bool) {
      fmt.Println("goodbye!")
      return true
}
  
// change the prompt
func (cmd *Cmd) SetPrompt(line string) (stop bool) {
cmd.Prompt = line
return
}

// initialize Cmd structure
commander := &cmd.Cmd{Prompt: "> ",}
commander.Init()

// add inline method
commander.Add(cmd.Command{
      "ls",
      `list stuff`,
      func(line string) (stop bool) {
          fmt.Println("listing stuff")
          return
  }})

// add another command
commander.Add(cmd.Command{
      Name: "prompt",
      Help: `Set prompt`,
      Call: commander.SetPrompt
      })

// and one more
commander.Add(cmd.Command{
      "exit",
      `terminate example`,
      Exit
  })

// start command loop
commander.CmdLoop()

Documentation

Overview

 This package is used to implement a "line oriented command interpreter", inspired by the python package with
 the same name http://docs.python.org/2/library/cmd.html

 Usage:

	 commander := &Cmd{...}
	 commander.Init()

         cmd := NewCommand(name,Option...)

	 commander.Add(cmd)

	 commander.CmdLoop()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorizeString

func ColorizeString(text string)

func PrintDefaults

func PrintDefaults(f *flag.FlagSet)

Prints the default values of all defined flags in the set.

func RightJustifyText

func RightJustifyText(text string) string

Types

type Cmd

type Cmd struct {
	// the prompt string
	Prompt string

	// the history file
	HistoryFile string

	// this function is called before starting the command loop
	PreLoop func()

	// this function is called before terminating the command loop
	PostLoop func()

	// this function is called before executing the selected command
	PreCmd func(string)

	// this function is called after a command has been executed
	// return true to terminate the interpreter, false to continue
	PostCmd func(string, bool) bool

	// this function is called if the last typed command was an empty line
	EmptyLine func()

	// this function is called if the command line doesn't match any existing command
	// by default it displays an error message
	Default func(string)

	// this function is called to implement command completion.
	// it should return a list of words that match the input text
	Complete func(string, string, int, int) []string

	// if true, enable shell commands
	EnableShell bool

	// this is the list of available commands indexed by command name
	Commands map[string]*Command
	// contains filtered or unexported fields
}

This the the "context" for the command interpreter

func (*Cmd) Add

func (cmd *Cmd) Add(command *Command)

Add a command to the command interpreter. Overrides a command with the same name, if there was one

func (*Cmd) AddCommandCompleter

func (cmd *Cmd) AddCommandCompleter()

Add a completer that matches on command names

func (*Cmd) CmdLoop

func (cmd *Cmd) CmdLoop()

This is the command interpreter entry point. It displays a prompt, waits for a command and executes it until the selected command returns true

func (*Cmd) Echo

func (cmd *Cmd) Echo(line string) (stop bool)

func (*Cmd) GetRestartLoop

func (cmd *Cmd) GetRestartLoop() bool

func (*Cmd) Go

func (cmd *Cmd) Go(line string) (stop bool)

func (*Cmd) Help

func (cmd *Cmd) Help(command *Command, line string) (stop bool)

Default help command. It lists all available commands or it displays the help for the specified command

func (*Cmd) Init

func (cmd *Cmd) Init()

Initialize the command interpreter context

func (*Cmd) OneCmd

func (cmd *Cmd) OneCmd(line string) (stop bool)

This method executes one command

func (*Cmd) RestartLoop

func (cmd *Cmd) RestartLoop()

func (*Cmd) SetRestartLoop

func (cmd *Cmd) SetRestartLoop(restart bool)

type Command

type Command struct {
	// contains filtered or unexported fields
}

This is used to describe a new command

func NewCommand

func NewCommand(name string, opts ...Option) *Command

func (*Command) AddSubCommand

func (command *Command) AddSubCommand(name string, opts ...Option)

func (*Command) GetBoolFlag

func (command *Command) GetBoolFlag(name string) bool

func (*Command) GetCmdline

func (command *Command) GetCmdline() *Cmd

func (*Command) GetFlag

func (command *Command) GetFlag(name string) string

func (*Command) Usage

func (command *Command) Usage()

type Option

type Option func(command *Command)

func SetBoolFlag

func SetBoolFlag(flag string, value bool, help string) Option

func SetCmd

func SetCmd(cmd func(command *Command, line string) (stop bool)) Option

func SetCmdAlias

func SetCmdAlias(alias string) Option

func SetFlag

func SetFlag(flag string, value string, help string) Option

func SetHelp

func SetHelp(help string) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL