imxdparser

Description

The MainParser and ChildParser allows the creation of a fully intermixed argument parser:

Examples

# Start the parser so that the following is valid:
# ./app (...) --cfgfile filename (...)
main_parser = MainParser()
main_parser.add_argument("--version", action="version", version="1.2.3")
main_parser.add_argument("-c", "--cfgfile")
main_parser.attach()

# Add the "one" and "two" options:
subparser = ChildParser(main_parser, "one")
subparser.add_argument("file")
subparser.attach()
subparser = ChildParser(main_parser, "two")
subparser.add_argument("file")
subparser.attach()

# Result (all the following are valid)
# ./app one --cfgfile filename
# ./app --cfgfile filename one
# ./app two --cfgfile filename
# ./app --cfgfile filename two

Classes

class imxdparser.imxdparser.BaseParser(**kwargs)
add_argument(*args, **kwargs) None

The add_argument() method attaches individual argument specifications to the parser. It supports positional arguments, options that accept values, and on/off flags.

set_defaults(**kwargs: Dict[str, Any]) None

set_defaults() allows some additional attributes that are determined without any inspection of the command line to be added

class imxdparser.imxdparser.MainParser(prog: str | None = None, usage: str | None = None, description: str | None = None, epilog: str | None = None, formatter_class=<class 'argparse.RawDescriptionHelpFormatter'>, prefix_chars: str = '-', fromfile_prefix_chars: str | None = None, argument_default=None, conflict_handler: str = 'error', add_help: bool | None = True, allow_abbrev: bool | None = True, exit_on_error: bool | None = True)

Bases: BaseParser

Root parser which holds as many child parsers as you wish

__init__(prog: str | None = None, usage: str | None = None, description: str | None = None, epilog: str | None = None, formatter_class=<class 'argparse.RawDescriptionHelpFormatter'>, prefix_chars: str = '-', fromfile_prefix_chars: str | None = None, argument_default=None, conflict_handler: str = 'error', add_help: bool | None = True, allow_abbrev: bool | None = True, exit_on_error: bool | None = True)

Create a base parser that holds an extra internal argument parser.

Parameters:
  • prog (dict[str]) – The name of the program (default: sys.argv[0])

  • usage (str) – A usage message (default: auto-generated from arguments)

  • description (str) – A description of what the program does

  • epilog (str) – Text following the argument descriptions

  • formatter_class – HelpFormatter class for printing help messages. Note: changed from default argparse.HelpFormatter

  • prefix_chars (str) – Characters that prefix optional arguments

  • fromfile_prefix_chars – Characters that prefix files containing additional arguments

  • argument_default – The default value for all arguments

  • conflict_handler (str) – String indicating how to handle conflicts

  • add_help (boolean) – Add a -h/-help option

  • allow_abbrev (bool) – Allow long options to be abbreviated unambiguously

  • exit_on_error (bool) – Determines whether or not ArgumentParser exits with error info when an error occurs

attach() None

Finalize parser.

parse_args(args: List[str] = None, namespace: object = None)

Convert argument strings to objects and assign them as attributes of the namespace. Return the populated namespace.

Parameters:
  • args (list[str]) – List of strings to parse. The default is taken from sys.argv.

  • namespace (object) – An object to take the attributes. The default is a new empty Namespace object.

class imxdparser.imxdparser.ChildParser(parent=None, prog: str | None = None, usage: str | None = None, description: str | None = None, epilog: str | None = None, formatter_class=<class 'argparse.RawDescriptionHelpFormatter'>, prefix_chars: str = '-', fromfile_prefix_chars: str | None = None, argument_default=None, conflict_handler: str = 'error', add_help: bool | None = True, allow_abbrev: bool | None = True, exit_on_error: bool | None = True)

Bases: BaseParser

Child parser which holds as many other child parsers as you wish

__init__(parent=None, prog: str | None = None, usage: str | None = None, description: str | None = None, epilog: str | None = None, formatter_class=<class 'argparse.RawDescriptionHelpFormatter'>, prefix_chars: str = '-', fromfile_prefix_chars: str | None = None, argument_default=None, conflict_handler: str = 'error', add_help: bool | None = True, allow_abbrev: bool | None = True, exit_on_error: bool | None = True)

Create a child parser that holds an extra internal argument parser.

Parameters:
  • parent – MainParser or ChildParser this parser depends on.

  • prog (dict[str]) – The name of the program (default: sys.argv[0])

  • usage (str) – A usage message (default: auto-generated from arguments)

  • description (str) – A description of what the program does

  • epilog (str) – Text following the argument descriptions

  • formatter_class – HelpFormatter class for printing help messages. Note: changed from default argparse.HelpFormatter

  • prefix_chars (str) – Characters that prefix optional arguments

  • fromfile_prefix_chars – Characters that prefix files containing additional arguments

  • argument_default – The default value for all arguments

  • conflict_handler (str) – String indicating how to handle conflicts

  • add_help (boolean) – Add a -h/-help option

  • allow_abbrev (bool) – Allow long options to be abbreviated unambiguously

  • exit_on_error (bool) – Determines whether or not ArgumentParser exits with error info when an error occurs

attach() None

Finalize parser and attach to parent parser

See also

Module argparse

Documentation of the argparse standard module.