kommandant

package module
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2021 License: Apache-2.0 Imports: 10 Imported by: 1

README

Kommandant README
=================

== Fetching from the repository ==

go get gitlab.com/ianbruene/Kommandant

== Purpose ==

Kommandant exists to ease the creation of command line interfaces for Go
programs. It is a near-port of Python's cmd module, with the caveat that
Go is a compiled language and cannot do introspection as easily. Despite
this mismatch the useful concepts are still useful after translation, and
the user-visible behavior will be the same.

== How To ==

link:kommandant-HOWTO.adoc[How To]

== Example Code ==

Example code can be found in the demos/ directory. It can be run with:

go run path/to/kommandant/demos/demo-readline.go

== Related projects ==

People who need an emulation of Python cmd are also likely
to find these useful:

https://github.com/ianbruene/go-difflib[go-difflib]::
	Much of Python's difflib ported to Go.

https://gitlab.com/esr/gogetopt::
	Python getopt ported to Go.

https://gitlab.com/esr/pytogo[pytogo]::
	A fast, rough, incomplete, but useful Python to Go translator.

https://github.com/chzyer/readline::
	Readline workalike that can be used to translate Python raw_input().

// end

Documentation

Index

Constants

View Source
const (
	NODEBUG = iota
	ERROR
	INFO
	DEBUG
	DEEPDEBUG
)

Debug levels

Variables

This section is empty.

Functions

func PcItemDynamicRecursive added in v0.3.0

func PcItemDynamicRecursive(callback readline.DynamicCompleteFunc) readline.PrefixCompleterInterface

func ReadToDelimiter added in v0.5.0

func ReadToDelimiter(r io.Reader, delimiter byte) (s string, err error)

Performs the same function as bufio's ReadString without the gluttony

Types

type Kmdt

type Kmdt struct {
	Debug        uint
	Sub          KmdtClient // The struct which will have all the handlers
	CommandQueue []string
	Intro        string
	LastCmd      string
	Ruler        string
	DocLeader    string
	DocHeader    string
	UnDocHeader  string
	MiscHeader   string
	NoHelp       string

	// Characters used in command names
	IdentChars string
	// Special hooks
	// =============
	// Hook run in place of OneCmd if present
	OneCmdHook func(ctx context.Context, lineIn string) (stopOut bool)
	// Hook executed once when the cmdloop() method is called.
	PreLoopHook func(ctx context.Context)
	// Hook executed once when the cmdloop() method is about to return.
	PostLoopHook func(ctx context.Context)
	// Hook executed just before the command line is interpreted, but after
	// the input prompt is generated and issued.
	PreCmdHook func(ctx context.Context, lineIn string) (lineOut string)
	// Hook executed just after a command dispatch is finished.
	PostCmdHook func(ctx context.Context, stopIn bool, lineIn string) (stopOut bool)
	// Called when an empty line is entered in response to the prompt.
	// If this is not set, the last nonempty command entered will be repeated.
	EmptyLineHook func(ctx context.Context) (stopOut bool)
	// Called on an input line when the command prefix is not recognized.
	// If this is not set, an error message is printed and it and returns.
	DefaultHook func(ctx context.Context, lineIn string) (stopOut bool)
	// Function called to complete an input line when no command-specific
	// complete function is available. By default, it returns an empty list.
	CompleteDefaultHook func(line string) (options []string)
	// contains filtered or unexported fields
}

func NewKommandant

func NewKommandant(handlers KmdtClient) *Kmdt

func NewKommandantDebug added in v0.5.0

func NewKommandantDebug(handlers KmdtClient, debug uint) *Kmdt

func (*Kmdt) CmdLoop

func (k *Kmdt) CmdLoop(ctx context.Context, intro string)

Repeatedly issue a prompt, accept input, parse an initial prefix off the received input, and dispatch to action methods, passing them the remainder of the line as argument.

func (*Kmdt) Default added in v0.4.0

func (k *Kmdt) Default(ctx context.Context, lineIn string) (stopOut bool)

func (*Kmdt) DoHelp added in v0.3.0

