Managing Status

Registering a command handler with a decorator and taking effect is cool, but it’s not always the case. Sometimes you may want to disable a command temporarily, or dynamically, without changing the source code. That’s when command status management becomes handy.

Closing and Opening Commands

Given a command greet

@mgr.command
def greet():
    return "Hello!"

To disable or close it, just call :meth”CommandsManager.close

mgr.close("greet")

Note

You should implement your own logic when to close and open commands. For example, check whether the user is granted to do so.

When attempting to execute a closed command, the comamnd handler will not be called, and the execution just returns Config.text_command_closed.

>>> mgr.exec("greet")
'Sorry, this command is currently disabled.'

Note

Pass the name to CommandsManager.close(), not the command handler function.

Opening a command using :meth”CommandsManager.open is the same:

mgr.open("greet")

Introducing Command Registry

Internally, command status and registry is managed in BaseCommandRegistry. By default, CommandsManager will use CommandRegistry, which implements a in-memory registry.

Marking Default Closed

Sometimes a command is more often closed than open. So it’s better to let it be closed until it’s time to open it. That’s what BaseCommandRegistry.mark_default_closed() does.

Note

Why implemented in BaseCommandRegistry instead of CommandsManager? Because marking default closed is completely a registry’s feature, while taking action to actually open and close commands involves updating Context ‘s reference count.

You can either use it as a decorator or just call it with command names. But make sure mark it close before registering the command handler. That’s because on registering, Context ‘s reference count will be updated based on the status. You will get a ValueError if trying to mark after registering the command.

@mgr.command
@mgr.command_reg.mark_default_closed
def hidden(payload):
    return f"You found {payload}"

mgr.command_reg.mark_default_closed("hello")

@mgr.command(groups=["hello"])
def hi():
    return "hi!"

@mgr.command(groups=["hello"])
def aloha():
    return "aloha!"