Skip to main content

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

AttributeTypeDescription
missing_args_messagestringCustom error message displayed when required arguments are missing during parsing.
called_from_command_linebooleanBoolean 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

NameTypeDescription
missing_args_messagestring = NoneCustom message to display when required arguments are missing.
called_from_command_lineboolean = NoneFlag indicating if the parser was invoked via the CLI to determine error handling behavior.
**kwargsdictArbitrary 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

NameTypeDescription
missing_args_messagestring = nullThe custom error message to display when required arguments are not provided
called_from_command_lineboolean = nullA flag indicating if the parser was invoked via the CLI, determining whether to exit or raise a CommandError
**kwargsdictAdditional 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

NameTypeDescription
argslist = nullA list of strings representing the command-line arguments to parse
namespaceobject = nullAn existing object to populate with attributes instead of creating a new Namespace

Returns

TypeDescription
NamespaceAn 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

NameTypeDescription
messagestringThe error message describing the parsing failure

Returns

TypeDescription
nullNone

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

NameTypeDescription
**kwargsdictKeyword arguments for sub-parser configuration, including the parser_class to use for sub-commands

Returns

TypeDescription
_SubParsersActionAn action object that allows adding specific sub-commands to the parser