https://github.com/python/cpython/commit/4868d1bab14a93c9a2f11b7b3b1815900fa71c51
commit: 4868d1bab14a93c9a2f11b7b3b1815900fa71c51
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-06-26T10:16:44Z
summary:

[3.12] gh-121018: Ensure ArgumentParser.parse_args with exit_on_error=False 
raises instead of exiting when given unrecognized arguments (GH-121019) 
(GH-121031)

(cherry picked from commit 0654336dd5138aec04e3017e15ccbb90a44e053d)

Co-authored-by: blhsing <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst
M Lib/argparse.py
M Lib/test/test_argparse.py

diff --git a/Lib/argparse.py b/Lib/argparse.py
index 120cb6c845897f..0890718270f46e 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1895,8 +1895,10 @@ def _get_positional_actions(self):
     def parse_args(self, args=None, namespace=None):
         args, argv = self.parse_known_args(args, namespace)
         if argv:
-            msg = _('unrecognized arguments: %s')
-            self.error(msg % ' '.join(argv))
+            msg = _('unrecognized arguments: %s') % ' '.join(argv)
+            if self.exit_on_error:
+                self.error(msg)
+            raise ArgumentError(None, msg)
         return args
 
     def parse_known_args(self, args=None, namespace=None):
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 940c93bcb02a3b..a4d6c4a35256f9 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -5743,6 +5743,9 @@ def test_exit_on_error_with_bad_args(self):
         with self.assertRaises(argparse.ArgumentError):
             self.parser.parse_args('--integers a'.split())
 
+    def test_exit_on_error_with_unrecognized_args(self):
+        with self.assertRaises(argparse.ArgumentError):
+            self.parser.parse_args('--foo bar'.split())
 
 def tearDownModule():
     # Remove global references to avoid looking like we have refleaks.
diff --git 
a/Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst 
b/Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst
new file mode 100644
index 00000000000000..fdc3c5f9e459af
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-06-26-03-04-24.gh-issue-121018.clVSc4.rst
@@ -0,0 +1,2 @@
+Fixed an issue where :meth:`!argparse.ArgumentParser.parses_args` did not 
honor ``exit_on_error=False`` when given unrecognized arguments.
+Patch by Ben Hsing.

_______________________________________________
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