diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-02-21 16:20:08 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-21 16:20:08 (GMT) |
commit | a27c540d20b51d20c110fa8bc62ea040b185f958 (patch) | |
tree | f1717095f042871cb211b21903c82056a0447d42 /Lib/test | |
parent | 35e5bbc845d2f36059a5e8c48316849faa48b978 (diff) | |
download | cpython-a27c540d20b51d20c110fa8bc62ea040b185f958.zip cpython-a27c540d20b51d20c110fa8bc62ea040b185f958.tar.gz cpython-a27c540d20b51d20c110fa8bc62ea040b185f958.tar.bz2 |
[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 5f7df88821347c5f44fc4e2c691e83a60a6c6cd5)
Co-authored-by: Daniel Mach <daniel.mach@suse.com>
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_argparse.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index c67fca4..790a4f8 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2743,6 +2743,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() |