Command

Command is the basic unit of functionality in an application. Each command links a handler to a trigger, which when entered will invoke it for processing.

Command encapsulates all information about a command: its trigger (keyword for invocation), description, set of flags, and set of aliases.


Initialization

1__init__(self, trigger: str, *,
2         description: str | None = None,
3         flags: Flag | Flags = DEFAULT_WITHOUT_FLAGS,
4         aliases: set[str] = DEFAULT_WITHOUT_ALIASES) -> None

Creates a new command for registration in a router.

  • trigger: String trigger that the user enters to invoke the command. Serves as the primary identifier.

  • description: Optional description explaining the command’s purpose. Displayed in help.

  • flags: Set of flags for configuring behavior. Can be a single Flag object or a Flags collection.

  • aliases: Set of string aliases for the main trigger.

Attributes:

trigger

The main command trigger. Used for its identification when processing user input.

description

Text description of the command. If not provided, the default value is used.

registered_flags

A Flags object containing all registered flags. If a Flag was passed, it is automatically converted from a single flag to a collection during initialization.

aliases

Set of string aliases. Empty if no aliases are defined.

Usage example:

 1from argenta.command import Flag, Flags, Command
 2
 3# Simple command without flags
 4hello_cmd = Command("hello", description="Greet the user")
 5
 6# Command with description and aliases
 7quit_cmd = Command("quit", description="Exit the application", aliases=["exit", "q"])
 8
 9# Command with flags
10deploy_cmd = Command(
11    "deploy",
12    description="Deploy application to server",
13    flags=Flags(
14        [
15            Flag("env", possible_values=["dev", "prod"]),
16            Flag("force"),
17        ]
18    ),
19    aliases=["dep"],
20)

See also

More about flags: Flags and Command flags.


Command Registration

Commands are passed as an argument to the @router.command() decorator.

Basic example:

 1from argenta import Command, Response, Router
 2
 3router = Router(title="User Management")
 4
 5
 6@router.command(Command("create-user", description="Create a new user account"))
 7def handle_create_user(response: Response):
 8    print("Creating new user...")
 9
10
11@router.command(
12    Command(
13        "delete-user",
14        description="Delete existing user account",
15        aliases=["remove-user", "rm-user"],
16    )
17)
18def handle_delete_user(response: Response):
19    print("Deleting user...")

Commands with flags:

 1from argenta import Command, Response, Router
 2from argenta.command import Flag, Flags
 3
 4router = Router(title="Server Management")
 5
 6
 7@router.command(
 8    Command(
 9        "start",
10        description="Start the server",
11        flags=Flags(
12            [
13                Flag("port"),
14                Flag("host"),
15                Flag("debug"),
16            ]
17        ),
18        aliases=["run"],
19    )
20)
21def handle_start(response: Response):
22    input_flags = response.input_flags
23    port_flag = input_flags.get_flag_by_name("port")
24    host_flag = input_flags.get_flag_by_name("host")
25    debug_flag = input_flags.get_flag_by_name("debug")
26
27    host = host_flag.input_value if host_flag else "localhost"
28    port = port_flag.input_value if port_flag else "8080"
29    debug = debug_flag and debug_flag.input_value
30
31    print(f"Starting server on {host}:{port}")
32    if debug:
33        print("Debug mode: ON")

Working with Aliases

Aliases allow invoking the same handler with different triggers while preserving the command’s flags and description.

Example with aliases:

 1from argenta import Router, Command, Response
 2
 3router = Router(title="System")
 4
 5
 6@router.command(
 7    Command(
 8        "shutdown",
 9        description="Shutdown the system",
10        aliases=["poweroff", "halt", "stop"]
11    )
12)
13def handle_shutdown(response: Response):
14    print("Shutting down the system...")

Now the user can invoke the command in any of the following ways:

shutdown
poweroff
halt
stop

All these variants will invoke the same handler handle_shutdown.


InputCommand

InputCommand represents a processed command entered by the user. This internal class is created automatically when processing user input. Direct work with it is possible when creating a custom handler for unknown commands.

See also

For more details on custom exception handlers, see here.

Attributes:

trigger

String trigger entered by the user.

input_flags

An InputFlags object containing all entered and parsed flags.