pyplanet.contrib.command

The commands contributed package contains command management and callback logic.

class pyplanet.contrib.command.CommandManager(instance)[source]

The Command Manager contributed extension is a manager that controls all chat-commands in the game. Your app needs to use this manager to register any custom commands you want to provide.

You should access this class within your app like this:

self.instance.command_manager

You can register your commands like this:

await self.instance.command_manager.register(
        Command(command='reboot', target=self.reboot_pool, perms='admin:reboot', admin=True),
)

More information of the command and the options of it, see the pyplanet.contrib.command.Command class.

Warning

Don’t initiate this class yourself. Access this class from the self.instance.command_manager instance.

register(*commands)[source]

Register your command.

Parameters:commands (pyplanet.contrib.command.command.Command) – Command instance.
class pyplanet.contrib.command.Command(command, target, aliases=None, admin=False, namespace=None, parser=None, perms=None, description=None)[source]

The command instance describes the command itself, the target to fire and all other related information, like admin command or aliases.

Some examples of some commands:

# Admin command with permission on it.
Command(command='reboot', target=self.reboot_pool, perms='admin:reboot', admin=True)

# Normal user command with optional argument.
Command(command='list', target=self.show_map_list)                      .add_param(name='search', required=False)
add_param(name: str, nargs=1, type=<class 'str'>, default=None, required: bool = True, help: str = None, dest: str = None)[source]

Add positional parameter.

Parameters:
  • name – Name of parameter, will be used to store result into!
  • nargs – Number of arguments, use integer or ‘*’ for multiple or infinite.
  • type – Type of value, keep str to match all types. Use any other to try to parse to the type.
  • default – Default value when no value is given.
  • required – Set the parameter required state, defaults to true.
  • help – Help text to display when parameter is invalid or not given and required.
  • dest – Destination to save into namespace result (defaults to name).
Returns:

parser instance

Return type:

pyplanet.contrib.command.command.Command

get_params(input)[source]

Get params in array from input in array.

Parameters:input (list) – Array of raw input.
Returns:Array of parameters, stripped of the command name and namespace, if defined.
Return type:list
handle(instance, player, argv)[source]

Handle command parsing and execution.

Parameters:
  • player (pyplanet.apps.core.maniaplanet.models.player.Player) – Player object.
  • argv – Arguments in array
match(raw)[source]

Try to match the command with the given input in array style (splitted by spaces).

Parameters:raw (list) – Raw input, split by spaces.
Returns:Boolean if command matches.
usage_text

The usage text line for the command.

class pyplanet.contrib.command.ParameterParser(prog=None)[source]

Parameter Parser.

Todo

Write introduction + examples.

add_param(name: str, nargs=1, type=<class 'str'>, default=None, required: bool = True, help: str = None, dest: str = None)[source]

Add positional parameter.

Parameters:
  • name – Name of parameter, will be used to store result into!
  • nargs – Number of arguments, use integer or ‘*’ for multiple or infinite.
  • type – Type of value, keep str to match all types. Use any other to try to parse to the type.
  • default – Default value when no value is given.
  • required – Set the parameter required state, defaults to true.
  • help – Help text to display when parameter is invalid or not given and required.
  • dest – Destination to save into namespace result (defaults to name).
Returns:

parser instance

Return type:

pyplanet.contrib.command.ParameterParser

errors

Get errors.

Returns:array of strings.
Return type:list
is_valid()[source]

Is data valid?

Returns:boolean
parse(argv)[source]

Parse arguments.

Parameters:argv – arguments.
parse_parameter(param, input, position)[source]

Validate and parse param value at input position.

Parameters:
  • param (dict) – Param dict given.
  • input (list) – Full params input (without command or namespace!)
  • position (int) – Current seek position.
Returns:

value.

exception pyplanet.contrib.command.exceptions.InvalidParamException[source]

Invalid parameter arguments given!

exception pyplanet.contrib.command.exceptions.NotValidated[source]

Your parser hasn’t been called with .parse() before, so no parsing took place!