mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
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.
(cherry picked from commit 9e87c0e03f
)
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
876ade1ae3
commit
e5edc8d737
3 changed files with 9 additions and 3 deletions
|
@ -878,7 +878,7 @@ class BooleanOptionalAction(Action):
|
||||||
option_string = '--no-' + option_string[2:]
|
option_string = '--no-' + option_string[2:]
|
||||||
_option_strings.append(option_string)
|
_option_strings.append(option_string)
|
||||||
|
|
||||||
if help is not None and default is not None:
|
if help is not None and default is not None and default is not SUPPRESS:
|
||||||
help += " (default: %(default)s)"
|
help += " (default: %(default)s)"
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
|
|
|
@ -3597,6 +3597,8 @@ class TestHelpUsage(HelpTestCase):
|
||||||
Sig('--bar', help='Whether to bar', default=True,
|
Sig('--bar', help='Whether to bar', default=True,
|
||||||
action=argparse.BooleanOptionalAction),
|
action=argparse.BooleanOptionalAction),
|
||||||
Sig('-f', '--foobar', '--barfoo', action=argparse.BooleanOptionalAction),
|
Sig('-f', '--foobar', '--barfoo', action=argparse.BooleanOptionalAction),
|
||||||
|
Sig('--bazz', action=argparse.BooleanOptionalAction,
|
||||||
|
default=argparse.SUPPRESS, help='Bazz!'),
|
||||||
]
|
]
|
||||||
argument_group_signatures = [
|
argument_group_signatures = [
|
||||||
(Sig('group'), [
|
(Sig('group'), [
|
||||||
|
@ -3609,8 +3611,8 @@ class TestHelpUsage(HelpTestCase):
|
||||||
usage = '''\
|
usage = '''\
|
||||||
usage: PROG [-h] [-w W [W ...]] [-x [X ...]] [--foo | --no-foo]
|
usage: PROG [-h] [-w W [W ...]] [-x [X ...]] [--foo | --no-foo]
|
||||||
[--bar | --no-bar]
|
[--bar | --no-bar]
|
||||||
[-f | --foobar | --no-foobar | --barfoo | --no-barfoo] [-y [Y]]
|
[-f | --foobar | --no-foobar | --barfoo | --no-barfoo]
|
||||||
[-z Z Z Z]
|
[--bazz | --no-bazz] [-y [Y]] [-z Z Z Z]
|
||||||
a b b [c] [d ...] e [e ...]
|
a b b [c] [d ...] e [e ...]
|
||||||
'''
|
'''
|
||||||
help = usage + '''\
|
help = usage + '''\
|
||||||
|
@ -3627,6 +3629,7 @@ class TestHelpUsage(HelpTestCase):
|
||||||
--foo, --no-foo Whether to foo
|
--foo, --no-foo Whether to foo
|
||||||
--bar, --no-bar Whether to bar (default: True)
|
--bar, --no-bar Whether to bar (default: True)
|
||||||
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
|
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
|
||||||
|
--bazz, --no-bazz Bazz!
|
||||||
|
|
||||||
group:
|
group:
|
||||||
-y [Y] y
|
-y [Y] y
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Fix exception in argparse help text generation if a
|
||||||
|
:class:`argparse.BooleanOptionalAction` argument's default is
|
||||||
|
``argparse.SUPPRESS`` and it has ``help`` specified. Patch by Felix Fontein.
|
Loading…
Add table
Add a link
Reference in a new issue