Documentation
¶
Index ¶
- Constants
- Variables
- type Argument
- type ArgumentType
- type Command
- func (c *Command) Call(req Request) Response
- func (c *Command) CheckArguments(req Request) error
- func (c *Command) Get(path []string) (*Command, error)
- func (c *Command) GetOptions(path []string) (map[string]Option, error)
- func (c *Command) Resolve(path []string) ([]*Command, error)
- func (c *Command) Subcommand(id string) *Command
- type Context
- type EncodingType
- type Error
- type ErrorType
- type Function
- type Marshaller
- type Option
- type OptionValue
- type Request
- type Response
Constants ¶
const (
Invalid = reflect.Invalid
Bool = reflect.Bool
Int = reflect.Int
Uint = reflect.Uint
Float = reflect.Float64
String = reflect.String
)
Types of Command options
const (
EncShort = "enc"
EncLong = "encoding"
)
Flag names
const (
JSON = "json"
XML = "xml"
Text = "text"
)
Supported EncodingType constants.
Variables ¶
var ErrNoFormatter = errors.New("This command cannot be formatted to plain text")
var ErrNotCallable = errors.New("This command can't be called directly. Try one of its subcommands.")
ErrNotCallable signals a command that cannot be called.
Functions ¶
This section is empty.
Types ¶
type Argument ¶
type Argument struct {
Name string
Type ArgumentType
Required bool
Variadic bool
Description string
}
type Command ¶
type Command struct {
Description string
Help string
Options []Option
Arguments []Argument
Run Function
Marshallers map[EncodingType]Marshaller
Type interface{}
Subcommands map[string]*Command
}
Command is a runnable command, with input arguments and options (flags). It can also have Subcommands, to group units of work into sets.
func (*Command) Call ¶
func (c *Command) Call(req Request) Response
Call invokes the command for the given Request
func (*Command) CheckArguments ¶
func (c *Command) CheckArguments(req Request) error
func (*Command) Get ¶
func (c *Command) Get(path []string) (*Command, error)
Get resolves and returns the Command addressed by path
func (*Command) GetOptions ¶
func (c *Command) GetOptions(path []string) (map[string]Option, error)
GetOptions gets the options in the given path of commands
func (*Command) Resolve ¶
func (c *Command) Resolve(path []string) ([]*Command, error)
Resolve gets the subcommands at the given path
func (*Command) Subcommand ¶
func (c *Command) Subcommand(id string) *Command
Subcommand returns the subcommand with the given id
type Error ¶
type Error struct {
Message string
Code ErrorType
}
Error is a struct for marshalling errors
type ErrorType ¶
type ErrorType uint
ErrorType signfies a category of errors
const (
ErrNormal ErrorType = iota // general errors
ErrClient // error was caused by the client, (e.g. invalid CLI usage)
)
ErrorTypes convey what category of error ocurred
type Function ¶
type Function func(Request) (interface{}, error)
Function is the type of function that Commands use. It reads from the Request, and writes results to the Response.
type Marshaller ¶
type Marshaller func(Response) ([]byte, error)
Marshaller is a function that takes in a Response, and returns a marshalled []byte (or an error on failure)
type Option ¶
type Option struct {
Names []string // a list of unique names to
Type reflect.Kind // value must be this type
Description string // a short string to describe this option
}
Option is used to specify a field that will be provided by a consumer
func BoolOption ¶
func BoolOption(names ...string) Option
func FloatOption ¶
func FloatOption(names ...string) Option
func NewOption ¶
func NewOption(kind reflect.Kind, names ...string) Option
constructor helper functions
func StringOption ¶
func StringOption(names ...string) Option
func UintOption ¶
func UintOption(names ...string) Option
type OptionValue ¶
type OptionValue struct {
// contains filtered or unexported fields
}
func (OptionValue) Bool ¶
func (ov OptionValue) Bool() (bool, error)
value accessor methods, gets the value as a certain type
type Request ¶
type Request interface {
Path() []string
Option(name string) *OptionValue
Options() optMap
SetOption(name string, val interface{})
Arguments() []interface{} // TODO: make argument value type instead of using interface{}
Context() *Context
SetContext(Context)
Command() *Command
ConvertOptions() error
}
Request represents a call to a command from a consumer
func NewRequest ¶
func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command, optDefs map[string]Option) Request
NewRequest returns a request initialized with given arguments
type Response ¶
type Response interface {
Request() Request
// Set/Return the response Error
SetError(err error, code ErrorType)
Error() *Error
// Sets/Returns the response value
SetOutput(interface{})
Output() interface{}
// Marshal marshals out the response into a buffer. It uses the EncodingType
// on the Request to chose a Marshaller (Codec).
Marshal() ([]byte, error)
// Gets a io.Reader that reads the marshalled output
Reader() (io.Reader, error)
}
Response is the result of a command request. Handlers write to the response, setting Error or Value. Response is returned to the client.
func NewResponse ¶
func NewResponse(req Request) Response
NewResponse returns a response to match given Request