8000 Make BooleanOptionalAction's addition of default to help more similar… · python/cpython@336bd2f · GitHub
[go: up one dir, main page]

Skip to content

Commit 336bd2f

Browse files
committed
Make BooleanOptionalAction's addition of default to help more similar to other actions.
Help for other actions omit the default value if default is SUPPRESS or already contains the special format string '%(default)'. Add those special cases to BooleanOptionalAction's help formatting too. Other actions emit (default: None) when the default value is None. This allows documenting that the unset case is treated differently. Add this functionality to BooleanOptionalAction as well. Fixes https://bugs.python.org/issue44587 so that default=SUPPRESS is not emitted. Fixes https://bugs.python.org/issue38956 as this code will detect whether '%(default)s' has already been specified in the help string.
1 parent b04e02c commit 336bd2f

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

Lib/argparse.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,27 @@ def _copy_items(items):
153153
# Formatting Help
154154
# ===============
155155

156+
def _add_default_to_help_string(action):
157+
"""
158+
Add the default value to the option help message.
159+
160+
ArgumentDefaultsHelpFormatter and BooleanOptionalAction both want to add
161+
the default value to the help message when it isn't already present. This
162+
code will do that, detecting cornercases to prevent duplicates or cases
163+
where it wouldn't make sense to the end user.
164+
"""
165+
help = action.help
166+
if help is None:
167+
help = ''
168+
169+
if '%(default)' not in help:
170+
if action.default is not SUPPRESS:
171+
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
172+
if action.option_strings or action.nargs in defaulting_nargs:
173+
help += ' (default: %(default)s)'
174+
return help
175+
176+
156177
class HelpFormatter(object):
157178
"""Formatter for generating usage messages and argument help strings.
158179
@@ -695,13 +716,7 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter):
695716
"""
696717

697718
def _get_help_string(self, action):
698-
help = action.help
699-
if '%(default)' not in action.help:
700-
if action.default is not SUPPRESS:
701-
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
702-
if action.option_strings or action.nargs in defaulting_nargs:
703-
help += ' (default: %(default)s)'
704-
return help
719+
return _add_default_to_help_string(action)
705720

706721

707722
class MetavarTypeHelpFormatter(HelpFormatter):
@@ -719,7 +734,6 @@ def _get_default_metavar_for_positional(self, action):
719734
return action.type.__name__
720735

721736

722-
723737
# =====================
724738
# Options and Arguments
725739
# =====================
@@ -882,9 +896,6 @@ def __init__(self,
882896
option_string = '--no-' + option_string[2:]
883897
_option_strings.append(option_string)
884898

885-
if help is not None and default is not None and default is not SUPPRESS:
886-
help += " (default: %(default)s)"
887-
888899
super().__init__(
889900
option_strings=_option_strings,
890901
dest=dest,
@@ -896,6 +907,9 @@ def __init__(self,
896907
help=help,
897908
metavar=metavar)
898909

910+
self.help = _add_default_to_help_string(self)
911+
912+
899913
def __call__(self, parser, namespace, values, option_string=None):
900914
if option_string in self.option_strings:
901915
setattr(namespace, self.dest, not option_string.startswith('--no-'))

Lib/test/test_argparse.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3348,6 +3348,7 @@ def _get_parser(self, tester):
33483348
def _test(self, tester, parser_text):
33493349
expected_text = getattr(tester, self.func_suffix)
33503350
expected_text = textwrap.dedent(expected_text)
3351+
tester.maxDiff = None
33513352
tester.assertEqual(expected_text, parser_text)
33523353

33533354
def test_format(self, tester):
@@ -3742,9 +3743,10 @@ class TestHelpUsage(HelpTestCase):
37423743
-h, --help show this help message and exit
37433744
-w W [W ...] w
37443745
-x [X ...] x
3745-
--foo, --no-foo Whether to foo
3746+
--foo, --no-foo Whether to foo (default: None)
37463747
--bar, --no-bar Whether to bar (default: True)
37473748
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
3749+
(default: None)
37483750
--bazz, --no-bazz Bazz!
37493751
37503752
group:
@@ -4423,6 +4425,8 @@ class TestHelpArgumentDefaults(HelpTestCase):
44234425
Sig('--bar', action='store_true', help='bar help'),
44244426
Sig('--taz', action=argparse.BooleanOptionalAction,
44254427
help='Whether to taz it', default=True),
4428+
Sig('--corge', action=argparse.BooleanOptionalAction,
4429+
help='Whether to corge it', default=argparse.SUPPRESS),
44264430
Sig('--quux', help="Set the quux", default=42),
44274431
Sig('spam', help='spam help'),
44284432
Sig('badger', nargs='?', default='wooden', help='badger help'),
@@ -4432,29 +4436,30 @@ class TestHelpArgumentDefaults(HelpTestCase):
44324436
[Sig('--baz', type=int, default=42, help='baz help')]),
44334437
]
44344438
usage = '''\
4435-
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--quux QUUX]
4436-
[--baz BAZ]
4439+
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--corge | --no-corge]
4440+
[--quux QUUX] [--baz BAZ]
44374441
spam [badger]
44384442
'''
44394443
help = usage + '''\
44404444
44414445
description
44424446
44434447
positional arguments:
4444-
spam spam help
4445-
badger badger help (default: wooden)
4448+
spam spam help
4449+
badger badger help (default: wooden)
44464450
44474451
options:
4448-
-h, --help show this help message and exit
4449-
--foo FOO foo help - oh and by the way, None
4450-
--bar bar help (default: False)
4451-
--taz, --no-taz Whether to taz it (default: True)
4452-
--quux QUUX Set the quux (default: 42)
4452+
-h, --help show this help message and exit
4453+
--foo FOO foo help - oh and by the way, None
4454+
--bar bar help (default: False)
4455+
--taz, --no-taz Whether to taz it (default: True)
4456+
--corge, --no-corge Whether to corge it
4457+
--quux QUUX Set the quux (default: 42)
44534458
44544459
title:
44554460
description
44564461
4457-
--baz BAZ baz help (default: 42)
4462+
--baz BAZ baz help (default: 42)
44584463
'''
44594464
version = ''
44604465

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix BooleanOptionalAction to not automatically add a default string when the
2+
default was manually specified using ``%(default)s`` or the default was set to
3+
SUPPRESS. Add a default string for ``default: None`` to differentiate when
4+
the unset state has a different meaning.

0 commit comments

Comments
 (0)
0