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 1 commit
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
Prev Previous commit
remove add default option to help strings in argparse
Signed-off-by: Micky Yun Chan (michiboo): <chanmickyyun@gmail.com>
  • Loading branch information
michiboo authored and abadger committed May 2, 2022
commit dc4babf8077a6cae5ecb2f8ecd20abde0c4e318b
42 changes: 19 additions & 23 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,6 @@ def _copy_items(items):
# Formatting Help
# ===============

def _add_default_to_help_string(action):
"""
Add the default value to the option help message.

ArgumentDefaultsHelpFormatter and BooleanOptionalAction both want to add
the default value to the help message 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 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 HelpFormatter(object):
"""Formatter for generating usage messages and argument help strings.
Expand Down Expand Up @@ -716,7 +696,25 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter):
"""

def _get_help_string(self, action):
return _add_default_to_help_string(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 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):
Expand Down Expand Up @@ -907,8 +905,6 @@ def __init__(self,
help=help,
metavar=metavar)

self.help = _add_default_to_help_string(self)


def __call__(self, parser, namespace, values, option_string=None):
if option_string in self.option_strings:
Expand Down
5 changes: 2 additions & 3 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3743,10 +3743,9 @@ class TestHelpUsage(HelpTestCase):
-h, --help show this help message and exit
-w W [W ...] w
-x [X ...] x
--foo, --no-foo Whether to foo (default: None)
--bar, --no-bar Whether to bar (default: True)
--foo, --no-foo Whether to foo
--bar, --no-bar Whether to bar
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
(default: None)
--bazz, --no-bazz Bazz!

group:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
Fix BooleanOptionalAction to not automatically add a default string when the
default was manually specified using ``%(default)s`` or the default was set to
SUPPRESS. Add a default string for ``default: None`` to differentiate when
the unset state has a different meaning.
Fix BooleanOptionalAction to not automatically add a default string. If a
default string is desired, use a formatter to add it.
0