HFormatter

Provides utility classes to format help text for command-line arguments.
- module:
hformatter
- author:
Le Bars, Yoann
- class src.hformatter.HelpFormatter(prog: str, indent_increment: int = 2, max_help_position: int = 24, width: int | None = None, usage_str: str | None = None, default_str: str | None = None)[source]
Bases:
ArgumentDefaultsHelpFormatterClass to format help output.
- Variables:
_usage_string (str) – Title for usage section.
_default_string (str) – Title for default value description.
- add_usage(usage: str, actions: Iterable[Action], groups: Iterable[_ArgumentGroup], prefix: str | None = None) None[source]
Reformat usage message.
- Parameters:
usage (str) – Program command line description.
actions (Iterable[argparse.Action]) – Action identifier.
groups (Iterable[argparse._ArgumentGroup]) – Groups identifier.
prefix (str) – Prefix to usage explanation.
- exception src.hformatter.InvalidCommandLine(message: str)[source]
Bases:
ExceptionException raised when the command line is invalid.
- class src.hformatter.PersonalisedArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True)[source]
Bases:
ArgumentParserA personalised argument parser that raises an exception on error instead of exiting.
This overrides the default error method to raise an InvalidCommandLine exception, which allows the main application to catch parsing errors and exit gracefully with a specific error code.
- error(message: str) None[source]
Handles a parsing error by printing the usage and raising an exception.
- Parameters:
message (str) – Error message.
- Raises:
InvalidCommandLine – If the command line is invalid.
- src.hformatter.create_parser(program_description: str, positional_name: str, optional_name: str, program_version: str, version_message: str, help_message: str, usage_message: str = 'Usage: ', default_message: str = 'default') PersonalisedArgumentParser[source]
# noqa: E501 Creates a personalised ArgumentParser with custom formatting and error handling.
- Parameters:
program_description (str) – String describing the aims of the program.
positional_name (str) – String for positional arguments title.
optional_name (str) – String for optional arguments title.
program_version (str) – String describing program version.
version_message (str) – Help text for the version option.
help_message (str) – String describing the help program option.
usage_message (str) – Title for usage section. Defaults to “Usage: ”.
default_message (str) – Title for default value description. Defaults to “default”.
- Returns:
An argument parser.
- Return type: