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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| #!/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 License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version. See file `LICENSE` or go to:
https://www.gnu.org/licenses/gpl-3.0.html
"""
# Defines the public names of this module.
__all__ = ['create_parser']
from typing import Iterable
import argparse
# Title for usage section.
usage_string: str = 'Usage: '
# Title for default value description.
default_string: str = 'default'
class HelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
"""
Class to format help output.
:param str __usage_string: Title for usage section.
:param str __default_string: Title for default value description.
"""
__usage_string: str
__default_string: str
def __init__(self, prog: str, indent_increment: int = 2, max_help_position: int = 24,
width: int | None = None) -> None:
"""
Class constructor.
:param str prog: Program name.
:param int indent_increment: Number of blank space when incrementing the text.
:param int max_help_position: Maximum text indent.
:param int width: Line width in help message.
"""
self.__usage_string = usage_string
self.__default_string = default_string
super().__init__(prog, indent_increment, max_help_position, width)
def _get_help_string(self, action: argparse.Action) -> str | None:
"""
Create the help message for a given action.
:param argparse.Action action: Action descriptor.
"""
help_string: str | None = action.help
if '%(default)' not in action.help:
if action.default is not argparse.SUPPRESS:
defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
help_string += f' ({self.__default_string}: %(default)s)'
return help_string
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, # pylint: disable=too-many-arguments
program_version: str, version_message: str,
help_message: str, usage_message: str = usage_string, # pylint: disable=used-prior-global-declaration
default_message: str = default_string) -> argparse.ArgumentParser: # pylint: disable=used-prior-global-declaration
"""
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:
:param str usage_message: Title for default value description. Default to default
:returns: An argument parser.
:rtype: argparse.ArgumentParser
"""
global usage_string, default_string # pylint: disable=global-statement
usage_string = usage_message
default_string = default_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 |
Partager