]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-142332: Fix usage formatting for positional arguments in mutually exclusive groups...
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 6 Dec 2025 18:03:45 +0000 (20:03 +0200)
committerGitHub <noreply@github.com>
Sat, 6 Dec 2025 18:03:45 +0000 (18:03 +0000)
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2025-12-06-13-02-13.gh-issue-142332.PNvXCV.rst [new file with mode: 0644]

index 07d7d77e8845be756ada45640f559b73848d815e..27a63728eb406425155e3af710f8c0b617f54d23 100644 (file)
@@ -467,16 +467,12 @@ class HelpFormatter(object):
             # produce all arg strings
             elif not action.option_strings:
                 default = self._get_default_metavar_for_positional(action)
-                part = (
-                    t.summary_action
-                    + self._format_args(action, default)
-                    + t.reset
-                )
-
+                part = self._format_args(action, default)
                 # if it's in a group, strip the outer []
                 if action in group_actions:
                     if part[0] == '[' and part[-1] == ']':
                         part = part[1:-1]
+                part = t.summary_action + part + t.reset
 
             # produce the first way to invoke the option in brackets
             else:
index dff7ba750fa55957953c3acb0bf446f2f2632bdf..041d36717061934260d07b8c9b7e5f930e232b88 100644 (file)
@@ -7361,7 +7361,28 @@ class TestColorized(TestCase):
             ),
         )
 
-    def test_argparse_color_usage(self):
+    def test_argparse_color_mutually_exclusive_group_usage(self):
+        parser = argparse.ArgumentParser(color=True, prog="PROG")
+        group = parser.add_mutually_exclusive_group()
+        group.add_argument('--foo', action='store_true', help='FOO')
+        group.add_argument('--spam', help='SPAM')
+        group.add_argument('badger', nargs='*', help='BADGER')
+
+        prog = self.theme.prog
+        heading = self.theme.heading
+        long = self.theme.summary_long_option
+        short = self.theme.summary_short_option
+        label = self.theme.summary_label
+        pos = self.theme.summary_action
+        reset = self.theme.reset
+
+        self.assertEqual(parser.format_usage(),
+            f"{heading}usage: {reset}{prog}PROG{reset} [{short}-h{reset}] "
+            f"[{long}--foo{reset} | "
+            f"{long}--spam {label}SPAM{reset} | "
+            f"{pos}badger ...{reset}]\n")
+
+    def test_argparse_color_custom_usage(self):
         # Arrange
         parser = argparse.ArgumentParser(
             add_help=False,
diff --git a/Misc/NEWS.d/next/Library/2025-12-06-13-02-13.gh-issue-142332.PNvXCV.rst b/Misc/NEWS.d/next/Library/2025-12-06-13-02-13.gh-issue-142332.PNvXCV.rst
new file mode 100644 (file)
index 0000000..ee2d5e1
--- /dev/null
@@ -0,0 +1,2 @@
+Fix usage formatting for positional arguments in mutually exclusive groups in :mod:`argparse`.
+in :mod:`argparse`.