WarMAC Parser#

WarMAC’s parser is built with Python’s argparse module. The appearance of the command-line interface has been substantially modified to appear in a clean and organized manner.

Note

Only global variables and constants that are public are documented. Please see the source code for private variable/constant documentation.

warmac_parser.DEFAULT_TIME: int = 10#

The default time that will be used for calculating listing ages.

class warmac_parser.CustomHelpFormat(prog: str, indent_increment: int = 2, max_help_position: int = 24, width: int | None = None)#

Bases: RawDescriptionHelpFormatter

Custom help formatter for warmac_parser.WarMACParser.

Extend argparse.RawDescriptionHelpFormatter to reimplement a few methods. Reimplementations include removing the command metavar tuples, removing the duplicate option metavars, and correcting the over-indentation on the help menu.

__init__(prog: str, indent_increment: int = 2, max_help_position: int = 24, width: int | None = None) None#

Construct a CustomHelpFormat object.

Parameters:
  • prog – The name of the program.

  • indent_increment – How much space should come before the options on the help screen, defaults to 2.

  • max_help_position – The width between indent_increment and the help text, defaults to 24.

  • width – The maximum width that the help screen is able to occupy in the terminal, defaults to None.

_format_action(action: Action) str#

Remove command metavar tuple and fix metavar indentation.

Override the HelpFormatter._format_action method to remove the command metavar tuple and fix the spacings between the option and its associated metavar.

Parameters:

action – The action in which to be formatted.

Returns:

HelpFormatter._format_action(action). If the action is a _SubParsersAction, the metavar tuple will be excluded, and the leading indentation will be corrected.

_format_action_invocation(action: Action) str#

Remove duplicate metavar for options with short and long form.

Override the HelpFormatter._format_action_invocation method to remove the duplicate help metavar for options that have both a short-form and a long-form argument.

Parameters:

action – The action in which to be formatted.

Returns:

The appropriately formatted string.

_iter_indented_subactions(action: Action) Generator[Action, None, None]#

Fix leading indentation for command names in the help menu.

Override the HelpFormatter._iter_indented_subactions method to fix the leading indentation for command names in the help menu.

Parameters:

action – The action to be yielded from.

Yield:

Actions from a list returned by action._get_subactions.

class warmac_parser.WarMACParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True)#

Bases: ArgumentParser

Extend argparse.ArgumentParser to reimplement the error method.

Extend argparse.ArgumentParser to reimplement the error method so it exits with status code 2, and prints to stderr.

error(message: str) NoReturn#

Modify exit message for argparse.ArgumentError.

Modify exit message for argparse.ArgumentError occurrences to print to stderr, and return an exit code of 2.

Parameters:

message – The message provided by the standard argparse.ArgumentParser class.

Returns:

A value is never returned by this function.

warmac_parser._int_checking(usr_inp: str, max_val: int) int#

Return usr_inp as an integer if 0 < usr_inp < max_val.

Cast usr_inp to an integer. If usr_inp is not an integer or is not 0 < usr_inp < max_val, then raise an argparse.ArgumentTypeError.

Parameters:
  • usr_inp – The user’s input as a string.

  • max_val – The maximum value that int(usr_inp) can be.

Raises:

argparse.ArgumentTypeError – Raised if usr_inp is not an integer or is not 0 < int(usr_inp) < max_val.

Returns:

Return usr_inp as an integer.

warmac_parser._create_parser() WarMACParser#

Create the command-line parser for the program.

Create a WarMACParser object that includes global –help and –version options. Create subparsers for multiple commands to be used within the program.

Returns:

The constructed WarMACParser object.

warmac_parser.handle_input() Namespace#

Create a WarMACParser and parse arguments.

Create WarMACParser object, parse command-line arguments, and return the parsed arguments as an argparse.Namespace object. Exits early if only “warmac” is called.

Returns:

The parsed command-line arguments.