]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-104860: Fix allow_abbrev=False for single-dash long options (GH-124340...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 7 Oct 2024 21:25:13 +0000 (23:25 +0200)
committerGitHub <noreply@github.com>
Mon, 7 Oct 2024 21:25:13 +0000 (00:25 +0300)
(cherry picked from commit 49e105f9488de18d3d92948232fcbd956cbe0c6e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2024-09-23-17-33-47.gh-issue-104860.O86OSc.rst [new file with mode: 0644]

index 04a535e42bed015942eef1d977b2440435750367..7c2370637063365c72801b748360acdb40d7582a 100644 (file)
@@ -2353,7 +2353,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
                     action = self._option_string_actions[option_string]
                     tup = action, option_string, '', short_explicit_arg
                     result.append(tup)
-                elif option_string.startswith(option_prefix):
+                elif self.allow_abbrev and option_string.startswith(option_prefix):
                     action = self._option_string_actions[option_string]
                     tup = action, option_string, None, None
                     result.append(tup)
index 24260bec0cae36c94ca096791d57b705f9214984..81daa4299c32ae1fd7cd322d7d959f9d6ec2b17f 100644 (file)
@@ -957,6 +957,23 @@ class TestOptionalsDisallowLongAbbreviationPrefixChars(ParserTestCase):
     ]
 
 
+class TestOptionalsDisallowSingleDashLongAbbreviation(ParserTestCase):
+    """Do not allow abbreviations of long options at all"""
+
+    parser_signature = Sig(allow_abbrev=False)
+    argument_signatures = [
+        Sig('-foo'),
+        Sig('-foodle', action='store_true'),
+        Sig('-foonly'),
+    ]
+    failures = ['-foon 3', '-food', '-food -foo 2']
+    successes = [
+        ('', NS(foo=None, foodle=False, foonly=None)),
+        ('-foo 3', NS(foo='3', foodle=False, foonly=None)),
+        ('-foonly 7 -foodle -foo 2', NS(foo='2', foodle=True, foonly='7')),
+    ]
+
+
 class TestDisallowLongAbbreviationAllowsShortGrouping(ParserTestCase):
     """Do not allow abbreviations of long options at all"""
 
diff --git a/Misc/NEWS.d/next/Library/2024-09-23-17-33-47.gh-issue-104860.O86OSc.rst b/Misc/NEWS.d/next/Library/2024-09-23-17-33-47.gh-issue-104860.O86OSc.rst
new file mode 100644 (file)
index 0000000..707c4d6
--- /dev/null
@@ -0,0 +1,2 @@
+Fix disallowing abbreviation of single-dash long options in :mod:`argparse`
+with ``allow_abbrev=False``.