CLI Parser¶
- class cli_parser.CustomHelpFormat¶
Bases:
RawDescriptionHelpFormatterCustom help formatter for
cli_parser.WarMACParser.Extend
argparse.RawDescriptionHelpFormatterto 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
CustomHelpFormatobject.- 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_incrementand 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_actionmethod 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_invocationmethod 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_subactionsmethod 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_valas an int ifmin_val <= inp_val < max_val.Cast
inp_valto an integer. Ifinp_valis not an integer or is notmin_val <= inp_val < max_val, then raise anargparse.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_valis not an integer or is notmin_val <= int(inp_val) < max_val.- Returns:
Return
inp_valas an integer.
- class cli_parser.WarMACParser¶
Bases:
ArgumentParserExtend
argparse.ArgumentParserto reimplement method.- error(message: str) NoReturn¶
Modify content of error message printed to stderr.
Modify exit message for
argparse.ArgumentErroroccurrences to print to stderr and return an exit code of 1.- Parameters:
message – Message provided by the standard
argparse.ArgumentParserclass.- Returns:
A value is never returned by this function.
- cli_parser.create_parser() WarMACParser¶
Create the command-line parser for the program.
Create a
WarMACParserobject that includes global –help and –version options. Create subparsers for multiple commands to be used within the program.- Returns:
Constructed
WarMACParserobject.
- cli_parser.handle_input(args: list[str] | None = None) Namespace¶
Create a
WarMACParserand parse arguments.Create
WarMACParserobject, parse command-line arguments, and return the parsed arguments as anargparse.Namespaceobject. 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.