8000 [3.12] gh-96310: Fix a traceback in argparse when all options in a mutually exclusive group are suppressed (GH-96311) by miss-islington · Pull Request #115767 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.12] gh-96310: Fix a traceback in argparse when all options in a mutually exclusive group are suppressed (GH-96311) #115767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-96310: Fix a traceback in argparse when all options in a mutually …
…exclusive group are suppressed (GH-96311)

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 <daniel.mach@suse.com>
  • Loading branch information
dmach authored and miss-islington committed Feb 21, 2024
commit 7c1683103c9c9568f323fda1cf1e4a20176e8bd2
2 changes: 2 additions & 0 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ def _format_actions_usage(self, actions, groups):
suppressed_actions_count += 1

exposed_actions_count = group_action_count - suppressed_actions_count
if not exposed_actions_count:
continue

if not group.required:
if start in inserts:
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2797,6 +2797,27 @@ def test_help(self):
'''
self.assertEqual(parser.format_help(), textwrap.dedent(expected))

def test_help_subparser_all_mutually_exclusive_group_members_suppressed(self):
self.maxDiff = None
parser = ErrorRaisingArgumentParser(prog='PROG')
commands = parser.add_subparsers(title="commands", dest="command")
cmd_foo = commands.add_parser("foo")
group = cmd_foo.add_mutually_exclusive_group()
group.add_argument('--verbose', action='store_true', help=argparse.SUPPRESS)
group.add_argument('--quiet', action='store_true', help=argparse.SUPPRESS)
longopt = '--' + 'long'*32
longmeta = 'LONG'*32
cmd_foo.add_argument(longopt)
expected = f'''\
usage: PROG foo [-h]
[{longopt} {longmeta}]

options:
-h, --help show this help message and exit
{longopt} {longmeta}
'''
self.assertEqual(cmd_foo.format_help(), textwrap.dedent(expected))

def test_empty_group(self):
# See issue 26952
parser = argparse.ArgumentParser()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a traceback in :mod:`argparse` when all options in a mutually exclusive
group are suppressed.
0