Index: Doc/library/argparse.rst =================================================================== --- Doc/library/argparse.rst (wersja 88552) +++ Doc/library/argparse.rst (kopia robocza) @@ -158,6 +158,8 @@ :data:`sys.argv[0]`) * usage_ - The string describing the program usage (default: generated) + + * metavars_ - How metavars should be created for actions The following sections describe how each of these are used. @@ -566,6 +568,27 @@ your usage messages. +metavars +^^^^^^^^ + +This option allows different way of creating of metavar names. Currently it +accepts :type: object, which produces type names instead of default metavar +names when that's possible:: + + >>> parser = argparse.ArgumentParser(prog="PROG", metavars=type) + >>> parser.add_argument('--first', metavar='META', type=int) + >>> parser.add_argument('--second', type=open) + >>> parser.add_argument('--third') + >>> parser.print_help() + usage: PROG [-h] [--first META] [--second open] [--third THIRD] + + optional arguments: + -h, --help show this help message and exit + --first META + --second open + --third THIRD + + The add_argument() method ------------------------- Index: Lib/argparse.py =================================================================== --- Lib/argparse.py (wersja 88552) +++ Lib/argparse.py (kopia robocza) @@ -154,7 +154,8 @@ prog, indent_increment=2, max_help_position=24, - width=None): + width=None, + metavars=None): # default setting for width if width is None: @@ -168,6 +169,7 @@ self._indent_increment = indent_increment self._max_help_position = max_help_position self._width = width + self._metavars = metavars self._current_indent = 0 self._level = 0 @@ -554,6 +556,8 @@ def _metavar_formatter(self, action, default_metavar): if action.metavar is not None: result = action.metavar + elif self._metavars is type and action.type is not None: + result = action.type.__name__ elif action.choices is not None: choice_strs = [str(choice) for choice in action.choices] result = '{%s}' % ','.join(choice_strs) @@ -1557,7 +1561,8 @@ fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', - add_help=True): + add_help=True, + metavars=None): if version is not None: import warnings @@ -1584,6 +1589,7 @@ self.formatter_class = formatter_class self.fromfile_prefix_chars = fromfile_prefix_chars self.add_help = add_help + self.metavars = metavars add_group = self.add_argument_group self._positionals = add_group(_('positional arguments')) @@ -2307,7 +2313,7 @@ return formatter.format_help() def _get_formatter(self): - return self.formatter_class(prog=self.prog) + return self.formatter_class(prog=self.prog, metavars=self.metavars) # ===================== # Help-printing methods Index: Lib/test/test_argparse.py =================================================================== --- Lib/test/test_argparse.py (wersja 88552) +++ Lib/test/test_argparse.py (kopia robocza) @@ -3940,6 +3940,30 @@ ''' version = '' + +class TestHelpMetavarType(HelpTestCase): + """""" + + parser_signature = Sig(prog='PROG', description='description', metavars=type) + argument_signatures = [Sig('-a', type=int), Sig('-b', type=open), + Sig('-c', type=float, metavar='SOME FLOAT')] + argument_group_signatures = [] + usage = '''\ + usage: PROG [-h] [-a int] [-b open] [-c SOME FLOAT] + ''' + help = usage + '''\ + + description + + optional arguments: + -h, --help show this help message and exit + -a int + -b open + -c SOME FLOAT + ''' + version = '' + + # ===================================== # Optional/Positional constructor tests # =====================================