diff options
author | blhsing <blhsing@gmail.com> | 2024-06-26 07:41:51 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-26 07:41:51 (GMT) |
commit | 0654336dd5138aec04e3017e15ccbb90a44e053d (patch) | |
tree | 5bb24608c03a07c7ce19627d7efb09b01169daaa /Lib/argparse.py | |
parent | 82235449b85165add62c1b200299456a50a1d097 (diff) | |
download | cpython-0654336dd5138aec04e3017e15ccbb90a44e053d.zip cpython-0654336dd5138aec04e3017e15ccbb90a44e053d.tar.gz cpython-0654336dd5138aec04e3017e15ccbb90a44e053d.tar.bz2 |
gh-121018: Ensure ArgumentParser.parse_args with exit_on_error=False raises instead of exiting when given unrecognized arguments (GH-121019)
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r-- | Lib/argparse.py | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index cdd29d3..f1c3808 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1843,8 +1843,10 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): 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): |