Skip to content

Commit

Permalink
pythongh-96310: Fix a traceback when all options in a mutually exclus…
Browse files Browse the repository at this point in the history
…ive group are suppressed

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.
  • Loading branch information
dmach committed Aug 26, 2022
1 parent 0319cd6 commit 0d6958c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Lib/argparse.py
Expand Up @@ -405,6 +405,8 @@ def _format_actions_usage(self, actions, groups):
else:
end = start + len(group._group_actions)
if actions[start:end] == group._group_actions:
if all((action.help is SUPPRESS for action in group._group_actions)):
continue
for action in group._group_actions:
group_actions.add(action)
if not group.required:
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_argparse.py
Expand Up @@ -2695,6 +2695,25 @@ 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)
cmd_foo.add_argument("--longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong")
expected = '''\
usage: PROG foo [-h]
[--longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong LONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONG]
options:
-h, --help show this help message and exit
--longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong LONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONG
'''
self.assertEqual(cmd_foo.format_help(), textwrap.dedent(expected))

def test_empty_group(self):
# See issue 26952
parser = argparse.ArgumentParser()
Expand Down

0 comments on commit 0d6958c

Please sign in to comment.