8000 gh-88753: Make BooleanOptionalAction's addition of default to help more similar to other actions by abadger · Pull Request #27808 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-88753: Make BooleanOptionalAction's addition of default to help more similar to other actions #27808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def _copy_items(items):
# Formatting Help
# ===============


class HelpFormatter(object):
"""Formatter for generating usage messages and argument help strings.

Expand Down Expand Up @@ -695,15 +696,27 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter):
"""

def _get_help_string(self, action):
"""
Add the default value to the option help message.

ArgumentDefaultsHelpFormatter and BooleanOptionalAction when it isn't
already present. This code will do that, detecting cornercases to
prevent duplicates or cases where it wouldn't make sense to the end
user.
"""
help = action.help
if '%(default)' not in action.help:
if help is None:
help = ''

if '%(default)' not in help:
if action.default is not SUPPRESS:
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
help += ' (default: %(default)s)'
return help



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



# =====================
# Options and Arguments
# =====================
Expand Down Expand Up @@ -882,9 +894,6 @@ def __init__(self,
option_string = '--no-' + option_string[2:]
_option_strings.append(option_string)

if help is not None and default is not None and default is not SUPPRESS:
help += " (default: %(default)s)"

super().__init__(
option_strings=_option_strings,
dest=dest,
Expand All @@ -896,6 +905,7 @@ def __init__(self,
help=help,
metavar=metavar)


def __call__(self, parser, namespace, values, option_string=None):
if option_string in self.option_strings:
setattr(namespace, self.dest, not option_string.startswith('--no-'))
Expand Down
26 changes: 15 additions & 11 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3348,6 +3348,7 @@ def _get_parser(self, tester):
def _test(self, tester, parser_text):
expected_text = getattr(tester, self.func_suffix)
expected_text = textwrap.dedent(expected_text)
tester.maxDiff = None
tester.assertEqual(expected_text, parser_text)

def test_format(self, tester):
Expand Down Expand Up @@ -3743,7 +3744,7 @@ class TestHelpUsage(HelpTestCase):
-w W [W ...] w
-x [X ...] x
--foo, --no-foo Whether to foo
--bar, --no-bar Whether to bar (default: True)
--bar, --no-bar Whether to bar
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
--bazz, --no-bazz Bazz!

Expand Down Expand Up @@ -4423,6 +4424,8 @@ class TestHelpArgumentDefaults(HelpTestCase):
Sig('--bar', action='store_true', help='bar help'),
Sig('--taz', action=argparse.BooleanOptionalAction,
help='Whether to taz it', default=True),
Sig('--corge', action=argparse.BooleanOptionalAction,
help='Whether to corge it', default=argparse.SUPPRESS),
Sig('--quux', help="Set the quux", default=42),
Sig('spam', help='spam help'),
Sig('badger', nargs='?', default='wooden', help='badger help'),
Expand All @@ -4432,29 +4435,30 @@ class TestHelpArgumentDefaults(HelpTestCase):
[Sig('--baz', type=int, default=42, help='baz help')]),
]
usage = '''\
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--quux QUUX]
[--baz BAZ]
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--corge | --no-corge]
[--quux QUUX] [--baz BAZ]
spam [badger]
'''
help = usage + '''\

description

positional arguments:
spam spam help
badger badger help (default: wooden)
spam spam help
badger badger help (default: wooden)

options:
-h, --help show this help message and exit
--foo FOO foo help - oh and by the way, None
--bar bar help (default: False)
--taz, --no-taz Whether to taz it (default: True)
--quux QUUX Set the quux (default: 42)
-h, --help show this help message and exit
--foo FOO foo help - oh and by the way, None
--bar bar help (default: False)
--taz, --no-taz Whether to taz it (default: True)
--corge, --no-corge Whether to corge it
--quux QUUX Set the quux (default: 42)

title:
description

--baz BAZ baz help (default: 42)
--baz BAZ baz help (default: 42)
'''
version = ''

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix BooleanOptionalAction to not automatically add a default string. If a
default string is desired, use a formatter to add it.
0