Skip to content
Stjepan Bakrac edited this page May 22, 2020 · 6 revisions
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 for a certain command (or nested sub-commands). The parameters to the specified function will be arguments, as described by the argument descriptors.

Definition

function command_object:register(...names : string, fn : function, ...args : argument_object)

Parameters

...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))

Return

This function does not return any values.



command_object:register_source

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.

Definition

function command_object:register_source(...names : string, fn : function, ...args : string or table)

Parameters

...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))

Return

This function does not return any values.



command_object:unregister

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).

Definition

function command_object:unregister(...names : string)

Parameters

...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')

Return

This function does not return any values.



command_object:syntax

Generates a syntax description for the provided sub-command or all further nested sub-commands.

function command_object:syntax(...names : string) : string

Parameters

...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 or def then do:

local syntax = command_object:syntax('abc')
local syntax = command_object:syntax('def')

Return

syntax string

The syntax description. This same string is printed in the help text for any command.

Clone this wiki locally