Error Handling¶
Argenta throws exceptions in edge cases related to user input. By default, they are handled by system handlers, but you can override them. This is done using App instance setters of the form .set_*_handler(). More details about each of them are described below.
Note
No exception goes unhandled, as a default handler is provided for each case. Therefore, overriding is optional.
Usage example:
1from argenta import App
2
3def empty_command_handler():
4 print("Empty command handler called")
5
6app: App = App()
7app.set_empty_command_handler(empty_command_handler)
Possible Exceptions and Non-Standard Behavior¶
UnprocessedInputFlagException: Incorrect Flag Syntax¶
This exception is thrown when the parser cannot process a command due to incorrect syntax. Most often this is related to an error in flag syntax. You can read more about them in the Flags section.
The default handler outputs to the console:
Incorrect flag syntax: <raw input command>
To override, use the .set_incorrect_input_syntax_handler() setter. It accepts a handler with the signature Callable[[str], None], where the only argument is a string with the unprocessed command.
Usage example:
1from argenta import App
2
3def incorrect_input_syntax_handler(raw_command: str):
4 print(f"Incorrect input syntax for command: {raw_command}")
5
6app: App = App()
7app.set_incorrect_input_syntax_handler(incorrect_input_syntax_handler)
RepeatedInputFlagsException: Repeated Flags in Command¶
The exception is thrown if the user entered a command with repeated flags. Two flags (InputFlag) are considered the same if their names match. More about flags and their syntax in the Flags section.
Note
Equality comparison for registered flags (Flag) works differently, see Flag for details.
The default handler outputs to the console:
Repeated input flags: <raw input command>
To override, use the .set_repeated_input_flags_handler() setter. It accepts a handler with the signature Callable[[str], None], where the only argument is a string with the unprocessed command.
Usage example:
1from argenta import App
2
3def repeated_input_flags_handler(raw_command: str):
4 print(f"Repeated input flags: {raw_command}")
5
6app: App = App()
7app.set_repeated_input_flags_handler(repeated_input_flags_handler)
EmptyInputCommandException: Empty Command Entered¶
The exception is thrown if the user entered an empty string or a string consisting only of whitespace characters (\n, \t, space, etc.).
The default handler outputs to the console:
Empty input command
To override, use the .set_empty_command_handler() setter. It accepts a handler with the signature Callable[[], None] (no arguments).
Usage example:
1from argenta import App
2
3def empty_command_handler():
4 print("Empty input command")
5
6app: App = App()
7app.set_empty_command_handler(empty_command_handler)
Handling Unknown Commands¶
This behavior is triggered when the user enters a command that is not registered in any of the routers and is not an alias for an existing command.
The default handler outputs to the console:
Unknown command: <trigger of the input command>
To override, use the .set_unknown_command_handler() setter. It accepts a handler with the signature Callable[[InputCommand], None], where the argument is an InputCommand object.
Usage example:
1from argenta import App
2from argenta.command import InputCommand
3
4def unknown_command_handler(command: InputCommand):
5 print(f"Unknown input command with trigger: {command.trigger}")
6
7app: App = App()
8app.set_unknown_command_handler(unknown_command_handler)
Exiting the Application¶
This behavior is triggered when the user enters a command marked as an exit command.
The default handler outputs text to the console and terminates the application:
See you
To override, use the .set_exit_command_handler() setter. It accepts a handler with the signature Callable[[Response], None], where the argument is a Response object.
Usage example:
1from argenta import App, Response
2
3def exit_command_handler(response: Response):
4 print("Exit command handler")
5
6app: App = App()
7app.set_exit_command_handler(exit_command_handler)