console

package
v0.0.0-...-03afc55 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2015 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package console implements a system for parsing and executing commands + logging.

When registering commands a description of the command is required. For basic commands the format of this is simple:

commandname sub1 sub2 sub...

Complex commands can be created by using % to specify arguments. The type of the argument will be inferred from the type over the passed function pointer. Extra constraints can be added after the % to gain finer control over the argument.

Built-in types:

string        any string, a length limit may be added
              after the % to enforce a max length

Executing commands works by treating whitespace at delimiters between arguments with the exception of whitespace contained within quotes (") as that will be treated as a single argument

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCommandNotFound is returned when no matching command was found
	ErrCommandNotFound = errors.New("command not found")
)

Functions

func Component

func Component(c format.AnyComponent)

Component appends the component to the log buffer.

func ExecConf

func ExecConf(path string)

ExecConf executes/loads the passed config file

func Execute

func Execute(cmd string, extra ...interface{}) (err error)

Execute tries to execute the specified command.

Panics if the number of extra arguments doesn't match the amount specified in Registry's ExtraParameters

func History

func History(lines int) []format.AnyComponent

History returns up to the requested number of lines from the log buffer.

As a special case -1 will return the whole buffer.

func Register

func Register(desc string, f interface{})

Register adds the passed function to the command registry using the description to decided on its name and location.

f must be a function

This is designed to panic instead of returning an error because its intended to be used in init methods and fail early on mistakes

func RegisterType

func RegisterType(t reflect.Type, handler TypeHandler)

RegisterType adds the passed type and handler to the the registry, any future calls to Register will be able use the type added here

func Text

func Text(f string, args ...interface{})

Text appends the passed formatted string plus a new line to the log buffer. The formatting uses the same rules as fmt.

Types

type BoolVar

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

BoolVar is a console var that contains an bool

func NewBoolVar

func NewBoolVar(name string, val bool, props ...Property) *BoolVar

NewBoolVar creates and registers a bool console variable

func (*BoolVar) Callback

func (b *BoolVar) Callback(cb func()) *BoolVar

Callback sets the callback to be called when the value is updated

func (*BoolVar) Doc

func (b *BoolVar) Doc(d string) *BoolVar

Doc sets the documentation for this cvar

func (*BoolVar) Name

func (b *BoolVar) Name() string

func (*BoolVar) SetValue

func (b *BoolVar) SetValue(v bool)

func (*BoolVar) String

func (b *BoolVar) String() string

func (*BoolVar) Value

func (b *BoolVar) Value() bool

type IntVar

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

IntVar is a console var that contains an integer

func NewIntVar

func NewIntVar(name string, val int, props ...Property) *IntVar

NewIntVar creates and registers a integer console variable

func (*IntVar) Callback

func (i *IntVar) Callback(cb func()) *IntVar

Callback sets the callback to be called when the value is updated

func (*IntVar) Doc

func (i *IntVar) Doc(d string) *IntVar

Doc sets the documentation for this cvar

func (*IntVar) Name

func (i *IntVar) Name() string

func (*IntVar) SetValue

func (i *IntVar) SetValue(v int)

func (*IntVar) String

func (i *IntVar) String() string

func (*IntVar) Value

func (i *IntVar) Value() int

type Property

type Property int

Property is a flag for cvar to control how its handled

const (
	// Mutable marks whether the var can be changed from the
	// console
	Mutable Property = 1 << iota
	// Serializable marks the var to be saved and loaded
	Serializable
)

type StringVar

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

StringVar is a console var that contains an string

func NewStringVar

func NewStringVar(name, val string, props ...Property) *StringVar

NewStringVar creates and registers a string console variable

func (*StringVar) Callback

func (s *StringVar) Callback(cb func()) *StringVar

Callback sets the callback to be called when the value is updated

func (*StringVar) Doc

func (s *StringVar) Doc(d string) *StringVar

Doc sets the documentation for this cvar

func (*StringVar) Name

func (s *StringVar) Name() string

func (*StringVar) SetValue

func (s *StringVar) SetValue(v string)

func (*StringVar) String

func (s *StringVar) String() string

func (*StringVar) Value

func (s *StringVar) Value() string

type TypeHandler

type TypeHandler interface {
	DefineType(arg string) interface{}
	ParseType(arg string, info interface{}) (interface{}, error)
	Equals(a, b interface{}) bool
}

TypeHandler handles defining and parsing dynamic arguments for command.

DefineType is called during Register where arg is the string after %, the return value will be stored and passed to ParseType

ParseType is called during execute where arg is the argument to parse. info is the value original returned from DefineType. This should return the parsed value.

Equals is called on the value returned from DefineType to see if the type has been defined already

Jump to

Keyboard shortcuts

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