-
Notifications
You must be signed in to change notification settings - Fork 20
command_object
local command = require('command')
local command_object = command.new(name)
Command objects are proxies to register and unregister commands related to a base string.
A command object contains the following functions:
-
command_object:register
Registers a function to execute on a certain command -
command_object:register_source
Registers a function to execute on a certain command, provided with a source argument -
command_object:unregister
Removes a function from the command registry -
command_object:syntax
Returns a syntax string for a specific command or group of commands
Registers a function to execute for a certain command (or nested sub-commands). The parameters to the specified function will be arguments, as described by the argument descriptors.
function command_object:register(...names : string, fn : function, ...args : argument_object)
...names string
Sub-command names. If you want to register just to the root
name
then leave these parameters out:command_object:register(fn, ...args)If you want to register
/<name> abc ...
and/<name> def ...
then do:command_object:register('abc', fn, ...args) command_object:register('def', fn, ...args)
fn function
The function to execute. Will be called with the parameters as specified by the following argument descriptors.
...args argument_object or string
Each of the remaining arguments is either an argument descriptor or a string which can be parsed by command.arg.parse. In the latter case, that is exactly what will happen internally and it is meant as a convenience. The following are equivalent:
command_object:register(fn, arg_string) command_object:register(fn, command.arg.parse(arg_string))
This function does not return any values.
Registers a function to execute for a certain command (or nested sub-commands). The first parameter to the specified function will be the source of the command, followed by the other arguments, as described by the argument descriptors.
function command_object:register_source(...names : string, fn : function, ...args : string or table)
...names string
Sub-command names. If you want to register just to the root
name
then leave these parameters out:command_object:register_source(fn, ...args)If you want to register
/<name> abc ...
and/<name> def ...
then do:command_object:register_source('abc', fn, ...args) command_object:register_source('def', fn, ...args)
fn function
The function to execute. Will be called with the source of the command as the first parameter, followed by the parameters as specified by the following argument descriptors.
...args string or table
Each of the remaining arguments is either an argument definition or a string which can be parsed by command.arg.parse. In the latter case, that is exactly what will happen internally and it is meant as a convenience. The following are equivalent:
command_object:register_source(fn, arg_string) command_object:register_source(fn, command.arg.parse(arg_string))
This function does not return any values.
Unregisters a previously registered function by the given sub-command, if any. If omitted, the root command will be unregistered, but this will not remove the core handler. To completely remove a command, call command.delete(command_object).
function command_object:unregister(...names : string)
...names string
Sub-command names. If you want to unregister just the root
name
then leave these parameters out:command_object:unregister()If you want to unregister
/<name> abc ...
and/<name> def ...
then do:command_object:unregister('abc') command_object:unregister('def')
This function does not return any values.
Generates a syntax description for the provided sub-command or all further nested sub-commands.
function command_object:syntax(...names : string) : string
...names string
Sub-command names. If you want the syntax for the entire command tree then leave these parameters out:
local syntax = command_object:syntax()If you want the syntax for a specific command only, like
abc
ordef
then do:local syntax = command_object:syntax('abc') local syntax = command_object:syntax('def')
syntax string
The syntax description. This same string is printed in the help text for any command.