Skip to content

Commit a27c540

Browse files
[3.11] gh-96310: Fix a traceback in argparse when all options in a mutually exclusive group are suppressed (GH-96311) (GH-115768)
Reproducer depends on terminal size - the traceback occurs when there's an option long enough so the usage line doesn't fit the terminal width. Option order is also important for reproducibility. Excluding empty groups (with all options suppressed) from inserts fixes the problem. (cherry picked from commit 5f7df88) Co-authored-by: Daniel Mach <[email protected]>
1 parent 35e5bbc commit a27c540

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Lib/argparse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ def _format_actions_usage(self, actions, groups):
414414
suppressed_actions_count += 1
415415

416416
exposed_actions_count = group_action_count - suppressed_actions_count
417+
if not exposed_actions_count:
418+
continue
417419

418420
if not group.required:
419421
if start in inserts:

Lib/test/test_argparse.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,27 @@ def test_help(self):
27432743
'''
27442744
self.assertEqual(parser.format_help(), textwrap.dedent(expected))
27452745

2746+
def test_help_subparser_all_mutually_exclusive_group_members_suppressed(self):
2747+
self.maxDiff = None
2748+
parser = ErrorRaisingArgumentParser(prog='PROG')
2749+
commands = parser.add_subparsers(title="commands", dest="command")
2750+
cmd_foo = commands.add_parser("foo")
2751+
group = cmd_foo.add_mutually_exclusive_group()
2752+
group.add_argument('--verbose', action='store_true', help=argparse.SUPPRESS)
2753+
group.add_argument('--quiet', action='store_true', help=argparse.SUPPRESS)
2754+
longopt = '--' + 'long'*32
2755+
longmeta = 'LONG'*32
2756+
cmd_foo.add_argument(longopt)
2757+
expected = f'''\
2758+
usage: PROG foo [-h]
2759+
[{longopt} {longmeta}]
2760+
2761+
options:
2762+
-h, --help show this help message and exit
2763+
{longopt} {longmeta}
2764+
'''
2765+
self.assertEqual(cmd_foo.format_help(), textwrap.dedent(expected))
2766+
27462767
def test_empty_group(self):
27472768
# See issue 26952
27482769
parser = argparse.ArgumentParser()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a traceback in :mod:`argparse` when all options in a mutually exclusive
2+
group are suppressed.

0 commit comments

Comments
 (0)