Skip to content

Commit 4556b9e

Browse files
[3.14] GH-75949: Fix argparse dropping '|' in mutually exclusive groups on line wrap (GH-142312) (#142347)
1 parent 95d19fc commit 4556b9e

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

Lib/argparse.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,14 @@ def _format_usage(self, usage, actions, groups, prefix):
356356
if len(prefix) + len(self._decolor(usage)) > text_width:
357357

358358
# break usage into wrappable parts
359-
opt_parts = self._get_actions_usage_parts(optionals, groups)
360-
pos_parts = self._get_actions_usage_parts(positionals, groups)
359+
# keep optionals and positionals together to preserve
360+
# mutually exclusive group formatting (gh-75949)
361+
all_actions = optionals + positionals
362+
parts, pos_start = self._get_actions_usage_parts_with_split(
363+
all_actions, groups, len(optionals)
364+
)
365+
opt_parts = parts[:pos_start]
366+
pos_parts = parts[pos_start:]
361367

362368
# helper for wrapping lines
363369
def get_lines(parts, indent, prefix=None):
@@ -421,6 +427,17 @@ def _is_long_option(self, string):
421427
return len(string) > 2
422428

423429
def _get_actions_usage_parts(self, actions, groups):
430+
parts, _ = self._get_actions_usage_parts_with_split(actions, groups)
431+
return parts
432+
433+
def _get_actions_usage_parts_with_split(self, actions, groups, opt_count=None):
434+
"""Get usage parts with split index for optionals/positionals.
435+
436+
Returns (parts, pos_start) where pos_start is the index in parts
437+
where positionals begin. When opt_count is None, pos_start is None.
438+
This preserves mutually exclusive group formatting across the
439+
optionals/positionals boundary (gh-75949).
440+
"""
424441
# find group indices and identify actions in groups
425442
group_actions = set()
426443
inserts = {}
@@ -516,8 +533,16 @@ def _get_actions_usage_parts(self, actions, groups):
516533
for i in range(start + group_size, end):
517534
parts[i] = None
518535

519-
# return the usage parts
520-
return [item for item in parts if item is not None]
536+
# if opt_count is provided, calculate where positionals start in
537+
# the final parts list (for wrapping onto separate lines).
538+
# Count before filtering None entries since indices shift after.
539+
if opt_count is not None:
540+
pos_start = sum(1 for p in parts[:opt_count] if p is not None)
541+
else:
542+
pos_start = None
543+
544+
# return the usage parts and split point (gh-75949)
545+
return [item for item in parts if item is not None], pos_start
521546

522547
def _format_text(self, text):
523548
if '%(prog)' in text:

Lib/test/test_argparse.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4885,6 +4885,25 @@ def test_long_mutex_groups_wrap(self):
48854885
''')
48864886
self.assertEqual(parser.format_usage(), usage)
48874887

4888+
def test_mutex_groups_with_mixed_optionals_positionals_wrap(self):
4889+
# https://github.com/python/cpython/issues/75949
4890+
# Mutually exclusive groups containing both optionals and positionals
4891+
# should preserve pipe separators when the usage line wraps.
4892+
parser = argparse.ArgumentParser(prog='PROG')
4893+
g = parser.add_mutually_exclusive_group()
4894+
g.add_argument('-v', '--verbose', action='store_true')
4895+
g.add_argument('-q', '--quiet', action='store_true')
4896+
g.add_argument('-x', '--extra-long-option-name', nargs='?')
4897+
g.add_argument('-y', '--yet-another-long-option', nargs='?')
4898+
g.add_argument('positional', nargs='?')
4899+
4900+
usage = textwrap.dedent('''\
4901+
usage: PROG [-h] [-v | -q | -x [EXTRA_LONG_OPTION_NAME] |
4902+
-y [YET_ANOTHER_LONG_OPTION] |
4903+
positional]
4904+
''')
4905+
self.assertEqual(parser.format_usage(), usage)
4906+
48884907

48894908
class TestHelpVariableExpansion(HelpTestCase):
48904909
"""Test that variables are expanded properly in help messages"""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`argparse` to preserve ``|`` separators in mutually exclusive groups when the usage line wraps due to length.

0 commit comments

Comments
 (0)