8000 remove add default option to help strings in argparse · python/cpython@dc4babf · GitHub
[go: up one dir, main page]

Skip to content

Commit dc4babf

Browse files
michibooabadger
authored andcommitted
remove add default option to help strings in argparse
Signed-off-by: Micky Yun Chan (michiboo): <chanmickyyun@gmail.com>
1 parent 336bd2f commit dc4babf

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

Lib/argparse.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -153,26 +153,6 @@ 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-
176156

177157
class HelpFormatter(object):
178158
"""Formatter for generating usage messages and argument help strings.
@@ -716,7 +696,25 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter):
716696
"""
717697

718698
def _get_help_string(self, action):
719-
return _add_default_to_help_string(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+
"""
707+
help = action.help
708+
if help is None:
709+
help = ''
710+
711+
if '%(default)' not in help:
712+
if action.default is not SUPPRESS:
713+
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
714+
if action.option_strings or action.nargs in defaulting_nargs:
715+
help += ' (default: %(default)s)'
716+
return help
717+
720718

721719

722720
class MetavarTypeHelpFormatter(HelpFormatter):
@@ -907,8 +905,6 @@ def __init__(self,
907905
help=help,
908906
metavar=metavar)
909907

910-
self.help = _add_default_to_help_string(self)
911-
912908

913909
def __call__(self, parser, namespace, values, option_string=None):
914910
if option_string in self.option_strings:

Lib/test/test_argparse.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3743,10 +3743,9 @@ class TestHelpUsage(HelpTestCase):
37433743
-h, --help show this help message and exit
37443744
-w W [W ...] w
37453745
-x [X ...] x
3746-
--foo, --no-foo Whether to foo (default: None)
3747-
--bar, --no-bar Whether to bar (default: True)
3746+
--foo, --no-foo Whether to foo
3747+
--bar, --no-bar Whether to bar
37483748
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
3749-
(default: None)
37503749
--bazz, --no-bazz Bazz!
37513750
37523751
group:
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
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.
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