CommandParser
Customized ArgumentParser class to improve some error messages and prevent SystemExit in several occasions, as SystemExit is unacceptable when a command is called programmatically.
Attributes
| Attribute | Type | Description |
|---|---|---|
| missing_args_message | string | Custom error message displayed when required arguments are missing during parsing. |
| called_from_command_line | boolean | Boolean flag determining if errors should trigger a SystemExit or raise a CommandError exception. |
Constructor
Signature
def CommandParser(
missing_args_message: string = None,
called_from_command_line: boolean = None,
**kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| missing_args_message | string = None | Custom message to display when required arguments are missing. |
| called_from_command_line | boolean = None | Flag indicating if the parser was invoked via the CLI to determine error handling behavior. |
| **kwargs | dict | Arbitrary keyword arguments passed to the parent ArgumentParser class. |
Signature
def CommandParser(
missing_args_message: string = null,
called_from_command_line: boolean = null,
**kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| missing_args_message | string = null | The custom error message to display when required arguments are not provided |
| called_from_command_line | boolean = null | A flag indicating if the parser was invoked via the CLI, determining whether to exit or raise a CommandError |
| **kwargs | dict | Additional keyword arguments passed to the parent ArgumentParser constructor |
Methods
parse_args()
@classmethod
def parse_args(
args: list = null,
namespace: object = null
) - > Namespace
Parses the provided argument strings into a namespace object while validating for missing required arguments.
Parameters
| Name | Type | Description |
|---|---|---|
| args | list = null | A list of strings representing the command-line arguments to parse |
| namespace | object = null | An existing object to populate with attributes instead of creating a new Namespace |
Returns
| Type | Description |
|---|---|
Namespace | An object containing the populated attributes from the parsed command-line arguments |
error()
@classmethod
def error(
message: string
) - > null
Handles parsing errors by either calling the system error handler or raising a CommandError depending on the execution context.
Parameters
| Name | Type | Description |
|---|---|---|
| message | string | The error message describing the parsing failure |
Returns
| Type | Description |
|---|---|
null | None |
add_subparsers()
@classmethod
def add_subparsers(
**kwargs: dict
) - > _SubParsersAction
Adds a sub-command parser to the current parser, ensuring child parsers inherit the execution context and class type.
Parameters
| Name | Type | Description |
|---|---|---|
| **kwargs | dict | Keyword arguments for sub-parser configuration, including the parser_class to use for sub-commands |
Returns
| Type | Description |
|---|---|
_SubParsersAction | An action object that allows adding specific sub-commands to the parser |