1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Utility class to format help text.
:module: hformatter
:author: Le*Bars, Yoann
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public Licence as published by the
Free Software Foundation; either version 3 of the Licence, or (at your
option) any later version. See file `LICENSE` or go to:
https://www.gnu.org/licenses/gpl-3.0.html
"""
from typing import Iterable
import argparse
# Title for usage section.
usage_string: str = 'Usage: '
class HelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
"""
Class to format help output.
:param str __usage_string: Title for usage section.
"""
__usage_string: str
def __init__(self, prog: str, indent_increment: int = 2, max_help_position: int = 24,
width: int | None = None) -> None:
self.__usage_string = usage_string
super().__init__(prog, indent_increment, max_help_position, width)
def add_usage(self, usage: str, actions: Iterable[argparse.Action],
groups: Iterable[argparse._ArgumentGroup],
prefix: str | None = None) -> None:
"""
Reformat usage message.
:param str usage: Program command line description.
:param str actions: Action identifier.
:param str groups: Groups identifier.
:param str prefix: Prefix to usage explanation.
:returns: Object describing usage string.
:rtype: None
"""
if prefix is None:
prefix = self.__usage_string
return super().add_usage(usage, actions, groups, prefix)
def create_parser(program_description: str, positional_name: str, optional_name: str,
program_version: str, version_message: str,
help_message: str, usage_message: str = 'Usage: ') -> argparse.ArgumentParser:
"""
Generates an argument parser.
:param str program_description: String describing the aims of the program.
:param str positional_name: String for positional arguments title.
:param str optional_name: String for optional arguments title.
:param str program_version: String describing program version.
:param str version_message: String describing the version program option.
:param str help_message: String describing the help program option.
:param str usage_message: Title for usage section. Default to Usage:
:returns: An argument parser.
:rtype: argparse.ArgumentParser
"""
global usage_string
usage_string = usage_message
# Command line parser.
parser = argparse.ArgumentParser(add_help=False,
formatter_class=HelpFormatter,
description=program_description)
parser._positionals.title = positional_name # pylint: disable=protected-access
parser._optionals.title = optional_name # pylint: disable=protected-access
parser.add_argument('-V', '--version', action='version', version='%(prog)s ' + program_version,
help=version_message)
parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS,
help=help_message)
return parser
# Defines the public names of this module.
__all__ = ['create_parser'] |
Partager