Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0331e90

Browse files
committed
Issue #11174: Add argparse.MetavarTypeHelpFormatter, which uses type names
for the names of optional and positional arguments in help messages.
1 parent 657bd0a commit 0331e90

4 files changed

Lines changed: 89 additions & 14 deletions

File tree

Doc/library/argparse.rst

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,10 @@ formatter_class
356356
^^^^^^^^^^^^^^^
357357

358358
:class:`ArgumentParser` objects allow the help formatting to be customized by
359-
specifying an alternate formatting class. Currently, there are three such
360-
classes: :class:`argparse.RawDescriptionHelpFormatter`,
361-
:class:`argparse.RawTextHelpFormatter` and
362-
:class:`argparse.ArgumentDefaultsHelpFormatter`. The first two allow more
363-
control over how textual descriptions are displayed, while the last
364-
automatically adds information about argument default values.
359+
specifying an alternate formatting class.
365360

361+
:class:`RawDescriptionHelpFormatter` and :class:`RawTextHelpFormatter` give
362+
more control over how textual descriptions are displayed.
366363
By default, :class:`ArgumentParser` objects line-wrap the description_ and
367364
epilog_ texts in command-line help messages::
368365

@@ -386,7 +383,7 @@ epilog_ texts in command-line help messages::
386383
likewise for this epilog whose whitespace will be cleaned up and whose words
387384
will be wrapped across a couple lines
388385

389-
Passing :class:`argparse.RawDescriptionHelpFormatter` as ``formatter_class=``
386+
Passing :class:`RawDescriptionHelpFormatter` as ``formatter_class=``
390387
indicates that description_ and epilog_ are already correctly formatted and
391388
should not be line-wrapped::
392389

@@ -412,11 +409,11 @@ should not be line-wrapped::
412409
optional arguments:
413410
-h, --help show this help message and exit
414411

415-
:class:`RawTextHelpFormatter` maintains whitespace for all sorts of help text
412+
:class:`RawTextHelpFormatter` maintains whitespace for all sorts of help text,
416413
including argument descriptions.
417414

418-
The other formatter class available, :class:`ArgumentDefaultsHelpFormatter`,
419-
will add information about the default value of each of the arguments::
415+
:class:`ArgumentDefaultsHelpFormatter` automatically adds information about
416+
default values to each of the argument help messages::
420417

