Skip to content

Commit e2920b1

Browse files
mhilsmiss-islington
authored andcommitted
bpo-38956: don't print BooleanOptionalAction's default twice (pythonGH-27672)
Co-authored-by: Micky Yun Chan <[email protected]> (cherry picked from commit 1512bc2) Co-authored-by: Maximilian Hils <[email protected]>
1 parent 8516ca5 commit e2920b1

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ def __init__(self,
875875
_option_strings.append(option_string)
876876

877877
if help is not None and default is not None:
878-
help += f" (default: {default})"
878+
help += " (default: %(default)s)"
879879

880880
super().__init__(
881881
option_strings=_option_strings,

Lib/test/test_argparse.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4262,6 +4262,9 @@ class TestHelpArgumentDefaults(HelpTestCase):
42624262
argument_signatures = [
42634263
Sig('--foo', help='foo help - oh and by the way, %(default)s'),
42644264
Sig('--bar', action='store_true', help='bar help'),
4265+
Sig('--taz', action=argparse.BooleanOptionalAction,
4266+
help='Whether to taz it', default=True),
4267+
Sig('--quux', help="Set the quux", default=42),
42654268
Sig('spam', help='spam help'),
42664269
Sig('badger', nargs='?', default='wooden', help='badger help'),
42674270
]
@@ -4270,25 +4273,29 @@ class TestHelpArgumentDefaults(HelpTestCase):
42704273
[Sig('--baz', type=int, default=42, help='baz help')]),
42714274
]
42724275
usage = '''\
4273-
usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger]
4276+
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--quux QUUX]
4277+
[--baz BAZ]
4278+
spam [badger]
42744279
'''
42754280
help = usage + '''\
42764281
42774282
description
42784283
42794284
positional arguments:
4280-
spam spam help
4281-
badger badger help (default: wooden)
4285+
spam spam help
4286+
badger badger help (default: wooden)
42824287
42834288
options:
4284-
-h, --help show this help message and exit
4285-
--foo FOO foo help - oh and by the way, None
4286-
--bar bar help (default: False)
4289+
-h, --help show this help message and exit
4290+
--foo FOO foo help - oh and by the way, None
4291+
--bar bar help (default: False)
4292+
--taz, --no-taz Whether to taz it (default: True)
4293+
--quux QUUX Set the quux (default: 42)
42874294
42884295
title:
42894296
description
42904297
4291-
--baz BAZ baz help (default: 42)
4298+
--baz BAZ baz help (default: 42)
42924299
'''
42934300
version = ''
42944301

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:class:`argparse.BooleanOptionalAction`'s default value is no longer printed twice when used with :class:`argparse.ArgumentDefaultsHelpFormatter`.

0 commit comments

Comments
 (0)