cmd

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InsufficientArgs = errors.New("not enough arguments for command")

InsufficientArgs is returned by argument parsing functions if it does not have sufficient arguments passed and is not optional.

Functions

This section is empty.

Types

type Command

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

Command is a wrapper around a Runnable. It provides additional identity and utility methods for the actual runnable command so that it may be identified more easily.

func New

func New(name, description string, r ...Runnable) Command

New returns a new Command using the name and description passed. The Runnable passed must be a (pointer to a) struct, with its fields representing the parameters of the command. When the command is ran, the Run method of the Runnable will be called, after all fields have their values from the parsed command set. If r is not a struct or a pointer to a struct, New panics.

func (Command) Description

func (cmd Command) Description() string

Description returns the description of the command. The description is shown in the /help list, and provides information on the functionality of a command.

func (Command) Execute

func (cmd Command) Execute(args string, source Source)

Execute executes the Command as a source with the args passed. The args are parsed assuming they do not start with the command name. Execute will attempt to parse and execute one Runnable at a time. If one of the Runnable was able to parse args correctly, it will be executed and no more Runnables will be attempted to be run. If parsing of all Runnables was unsuccessful, a command output with an error message is sent to the Source passed, and the Run method of the Runnables are not called. The Source passed must not be nil. The method will panic if a nil Source is passed.

func (Command) Name

func (cmd Command) Name() string

Name returns the name of the command. The name is guaranteed to be lowercase and will never have spaces in it. This name is used to call the command, and is shown in the /help list.

func (Command) Usage

func (cmd Command) Usage() string

Usage returns the usage of the command. The usage will be roughly equal to the one showed by the client in-game.

type Enum

type Enum interface {
	// Type returns the type of the enum. This type shows up client-side in the command usage, in the spot
	// where parameter types otherwise are.
	Type() string
	// Options should return a list of options that show up on the client side. The command will ensure that
	// the argument passed to the enum parameter will be equal to one of these options.
	Options() []string
	// SetOption sets the option that was selected in the argument. It is guaranteed to be one of the options
	// obtained using Enum.Options().
	SetOption(option string, v reflect.Value)
}

Enum is an interface for enum-type parameters. Users may have types as command parameters that implement this parameter in order to allow a specific set of options only.

type Limiter

type Limiter interface {
	// Limit returns all sources that the command can be run by. It is only limited to these sources: Other
	// sources can under no circumstance call it.
	Limit() []Source
}

Limiter may be implemented by a type also implementing Runnable to limit the sources that may run the command. If the source running the command is not on of the types returned by the Limit method, the command will fail.

type Line

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

Line represents a command line holding command arguments that were passed upon the execution of the command. It is a convenience wrapper around a string slice.

func (*Line) Leftover

func (line *Line) Leftover() []string

Leftover takes the leftover arguments from the command line.

func (*Line) Len

func (line *Line) Len() int

Len returns the leftover length of the arguments in the command line.

func (*Line) Next

func (line *Line) Next() (string, bool)

Next takes the next argument from the command line and returns it. If there were no more arguments to consume, false is returned.

func (*Line) NextN

func (line *Line) NextN(n int) ([]string, bool)

NextN takes the next N arguments from the command line and returns them. If there were not enough arguments (n arguments), false is returned.

type Output

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

Output holds the output of a command execution. It holds success messages and error messages, which the source of a command execution gets sent.

func (*Output) Error

func (o *Output) Error(a ...interface{})

Error formats an error message and adds it to the command output.

func (*Output) ErrorCount

func (o *Output) ErrorCount() int

ErrorCount returns the count of errors that the command output has.

func (*Output) Errorf

func (o *Output) Errorf(format string, a ...interface{})

Errorf formats an error message and adds it to the command output.

func (*Output) Errors

func (o *Output) Errors() []error

Errors returns a list of all errors added to the command output. Usually only one error message is set: After one error message, execution of a command typically terminates.

func (*Output) MessageCount

func (o *Output) MessageCount() int

MessageCount returns the count of (success) messages that the command output has.

func (*Output) Messages

func (o *Output) Messages() []string

Messages returns a list of all messages added to the command output. The amount of messages set depends on the command called.

func (*Output) Print

func (o *Output) Print(a ...interface{})

Print formats a (success) message and adds it to the command output.

func (*Output) Printf

func (o *Output) Printf(format string, a ...interface{})

Printf formats a (success) message and adds it to the command output.

type Parameter

type Parameter interface {
	// Parse takes an arbitrary amount of arguments from the command Line passed and parses it, so that it can
	// store it to value v. If the arguments cannot be parsed from the Line, an error should be returned.
	Parse(line *Line, v reflect.Value) error
	// Type returns the type of the parameter. It will show up in the usage of the command, and, if one of the
	// known type names, will also show up client-side.
	Type() string
}

Parameter is an interface for a generic parameters. Users may have types as command parameters that implement this parameter.

type Runnable

type Runnable interface {
	// Run runs the Command, using the arguments passed to the Command. The source is passed to the method,
	// which is the source of the execution of the Command, and the output is passed, to which messages may be
	// added which get sent to the source.
	Run(source Source, output *Output)
}

Runnable represents a Command that may be ran by a Command source. The Command must be a struct type and its fields represent the parameters of the Command. When the Run method is called, these fields are set and may be used for behaviour in the Command. A Runnable may have exported fields only of the following types: int8, int16, int32, int64, int, uint8, uint16, uint32, uint64, uint, float32, float64, string, bool, mgl32.Vec3, or a type that implements the cmd.Parameter or cmd.Enum interface. Fields in the Runnable struct may have the `optional:""` struct tag to mark them as an optional parameter, the `suffix:"$suffix"` struct tag to add a suffix to the parameter in the usage, and the `name:"name"` tag to specify a name different than the field name for the parameter.

type Source

type Source interface {
	// SendCommandOutput sends a command output to the source. The way the output is applied, depends on what
	// kind of source it is.
	// SendCommandOutput is called by a Command automatically after being run.
	SendCommandOutput(output *Output)
}

Source represents a source of a command execution. Commands may limit the sources that can run them by implementing the Limiter interface.

Jump to

Keyboard shortcuts

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