421418
>>> parser = argparse.ArgumentParser(
422419
... prog='PROG',
@@ -433,6 +430,25 @@ will add information about the default value of each of the arguments::
433430
-h, --help show this help message and exit
434431
--foo FOO FOO! (default: 42)
435432

433+
:class:`MetavarTypeHelpFormatter` uses the name of the type_ argument for each
434+
argument as as the display name for its values (rather than using the dest_
435+
as the regular formatter does)::
436+
437+
>>> parser = argparse.ArgumentParser(
438+
... prog='PROG',
439+
... formatter_class=argparse.MetavarTypeHelpFormatter)
440+
>>> parser.add_argument('--foo', type=int)
441+
>>> parser.add_argument('bar', type=float)
442+
>>> parser.print_help()
443+
usage: PROG [-h] [--foo int] float
444+
445+
positional arguments:
446+
float
447+
448+
optional arguments:
449+
-h, --help show this help message and exit
450+
--foo int
451+
436452

437453
conflict_handler
438454
^^^^^^^^^^^^^^^^

Lib/argparse.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
'ArgumentDefaultsHelpFormatter',
7272
'RawDescriptionHelpFormatter',
7373
'RawTextHelpFormatter',
74+
'MetavarTypeHelpFormatter',
7475
'Namespace',
7576
'Action',
7677
'ONE_OR_MORE',
@@ -422,7 +423,8 @@ def _format_actions_usage(self, actions, groups):
422423

423424
# produce all arg strings
424425
elif not action.option_strings:
425-
part = self._format_args(action, action.dest)
426+
default = self._get_default_metavar_for_positional(action)
427+
part = self._format_args(action, default)
426428

427429
# if it's in a group, strip the outer []
428430
if action in group_actions:
@@ -444,7 +446,7 @@ def _format_actions_usage(self, actions, groups):
444446
# if the Optional takes a value, format is:
445447
# -s ARGS or --long ARGS
446448
else:
447-
default = action.dest.upper()
449+
default = self._get_default_metavar_for_optional(action)
448450
args_string = self._format_args(action, default)
449451
part = '%s %s' % (option_string, args_string)
450452

@@ -530,7 +532,8 @@ def _format_action(self, action):
530532

531533
def _format_action_invocation(self, action):
532534
if not action.option_strings:
533-
metavar, = self._metavar_formatter(action, action.dest)(1)
535+
default = self._get_default_metavar_for_positional(action)
536+
metavar, = self._metavar_formatter(action, default)(1)
534537
return metavar
535538

536539
else:
@@ -544,7 +547,7 @@ def _format_action_invocation(self, action):
544547
# if the Optional takes a value, format is:
545548
# -s ARGS, --long ARGS
546549
else:
547-
default = action.dest.upper()
550+
default = self._get_default_metavar_for_optional(action)
548551
args_string = self._format_args(action, default)
549552
for option_string in action.option_strings:
550553
parts.append('%s %s' % (option_string, args_string))
@@ -622,6 +625,12 @@ def _fill_text(self, text, width, indent):
622625
def _get_help_string(self, action):
623626
return action.help
624627

628+
def _get_default_metavar_for_optional(self, action):
629+
return action.dest.upper()
630+
631+
def _get_default_metavar_for_positional(self, action):
632+
return action.dest
633+
625634

626635
class RawDescriptionHelpFormatter(HelpFormatter):
627636
"""Help message formatter which retains any formatting in descriptions.
@@ -662,6 +671,22 @@ def _get_help_string(self, action):
662671
return help
663672

664673

674+
class MetavarTypeHelpFormatter(HelpFormatter):
675+
"""Help message formatter which uses the argument 'type' as the default
676+
metavar value (instead of the argument 'dest')
677+
678+
Only the name of this class is considered a public API. All the methods
679+
provided by the class are considered an implementation detail.
680+
"""
681+
682+
def _get_default_metavar_for_optional(self, action):
683+
return action.type.__name__
684+
685+
def _get_default_metavar_for_positional(self, action):
686+
return action.type.__name__
687+
688+
689+
665690
# =====================
666691
# Options and Arguments
667692
# =====================

Lib/test/test_argparse.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3940,6 +3940,37 @@ class TestHelpVersionAction(HelpTestCase):
39403940
'''
39413941
version = ''
39423942

3943+
3944+
class TestHelpMetavarTypeFormatter(HelpTestCase):
3945+
""""""
3946+
3947+
def custom_type(string):
3948+
return string
3949+
3950+
parser_signature = Sig(prog='PROG', description='description',
3951+
formatter_class=argparse.MetavarTypeHelpFormatter)
3952+
argument_signatures = [Sig('a', type=int),
3953+
Sig('-b', type=custom_type),
3954+
Sig('-c', type=float, metavar='SOME FLOAT')]
3955+
argument_group_signatures = []
3956+
usage = '''\
3957+
usage: PROG [-h] [-b custom_type] [-c SOME FLOAT] int
3958+
'''
3959+
help = usage + '''\
3960+
3961+
description
3962+
3963+
positional arguments:
3964+
int
3965+
3966+
optional arguments:
3967+
-h, --help show this help message and exit
3968+
-b custom_type
3969+
-c SOME FLOAT
3970+
'''
3971+
version = ''
3972+
3973+
39433974
# =====================================
39443975
# Optional/Positional constructor tests
39453976
# =====================================

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ Library
291291

292292
- Issue #11388: Added a clear() method to MutableSequence
293293

294+
- Issue #11174: Add argparse.MetavarTypeHelpFormatter, which uses type names
295+
for the names of optional and positional arguments in help messages.
296+
294297
Build
295298
-----
296299

0 commit comments

Comments
 (0)