summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
diff options
context:
space:
mode:
authorHai Shi <shihai1992@gmail.com>2019-09-12 10:56:05 (GMT)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-09-12 10:56:05 (GMT)
commitf545638b5701652ffbe1774989533cdf5bc6631e (patch)
tree27c90f441b2aa84cf770ff3782550947b4ab36af /Lib/argparse.py
parent2d32bf1ef23c9e468b2e8afab3c24e7a2047ac36 (diff)
downloadcpython-f545638b5701652ffbe1774989533cdf5bc6631e.zip
cpython-f545638b5701652ffbe1774989533cdf5bc6631e.tar.gz
cpython-f545638b5701652ffbe1774989533cdf5bc6631e.tar.bz2
bpo-9938: Add optional keyword argument exit_on_error to argparse.ArgumentParser (GH-15362)
Co-Authored-by: Xuanji Li <xuanji@gmail.com> https://bugs.python.org/issue9938 Automerge-Triggered-By: @matrixise
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r--Lib/argparse.py26
1 files changed, 17 insertions, 9 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index ddfd772..2e46e76 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1630,6 +1630,8 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
- conflict_handler -- String indicating how to handle conflicts
- add_help -- Add a -h/-help option
- allow_abbrev -- Allow long options to be abbreviated unambiguously
+ - exit_on_error -- Determines whether or not ArgumentParser exits with
+ error info when an error occurs
"""
def __init__(self,
@@ -1644,7 +1646,8 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
argument_default=None,
conflict_handler='error',
add_help=True,
- allow_abbrev=True):
+ allow_abbrev=True,
+ exit_on_error=True):
superinit = super(ArgumentParser, self).__init__
superinit(description=description,
@@ -1663,6 +1666,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
self.fromfile_prefix_chars = fromfile_prefix_chars
self.add_help = add_help
self.allow_abbrev = allow_abbrev
+ self.exit_on_error = exit_on_error
add_group = self.add_argument_group
self._positionals = add_group(_('positional arguments'))
@@ -1793,15 +1797,19 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
setattr(namespace, dest, self._defaults[dest])
# parse the arguments and exit if there are any errors
- try:
+ if self.exit_on_error:
+ try:
+ namespace, args = self._parse_known_args(args, namespace)
+ except ArgumentError:
+ err = _sys.exc_info()[1]
+ self.error(str(err))
+ else:
namespace, args = self._parse_known_args(args, namespace)
- if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):
- args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR))
- delattr(namespace, _UNRECOGNIZED_ARGS_ATTR)
- return namespace, args
- except ArgumentError:
- err = _sys.exc_info()[1]
- self.error(str(err))
+
+ if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):
+ args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR))
+ delattr(namespace, _UNRECOGNIZED_ARGS_ATTR)
+ return namespace, args
def _parse_known_args(self, arg_strings, namespace):
# replace arg strings that are file references