Skip to content

CommandRegistry

🛈 DocStrings

Collect commands for later registration.

Source code in src/slashed/registry.py
class CommandRegistry:
    """Collect commands for later registration."""

    def __init__(self) -> None:
        """Initialize registry."""
        self.commands: list[CommandEntry] = []

    def command(
        self,
        *,
        name: str | None = None,
        description: str | None = None,
        category: str = "general",
        usage: str | None = None,
        help_text: str | None = None,
        completer: CompletionProvider | Callable[[], CompletionProvider] | None = None,
        condition: Callable[[], bool] | None = None,
    ) -> Callable[[TCommandFunc], TCommandFunc]:
        """Decorator to collect a command for later registration."""

        def decorator(func: TCommandFunc) -> TCommandFunc:
            cmd_name = name or getattr(func, "__name__", "command").replace("_", "-")
            entry = CommandEntry(
                name=cmd_name,
                func=func,
                description=description,
                category=category,
                usage=usage,
                help_text=help_text,
                completer=completer,
                condition=condition,
            )
            self.commands.append(entry)
            return func

        return decorator

    def register_to(self, store: CommandStore) -> None:
        """Register all collected commands to a store."""
        for entry in self.commands:
            store.add_command(
                name=entry.name,
                fn=entry.func,
                description=entry.description,
                category=entry.category,
                usage=entry.usage,
                help_text=entry.help_text,
                completer=entry.completer,
                condition=entry.condition,
            )

__init__

__init__() -> None

Initialize registry.

Source code in src/slashed/registry.py
def __init__(self) -> None:
    """Initialize registry."""
    self.commands: list[CommandEntry] = []

command

command(
    *,
    name: str | None = None,
    description: str | None = None,
    category: str = "general",
    usage: str | None = None,
    help_text: str | None = None,
    completer: CompletionProvider | Callable[[], CompletionProvider] | None = None,
    condition: Callable[[], bool] | None = None,
) -> Callable[[TCommandFunc], TCommandFunc]

Decorator to collect a command for later registration.

Source code in src/slashed/registry.py
def command(
    self,
    *,
    name: str | None = None,
    description: str | None = None,
    category: str = "general",
    usage: str | None = None,
    help_text: str | None = None,
    completer: CompletionProvider | Callable[[], CompletionProvider] | None = None,
    condition: Callable[[], bool] | None = None,
) -> Callable[[TCommandFunc], TCommandFunc]:
    """Decorator to collect a command for later registration."""

    def decorator(func: TCommandFunc) -> TCommandFunc:
        cmd_name = name or getattr(func, "__name__", "command").replace("_", "-")
        entry = CommandEntry(
            name=cmd_name,
            func=func,
            description=description,
            category=category,
            usage=usage,
            help_text=help_text,
            completer=completer,
            condition=condition,
        )
        self.commands.append(entry)
        return func

    return decorator

register_to

register_to(store: CommandStore) -> None

Register all collected commands to a store.

Source code in src/slashed/registry.py
def register_to(self, store: CommandStore) -> None:
    """Register all collected commands to a store."""
    for entry in self.commands:
        store.add_command(
            name=entry.name,
            fn=entry.func,
            description=entry.description,
            category=entry.category,
            usage=entry.usage,
            help_text=entry.help_text,
            completer=entry.completer,
            condition=entry.condition,
        )

:fa-brands-github: Show source on GitHub