mirror of https://github.com/python/cpython
gh-121018: Ensure ArgumentParser.parse_args with exit_on_error=False raises instead of exiting when given unrecognized arguments (GH-121019)
This commit is contained in:
parent
82235449b8
commit
0654336dd5
|
@ -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):
|
||||
|
|
|
@ -6053,6 +6053,9 @@ class TestExitOnError(TestCase):
|
|||
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.
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Fixed an issue where :func:`argparse.ArgumentParser.parses_args` did not honor ``exit_on_error=False`` when given unrecognized arguments.
|
||||
Patch by Ben Hsing
|
Loading…
Reference in New Issue