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 ¶
- Variables
- func Component(c format.AnyComponent)
- func ExecConf(path string)
- func Execute(cmd string, extra ...interface{}) (err error)
- func History(lines int) []format.AnyComponent
- func Register(desc string, f interface{})
- func RegisterType(t reflect.Type, handler TypeHandler)
- func Text(f string, args ...interface{})
- type BoolVar
- type IntVar
- type Property
- type StringVar
- type TypeHandler
Constants ¶
This section is empty.
Variables ¶
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 Execute ¶
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
Types ¶
type BoolVar ¶
type BoolVar struct {
// contains filtered or unexported fields
}
BoolVar is a console var that contains an bool
func NewBoolVar ¶
NewBoolVar creates and registers a bool console variable
type IntVar ¶
type IntVar struct {
// contains filtered or unexported fields
}
IntVar is a console var that contains an integer
type StringVar ¶
type StringVar struct {
// contains filtered or unexported fields
}
StringVar is a console var that contains an string
func NewStringVar ¶
NewStringVar creates and registers a string console variable
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