mirror of https://github.com/python/cpython
gh-121151: argparse: Fix wrapping of long usage text of arguments inside a mutually exclusive groups (GH-121159)
This commit is contained in:
parent
9e551f9b35
commit
013a092975
|
@ -447,15 +447,24 @@ class HelpFormatter(object):
|
||||||
parts.append(part)
|
parts.append(part)
|
||||||
|
|
||||||
# group mutually exclusive actions
|
# group mutually exclusive actions
|
||||||
|
inserted_separators_indices = set()
|
||||||
for start, end in sorted(inserts, reverse=True):
|
for start, end in sorted(inserts, reverse=True):
|
||||||
group = inserts[start, end]
|
group = inserts[start, end]
|
||||||
group_parts = [item for item in parts[start:end] if item is not None]
|
group_parts = [item for item in parts[start:end] if item is not None]
|
||||||
|
group_size = len(group_parts)
|
||||||
if group.required:
|
if group.required:
|
||||||
open, close = "()" if len(group_parts) > 1 else ("", "")
|
open, close = "()" if group_size > 1 else ("", "")
|
||||||
else:
|
else:
|
||||||
open, close = "[]"
|
open, close = "[]"
|
||||||
parts[start] = open + " | ".join(group_parts) + close
|
group_parts[0] = open + group_parts[0]
|
||||||
for i in range(start + 1, end):
|
group_parts[-1] = group_parts[-1] + close
|
||||||
|
for i, part in enumerate(group_parts[:-1], start=start):
|
||||||
|
# insert a separator if not already done in a nested group
|
||||||
|
if i not in inserted_separators_indices:
|
||||||
|
parts[i] = part + ' |'
|
||||||
|
inserted_separators_indices.add(i)
|
||||||
|
parts[start + group_size - 1] = group_parts[-1]
|
||||||
|
for i in range(start + group_size, end):
|
||||||
parts[i] = None
|
parts[i] = None
|
||||||
|
|
||||||
# return the usage parts
|
# return the usage parts
|
||||||
|
|
|
@ -2959,12 +2959,12 @@ class TestMutuallyExclusiveLong(MEMixin, TestCase):
|
||||||
]
|
]
|
||||||
|
|
||||||
usage_when_not_required = '''\
|
usage_when_not_required = '''\
|
||||||
usage: PROG [-h] [--abcde ABCDE] [--fghij FGHIJ]
|
usage: PROG [-h] [--abcde ABCDE] [--fghij FGHIJ] [--klmno KLMNO |
|
||||||
[--klmno KLMNO | --pqrst PQRST]
|
--pqrst PQRST]
|
||||||
'''
|
'''
|
||||||
usage_when_required = '''\
|
usage_when_required = '''\
|
||||||
usage: PROG [-h] [--abcde ABCDE] [--fghij FGHIJ]
|
usage: PROG [-h] [--abcde ABCDE] [--fghij FGHIJ] (--klmno KLMNO |
|
||||||
(--klmno KLMNO | --pqrst PQRST)
|
--pqrst PQRST)
|
||||||
'''
|
'''
|
||||||
help = '''\
|
help = '''\
|
||||||
|
|
||||||
|
@ -4347,6 +4347,24 @@ class TestHelpUsageNoWhitespaceCrash(TestCase):
|
||||||
''')
|
''')
|
||||||
self.assertEqual(parser.format_usage(), usage)
|
self.assertEqual(parser.format_usage(), usage)
|
||||||
|
|
||||||
|
def test_long_mutex_groups_wrap(self):
|
||||||
|
parser = argparse.ArgumentParser(prog='PROG')
|
||||||
|
g = parser.add_mutually_exclusive_group()
|
||||||
|
g.add_argument('--op1', metavar='MET', nargs='?')
|
||||||
|
g.add_argument('--op2', metavar=('MET1', 'MET2'), nargs='*')
|
||||||
|
g.add_argument('--op3', nargs='*')
|
||||||
|
g.add_argument('--op4', metavar=('MET1', 'MET2'), nargs='+')
|
||||||
|
g.add_argument('--op5', nargs='+')
|
||||||
|
g.add_argument('--op6', nargs=3)
|
||||||
|
g.add_argument('--op7', metavar=('MET1', 'MET2', 'MET3'), nargs=3)
|
||||||
|
|
||||||
|
usage = textwrap.dedent('''\
|
||||||
|
usage: PROG [-h] [--op1 [MET] | --op2 [MET1 [MET2 ...]] | --op3 [OP3 ...] |
|
||||||
|
--op4 MET1 [MET2 ...] | --op5 OP5 [OP5 ...] | --op6 OP6 OP6 OP6 |
|
||||||
|
--op7 MET1 MET2 MET3]
|
||||||
|
''')
|
||||||
|
self.assertEqual(parser.format_usage(), usage)
|
||||||
|
|
||||||
|
|
||||||
class TestHelpVariableExpansion(HelpTestCase):
|
class TestHelpVariableExpansion(HelpTestCase):
|
||||||
"""Test that variables are expanded properly in help messages"""
|
"""Test that variables are expanded properly in help messages"""
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix wrapping of long usage text of arguments inside a mutually exclusive
|
||||||
|
group in :mod:`argparse`.
|
Loading…
Reference in New Issue