summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
diff options
context:
space:
mode:
authorAli Hamdan <ali.hamdan.dev@gmail.com>2024-08-07 13:20:38 (GMT)
committerGitHub <noreply@github.com>2024-08-07 13:20:38 (GMT)
commit013a0929750ed2b46ae990b59d02e3db84337474 (patch)
tree4a176cf43a56a10d64fc99903d55d16717332eba /Lib/argparse.py
parent9e551f9b351440ebae79e07a02d0e4a1b61d139e (diff)
downloadcpython-013a0929750ed2b46ae990b59d02e3db84337474.zip
cpython-013a0929750ed2b46ae990b59d02e3db84337474.tar.gz
cpython-013a0929750ed2b46ae990b59d02e3db84337474.tar.bz2
gh-121151: argparse: Fix wrapping of long usage text of arguments inside a mutually exclusive groups (GH-121159)
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r--Lib/argparse.py15
1 files changed, 12 insertions, 3 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 8318911..100ef9f 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -447,15 +447,24 @@ class HelpFormatter(object):
parts.append(part)
# group mutually exclusive actions
+ inserted_separators_indices = set()
for start, end in sorted(inserts, reverse=True):
group = inserts[start, end]
group_parts = [item for item in parts[start:end] if item is not None]
+ group_size = len(group_parts)
if group.required:
- open, close = "()" if len(group_parts) > 1 else ("", "")
+ open, close = "()" if group_size > 1 else ("", "")
else:
open, close = "[]"
- parts[start] = open + " | ".join(group_parts) + close
- for i in range(start + 1, end):
+ group_parts[0] = open + group_parts[0]
+ 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
# return the usage parts