ArgParser¶
ArgParser is designed for processing command-line arguments passed to the application at startup. It’s important not to confuse them with flags that the user enters in interactive mode. ArgParser allows receiving external configuration at startup (e.g., path to settings file, debug flags, or launch mode).
Initialization¶
1def __init__(self, processed_args: list[ValueArgument | BooleanArgument], *,
2 name: str = "Argenta",
3 description: str = "Argenta available arguments",
4 epilog: str = "github.com/koloideal/Argenta | made by kolo")
Creates an instance of the command-line argument parser.
processed_args: List of arguments to process at application startup. For more details, see here.name: Application name for display in help.description: Application description for display in help.epilog: Additional information for display at the end of help.
Attributes¶
- parsed_argspace: ArgSpace
ArgSpaceinstance containing all processed command-line arguments. For more details, see here.
Caution
Before initializing Orchestrator, to whose constructor an ArgParser instance was passed, the parsed_argspace attribute will contain an empty ArgSpace.
Parsing and validation of arguments occur during Orchestrator initialization, so using parsed_argspace is advisable only after that.
Best Practices¶
Using the parsed_argspace attribute is recommended only during the application setup phase. In handlers, the best practice is to obtain ArgSpace through DI. For more details, see here.
Usage example:
1from argenta import App, Orchestrator
2from argenta.orchestrator.argparser import ArgParser, BooleanArgument
3
4arg_parser = ArgParser(
5 processed_args=[
6 BooleanArgument("dev")
7 ]
8)
9orchestrator = Orchestrator(
10 arg_parser=arg_parser,
11)
12
13if __name__ == "__main__":
14 if arg_parser.parsed_argspace.get_by_name("dev"):
15 orchestrator.start_polling(App(initial_message="ArgentaDev"))
16 else:
17 orchestrator.start_polling(App())
Error Handling¶
See also
For more details on argument types, see Arguments
When working with command-line arguments, the standard ArgumentParser automatically handles the following situations:
Missing required argument:
$ python app.py
usage: Argenta [-h] --config CONFIG
Argenta: error: the following arguments are required: --config
Invalid value from possible_values list:
$ python app.py --config app.yaml --log-level TRACE
usage: Argenta [-h] --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
Argenta: error: argument --log-level: invalid choice: 'TRACE'
Using a deprecated argument:
When using an argument with is_deprecated=True, a warning is displayed, but execution continues:
$ python app.py --old-param value
Warning: argument --old-param is deprecated
Warning
Параметр поддерживается начиная с версии CPython 3.13, если версия ниже, то параметр будет игнорироваться.