Skip to content

Commit

Permalink
remove add default option to help strings in argparse
Browse files Browse the repository at this point in the history
Signed-off-by: Micky Yun Chan (michiboo): <[email protected]>
  • Loading branch information
michiboo authored and abadger committed May 2, 2022
1 parent 336bd2f commit dc4babf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
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 comments on commit dc4babf

Please sign in to comment.