8000 bpo-46080: fix argparse help generation exception in edge case (GH-30… · python/cpython@9e87c0e · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e87c0e

Browse files
authored
bpo-46080: fix argparse help generation exception in edge case (GH-30111)
Fix an uncaught exception during help text generation when argparse.BooleanOptionalAction is used with default=argparse.SUPPRESS and help is specified.
1 parent 30fb6d0 commit 9e87c0e

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ def __init__(self,
881881
option_string = '--no-' + option_string[2:]
882882
_option_strings.append(option_string)
883883

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

887887
super().__init__(

Lib/test/test_argparse.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3626,6 +3626,8 @@ class TestHelpUsage(HelpTestCase):
36263626
Sig('--bar', help='Whether to bar', default=True,
36273627
action=argparse.BooleanOptionalAction),
36283628
Sig('-f', '--foobar', '--barfoo', action=argparse.BooleanOptionalAction),
3629+
Sig('--bazz', action=argparse.BooleanOptionalAction,
3630+
default=argparse.SUPPRESS, help='Bazz!'),
36293631
]
36303632
argument_group_signatures = [
36313633
(Sig('group'), [
@@ -3638,8 +3640,8 @@ class TestHelpUsage(HelpTestCase):
36383640
usage = '''\
36393641
usage: PROG [-h] [-w W [W ...]] [-x [X ...]] [--foo | --no-foo]
36403642
[--bar | --no-bar]
3641-
[-f | --foobar | --no-foobar | --barfoo | --no-barfoo] [-y [Y]]
3642-
[-z Z Z Z]
3643+
[-f | --foobar | --no-foobar | --barfoo | --no-barfoo]
3644+
[--bazz | --no-bazz] [-y [Y]] [-z Z Z Z]
36433645
a b b [c] [d ...] e [e ...]
36443646
'''
36453647
help = usage + '''\
@@ -3656,6 +3658,7 @@ class TestHelpUsage(HelpTestCase):
36563658
--foo, --no-foo Whether to foo
36573659
--bar, --no-bar Whether to bar (default: True)
36583660
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
3661+
--bazz, --no-bazz Bazz!
36593662
36603663
group:
36613664
-y [Y] y
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix exception in argparse help text generation if a
2+
:class:`argparse.BooleanOptionalAction` argument's default is
3+
``argparse.SUPPRESS`` and it has ``help`` specified. Patch by Felix Fontein.

0 commit comments

Comments
 (0)
0