summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-09-02 10:55:16 (GMT)
committerGitHub <noreply@github.com>2024-09-02 10:55:16 (GMT)
commitf1a6d2254f691e476541deaea67a3f679a339691 (patch)
treefaf41e8aebd47888c178c598cccff41692ac3eb3 /Lib/argparse.py
parent8c01b3426860acd5252a644e222b1d0d1f4e118f (diff)
downloadcpython-f1a6d2254f691e476541deaea67a3f679a339691.zip
cpython-f1a6d2254f691e476541deaea67a3f679a339691.tar.gz
cpython-f1a6d2254f691e476541deaea67a3f679a339691.tar.bz2
[3.13] gh-121151: argparse: Fix wrapping of long usage text of arguments inside a mutually exclusive groups (GH-121159) (#122777)
gh-121151: argparse: Fix wrapping of long usage text of arguments inside a mutually exclusive groups (GH-121159) (cherry picked from commit 013a0929750ed2b46ae990b59d02e3db84337474) Co-authored-by: Ali Hamdan <ali.hamdan.dev@gmail.com>
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 7e5e312..bf17af0 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