func (k *Kmdt) DoHelp(ctx context.Context, argIn string) (stopOut bool)

func (*Kmdt) EmptyLine

func (k *Kmdt) EmptyLine(ctx context.Context) (stopOut bool)

func (*Kmdt) EnableReadline added in v0.3.0

func (k *Kmdt) EnableReadline(enable bool) (err error)

func (*Kmdt) GetPrompt added in v0.3.0

func (k *Kmdt) GetPrompt() (prompt string)

func (*Kmdt) GetStdin added in v0.3.0

func (k *Kmdt) GetStdin() (stdin io.ReadCloser)

func (*Kmdt) GetStdout added in v0.3.0

func (k *Kmdt) GetStdout() (stdout io.Writer)

func (*Kmdt) OneCmd

func (k *Kmdt) OneCmd(ctx context.Context, lineIn string) (stopOut bool)

func (*Kmdt) OneCmd_core added in v0.3.0

func (k *Kmdt) OneCmd_core(ctx context.Context, lineIn string) (stopOut bool)

Interpret the argument as though it had been typed in response to the prompt.

func (*Kmdt) ParseLine

func (k *Kmdt) ParseLine(lineIn string) (cmd string, arg string, lineOut string)

Parse the line into a command name and a string containing the arguments. Returns a tuple containing (command, args, line). 'command' and 'args' may be None if the line couldn't be parsed.

func (*Kmdt) PostCmd added in v0.4.0

func (k *Kmdt) PostCmd(ctx context.Context, stopIn bool, lineIn string) (stopOut bool)

func (*Kmdt) PostLoop

func (k *Kmdt) PostLoop(ctx context.Context)

func (*Kmdt) PreCmd added in v0.4.0

func (k *Kmdt) PreCmd(ctx context.Context, lineIn string) (lineOut string)

func (*Kmdt) PreLoop

func (k *Kmdt) PreLoop(ctx context.Context)

func (*Kmdt) Readline added in v0.5.0

func (k *Kmdt) Readline() (line string, err error)

func (*Kmdt) ReadlineEnabled added in v0.3.0

func (k *Kmdt) ReadlineEnabled() (enabled bool)

func (*Kmdt) SetPrompt added in v0.3.0

func (k *Kmdt) SetPrompt(prompt string)

func (*Kmdt) SetStdin added in v0.3.0

func (k *Kmdt) SetStdin(stdin io.ReadCloser)

func (*Kmdt) SetStdout added in v0.3.0

func (k *Kmdt) SetStdout(stdout io.Writer)

func (*Kmdt) Write added in v0.3.0

func (k *Kmdt) Write(data []byte)

func (*Kmdt) WriteString added in v0.3.0

func (k *Kmdt) WriteString(data string)

type KmdtClient added in v0.3.0

type KmdtClient interface {
	SetCore(k *Kmdt)
}

type RecursiveCompleter added in v0.3.0

type RecursiveCompleter struct {
	Completer      readline.PrefixCompleter
	MaxRecurse     int
	CurrentRecurse int
}

The normal readline completion handling requires a seperate dynamic callback for each layer of the completion. This allows the same callback to handle multiple arguments on a single command.

func (*RecursiveCompleter) Do added in v0.3.0

func (r *RecursiveCompleter) Do(line []rune, pos int) (newLine [][]rune, length int)

func (*RecursiveCompleter) GetChildren added in v0.3.0

func (*RecursiveCompleter) GetDynamicNames added in v0.3.0

func (r *RecursiveCompleter) GetDynamicNames(line []rune) [][]rune

func (*RecursiveCompleter) GetName added in v0.3.0

func (r *RecursiveCompleter) GetName() []rune

func (*RecursiveCompleter) IsDynamic added in v0.3.0

func (r *RecursiveCompleter) IsDynamic() bool

func (*RecursiveCompleter) Print added in v0.3.0

func (r *RecursiveCompleter) Print(prefix string, level int, buf *bytes.Buffer)

func (*RecursiveCompleter) SetChildren added in v0.3.0

func (r *RecursiveCompleter) SetChildren(children []readline.PrefixCompleterInterface)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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