Command¶
Module for defining commands
This module provides a decorator to define commands in a plugin. It also provides fully documented available options unlike Nicotine+’s non existing documentation.
Example
from npc import command, BasePlugin
class Plugin(BasePlugin):
@command
def hello(self) -> None:
"""Say hello to the world"""
self.log.info("Hello World!")
@command(parameters=["<name>", "<age>"])
def greet(self, name: str, age: int) -> None:
"""Greet a person by name and age"""
self.window(f"Hello {name}, you are {age} years old")
@command("custom-name", description="Via decorator instead of docstring")
def other_name(self) -> None:
...
# Shows up under "Users" in the command help window instead of the plugin
@command(group=CommandGroup.USERS)
def user_command(self) -> None:
...
Note
The command name is extracted from the function name by default
The command description is extracted from the function’s docstring by default
The command will be available in all interfaces by default
The command will not be daemonized by default (blocking the main thread)
The command will parse arguments according to the function’s annotations by default
See also
npc.command() for more information on the parameters
- npc.command.F = ~F¶
Type variable for function
- npc.command.AF = ~AF¶
Type for function taking parsed input arguments
- npc.command.DF = ~DF¶
Type function taking default n+ command arguments
- npc.command(*, description: str | None = None, aliases: List[str] = [], disabled_interfaces: List[CommandInterface] = [], group: str | CommandGroup | None = None, parameters: List[str] = [], parameters_chatroom: List[str] = [], parameters_private_chat: List[str] = [], parameters_cli: List[str] = [], daemonize: bool = False, daemonize_return: ReturnCode = ReturnCode.PASS, parse_args: Literal[True] = True) Callable[[Callable[[...], ReturnCode | None]], Callable[[str, str | None, str | None], ReturnCode | None]]¶
- npc.command(*, description: str | None = None, aliases: List[str] = [], disabled_interfaces: List[CommandInterface] = [], group: str | CommandGroup | None = None, parameters: List[str] = [], parameters_chatroom: List[str] = [], parameters_private_chat: List[str] = [], parameters_cli: List[str] = [], daemonize: bool = False, daemonize_return: ReturnCode = ReturnCode.PASS, parse_args: Literal[False] = False) Callable[[Callable[[str, str | None, str | None], ReturnCode | None]], Callable[[str, str | None, str | None], ReturnCode | None]]
- npc.command(func_or_name: AF, *, description: str | None = None, aliases: List[str] = [], disabled_interfaces: List[CommandInterface] = [], group: str | CommandGroup | None = None, parameters: List[str] = [], parameters_chatroom: List[str] = [], parameters_private_chat: List[str] = [], parameters_cli: List[str] = [], daemonize: bool = False, daemonize_return: ReturnCode = ReturnCode.PASS, parse_args: Literal[True] = True) Callable[[str, str | None, str | None], ReturnCode | None]
- npc.command(func_or_name: DF, *, description: str | None = None, aliases: List[str] = [], disabled_interfaces: List[CommandInterface] = [], group: str | CommandGroup | None = None, parameters: List[str] = [], parameters_chatroom: List[str] = [], parameters_private_chat: List[str] = [], parameters_cli: List[str] = [], daemonize: bool = False, daemonize_return: ReturnCode = ReturnCode.PASS, parse_args: Literal[False] = False) Callable[[str, str | None, str | None], ReturnCode | None]
- npc.command(func_or_name: str, *, description: str | None = None, aliases: List[str] = [], disabled_interfaces: List[CommandInterface] = [], group: str | CommandGroup | None = None, parameters: List[str] = [], parameters_chatroom: List[str] = [], parameters_private_chat: List[str] = [], parameters_cli: List[str] = [], daemonize: bool = False, daemonize_return: ReturnCode = ReturnCode.PASS, parse_args: Literal[True] = True) Callable[[AF], Callable[[str, str | None, str | None], ReturnCode | None]]
- npc.command(func_or_name: str, *, description: str | None = None, aliases: List[str] = [], disabled_interfaces: List[CommandInterface] = [], group: str | CommandGroup | None = None, parameters: List[str] = [], parameters_chatroom: List[str] = [], parameters_private_chat: List[str] = [], parameters_cli: List[str] = [], daemonize: bool = False, daemonize_return: ReturnCode = ReturnCode.PASS, parse_args: Literal[False] = False) Callable[[DF], Callable[[str, str | None, str | None], ReturnCode | None]]
Decorator to define a command
See also
npc.types.Commandfor more information on the parametersChanged in version 0.3.5: Support legacy command system for Nicotine+ < 3.3.0
- Parameters:
func_or_name (
str|npc.types.Callback, optional) – Name of the command or the function to be decorateddescription (
str, optional) – Description of the command, will be extracted from the function’s docstring if not providedaliases (
listofstr, optional) – List of aliases that can be used to call the commanddisabled_interfaces (
listofnpc.types.CommandInterface, optional) – List of CommandInterfaces that the command should be disabled forgroup (
str|npc.types.CommandGroup, optional) – Group used to group the command in the command help windowparameters (
listofstr, optional) – List of parameters that are required to call the command everywhere unless overridden by any of the other parametersparameters_chatroom (
listofstr, optional) – List of parameters that are required to call the command in a chatroomparameters_private_chat (
listofstr, optional) – List of parameters that are required to call the command in a private chatparameters_cli (
listofstr, optional) – List of parameters that are required to call the command in the command line interfacedaemonize (
bool, optional) – Whether the command should be run in a separate thread. These threads are daemons and will not block the main thread. This also means that the return value of the function is predefined asnpc.types.ReturnCode.PASSor set viadaemonize_returndaemonize_return (
npc.types.ReturnCode, optional) – Return code to be returned when the command is daemonized, default isnpc.types.ReturnCode.PASSparse_args (
bool, optional) – Whether the arguments should be parsed according to the function’s annotations
- Returns:
- Decorator if only the options are provided, else the
decorated function
- Return type:
- Raises:
ValueError – If the command name is invalid
ValueError – If the description is not provided