mirror of
https://github.com/python/cpython.git
synced 2025-12-09 10:37:17 +00:00
[3.13] gh-121018: Fix more cases of exiting in argparse when exit_on_error=False (GH-121056) (GH-121128)
* parse_intermixed_args() now raises ArgumentError instead of calling
error() if exit_on_error is false.
* Internal code now always raises ArgumentError instead of calling
error(). It is then caught at the higher level and error() is called if
exit_on_error is true.
(cherry picked from commit 81a654a342)
This commit is contained in:
parent
394dc93bf9
commit
99de20d729
3 changed files with 58 additions and 14 deletions
|
|
@ -1819,7 +1819,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
# ==================================
|
||||
def add_subparsers(self, **kwargs):
|
||||
if self._subparsers is not None:
|
||||
self.error(_('cannot have multiple subparser arguments'))
|
||||
raise ArgumentError(None, _('cannot have multiple subparser arguments'))
|
||||
|
||||
# add the parser class to the arguments if it's not present
|
||||
kwargs.setdefault('parser_class', type(self))
|
||||
|
|
@ -1874,7 +1874,8 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
msg = _('unrecognized arguments: %s') % ' '.join(argv)
|
||||
if self.exit_on_error:
|
||||
self.error(msg)
|
||||
raise ArgumentError(None, msg)
|
||||
else:
|
||||
raise ArgumentError(None, msg)
|
||||
return args
|
||||
|
||||
def parse_known_args(self, args=None, namespace=None):
|
||||
|
|
@ -2163,7 +2164,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
self._get_value(action, action.default))
|
||||
|
||||
if required_actions:
|
||||
self.error(_('the following arguments are required: %s') %
|
||||
raise ArgumentError(None, _('the following arguments are required: %s') %
|
||||
', '.join(required_actions))
|
||||
|
||||
# make sure all required groups had one option present
|
||||
|
|
@ -2179,7 +2180,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
for action in group._group_actions
|
||||
if action.help is not SUPPRESS]
|
||||
msg = _('one of the arguments %s is required')
|
||||
self.error(msg % ' '.join(names))
|
||||
raise ArgumentError(None, msg % ' '.join(names))
|
||||
|
||||
# return the updated namespace and the extra arguments
|
||||
return namespace, extras
|
||||
|
|
@ -2206,7 +2207,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
arg_strings = self._read_args_from_files(arg_strings)
|
||||
new_arg_strings.extend(arg_strings)
|
||||
except OSError as err:
|
||||
self.error(str(err))
|
||||
raise ArgumentError(None, str(err))
|
||||
|
||||
# return the modified argument list
|
||||
return new_arg_strings
|
||||
|
|
@ -2286,7 +2287,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
for action, option_string, sep, explicit_arg in option_tuples])
|
||||
args = {'option': arg_string, 'matches': options}
|
||||
msg = _('ambiguous option: %(option)s could match %(matches)s')
|
||||
self.error(msg % args)
|
||||
raise ArgumentError(None, msg % args)
|
||||
|
||||
# if exactly one action matched, this segmentation is good,
|
||||
# so return the parsed action
|
||||
|
|
@ -2346,7 +2347,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
|
||||
# shouldn't ever get here
|
||||
else:
|
||||
self.error(_('unexpected option string: %s') % option_string)
|
||||
raise ArgumentError(None, _('unexpected option string: %s') % option_string)
|
||||
|
||||
# return the collected option tuples
|
||||
return result
|
||||
|
|
@ -2403,8 +2404,11 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
def parse_intermixed_args(self, args=None, namespace=None):
|
||||
args, argv = self.parse_known_intermixed_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)
|
||||
else:
|
||||
raise ArgumentError(None, msg)
|
||||
return args
|
||||
|
||||
def parse_known_intermixed_args(self, args=None, namespace=None):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue