mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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.
This commit is contained in:
parent
4a9e6497c2
commit
5f7df88821
3 changed files with 25 additions and 0 deletions
|
@ -2864,6 +2864,27 @@ class TestMutuallyExclusiveGroupErrors(TestCase):
|
|||
'''
|
||||
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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue