CLI 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.
class cli_parser.CustomHelpFormat

Bases: RawDescriptionHelpFormatter

Custom help formatter for cli_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 – Name of the program.

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

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

  • width – 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.

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 – 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 – Action in which to be formatted.

Returns:

Appropriately formatted string.

_iter_indented_subactions(action: Action) Generator[Action]

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 – Action to be assessed for correct indentation.

Yield:

Actions from a list returned by action._get_subactions.

cli_parser.str_to_int_bounds_check(inp_val: str, min_val: int, max_val: int) int

Return inp_val as an int if min_val <= inp_val <  max_val.

Cast inp_val to an integer. If inp_val is not an integer or is not min_val <= inp_val < max_val, then raise an argparse.ArgumentTypeError.

Parameters:
  • inp_val – User’s input as a string.

  • min_val – Minimum value that int(inp_val) can be.

  • max_val – Maximum value that int(inp_val) can be.

Raises:

argparse.ArgumentTypeError – Raised if inp_val is not an integer or is not min_val <= int(inp_val) < max_val.

Returns:

Return inp_val as an integer.

class cli_parser.WarMACParser

Bases: ArgumentParser

Extend argparse.ArgumentParser to reimplement method.

error(message: str) NoReturn

Modify content of error message printed to stderr.

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

Parameters:

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

Returns:

A value is never returned by this function.

cli_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:

Constructed WarMACParser object.

cli_parser.handle_input(args: list[str] | None = None) 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 or if the subcommand “help” is used. Print to stderr if only “warmac” or a bare subcommand (ex: “warmac average”) is called (excluding “help” subcommand). Print to stdout if “–help” option is used, or if the help subcommand is called (even if it’s bare).

When calling as a function, omit leading “warmac” and supply the remaining arguments accordingly. Ex: handle_input(["average", "bite"])

Parameters:

args – Substituted command line arguments, defaults to None

Returns:

Parsed command-line arguments.