8000 gh-88753: Make BooleanOptionalAction's addition of default to help mo… · python/cpython@20490d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 20490d5

Browse files
abadgermichiboo
andauthored
gh-88753: Make BooleanOptionalAction's addition of default to help more similar to other actions (#27808)
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. 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. Signed-off-by: Micky Yun Chan (michiboo): <chanmickyyun@gmail.com> Co-authored-by: Micky Yun Chan <michan@redhat.com>
1 parent 6c25bf0 commit 20490d5

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

Lib/argparse.py

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

156+
156157
class HelpFormatter(object):
157158
"""Formatter for generating usage messages and argument help strings.
158159
@@ -695,15 +696,27 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter):
695696
"""
696697

697698
def _get_help_string(self, action):
699+
"""
700+
Add the default value to the option help message.
701+
702+
ArgumentDefaultsHelpFormatter and BooleanOptionalAction when it isn't
703+
already present. This code will do that, detecting cornercases to
704+
prevent duplicates or cases where it wouldn't make sense to the end
705+
user.
706+
"""
698707
help = action.help
699-
if '%(default)' not in action.help:
708+
if help is None:
709+
help = ''
710+
711+
if '%(default)' not in help:
700712
if action.default is not SUPPRESS:
701713
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
702714
if action.option_strings or action.nargs in defaulting_nargs:
703715
help += ' (default: %(default)s)'
704716
return help
705717

706718

719+
707720
class MetavarTypeHelpFormatter(HelpFormatter):
708721
"""Help message formatter which uses the argument 'type' as the default
709722
metavar value (instead of the argument 'dest')
@@ -719,7 +732,6 @@ def _get_default_metavar_for_positional(self, action):
719732
return action.type.__name__
720733

721734

722-
723735
# =====================
724736
# Options and Arguments
725737
# =====================
@@ -882,9 +894,6 @@ def __init__(self,
882894
option_string = '--no-' + option_string[2:]
883895
_option_strings.append(option_string)
884896

885-
if help is not None and default is not None and default is not SUPPRESS:
886-
help += " (default: %(default)s)"
887-
888897
super().__init__(
889898
option_strings=_option_strings,
890899
dest=dest,
@@ -896,6 +905,7 @@ def __init__(self,
896905
help=help,
897906
metavar=metavar)
898907

908+
899909
def __call__(self, parser, namespace, values, option_string=None):
900910
if option_string in self.option_strings:
901911
setattr(namespace, self.dest, not option_string.startswith('--no-'))

Lib/test/test_argparse.py

Lines changed: 15 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):
@@ -3743,7 +3744,7 @@ class TestHelpUsage(HelpTestCase):
37433744
-w W [W ...] w
37443745
-x [X ...] x
37453746
--foo, --no-foo Whether to foo
3746-
--bar, --no-bar Whether to bar (default: True)
3747+
--bar, --no-bar Whether to bar
37473748
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
37483749
--bazz, --no-bazz Bazz!
37493750
@@ -4423,6 +4424,8 @@ class TestHelpArgumentDefaults(HelpTestCase):
44234424
Sig('--bar', action='store_true', help='bar help'),
44244425
Sig('--taz', action=argparse.BooleanOptionalAction,
44254426
help='Whether to taz it', default=True),
4427+
Sig('--corge', action=argparse.BooleanOptionalAction,
4428+
help='Whether to corge it', default=argparse.SUPPRESS),
44264429
Sig('--quux', help="Set the quux", default=42),
44274430
Sig('spam', help='spam help'),
44284431
Sig('badger', nargs='?', default='wooden', help='badger help'),
@@ -4432,29 +4435,30 @@ class TestHelpArgumentDefaults(HelpTestCase):
44324435
[Sig('--baz', type=int, default=42, help='baz help')]),
44334436
]
44344437
usage = '''\
4435-
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--quux QUUX]
4436-
[--baz BAZ]
4438+
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--corge | --no-corge]
4439+
[--quux QUUX] [--baz BAZ]
44374440
spam [badger]
44384441
'''
44394442
help = usage + '''\
44404443
44414444
description
44424445
44434446
positional arguments:
4444-
spam spam help
4445-
badger badger help (default: wooden)
4447+
spam spam help
4448+
badger badger help (default: wooden)
44464449
44474450
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)
4451+
-h, --help show this help message and exit
4452+
--foo FOO foo help - oh and by the way, None
4453+
--bar bar help (default: False)
4454+
--taz, --no-taz Whether to taz it (default: True)
4455+
--corge, --no-corge Whether to corge it
4456+
--quux QUUX Set the quux (default: 42)
44534457
44544458
title:
44554459
description
44564460
4457-
--baz BAZ baz help (default: 42)
4461+
--baz BAZ baz help (default: 42)
44584462
'''
44594463
version = ''
44604464

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix BooleanOptionalAction to not automatically add a default string. If a
2+
default string is desired, use a formatter to add it.

0 commit comments

Comments
 (0)
0