https://github.com/python/cpython/commit/00fd32af21a3ff371cf12a509b324de9602af894
commit: 00fd32af21a3ff371cf12a509b324de9602af894
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-09-29T08:16:38Z
summary:

[3.12] gh-104860: Fix allow_abbrev=False for single-dash long options 
(GH-124340) (GH-124750)

(cherry picked from commit 49e105f9488de18d3d92948232fcbd956cbe0c6e)

Co-authored-by: Serhiy Storchaka <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-09-23-17-33-47.gh-issue-104860.O86OSc.rst
M Lib/argparse.py
M Lib/test/test_argparse.py

diff --git a/Lib/argparse.py b/Lib/argparse.py
index d0302ad4fb6fb7..4f5867a4af6b66 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -2361,7 +2361,7 @@ def _get_option_tuples(self, option_string):
                     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)
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 854a50feebd0cc..5d4fd3d42777ff 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -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
index 00000000000000..707c4d651cb5e6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-09-23-17-33-47.gh-issue-104860.O86OSc.rst
@@ -0,0 +1,2 @@
+Fix disallowing abbreviation of single-dash long options in :mod:`argparse`
+with ``allow_abbrev=False``.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to