diff options
author | Hai Shi <shihai1992@gmail.com> | 2019-09-12 10:56:05 (GMT) |
---|---|---|
committer | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-09-12 10:56:05 (GMT) |
commit | f545638b5701652ffbe1774989533cdf5bc6631e (patch) | |
tree | 27c90f441b2aa84cf770ff3782550947b4ab36af /Doc/library/argparse.rst | |
parent | 2d32bf1ef23c9e468b2e8afab3c24e7a2047ac36 (diff) | |
download | cpython-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 'Doc/library/argparse.rst')
-rw-r--r-- | Doc/library/argparse.rst | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 368b1cf..c2cf7d3 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -142,7 +142,7 @@ ArgumentParser objects formatter_class=argparse.HelpFormatter, \ prefix_chars='-', fromfile_prefix_chars=None, \ argument_default=None, conflict_handler='error', \ - add_help=True, allow_abbrev=True) + add_help=True, allow_abbrev=True, exit_on_error=True) Create a new :class:`ArgumentParser` object. All parameters should be passed as keyword arguments. Each parameter has its own more detailed description @@ -179,6 +179,9 @@ ArgumentParser objects * allow_abbrev_ - Allows long options to be abbreviated if the abbreviation is unambiguous. (default: ``True``) + * exit_on_error_ - Determines whether or not ArgumentParser exits with + error info when an error occurs. (default: ``True``) + .. versionchanged:: 3.5 *allow_abbrev* parameter was added. @@ -186,6 +189,9 @@ ArgumentParser objects In previous versions, *allow_abbrev* also disabled grouping of short flags such as ``-vv`` to mean ``-v -v``. + .. versionchanged:: 3.9 + *exit_on_error* parameter was added. + The following sections describe how each of these are used. @@ -647,6 +653,28 @@ the help options:: +h, ++help show this help message and exit +exit_on_error +^^^^^^^^^^^^^ + +Normally, when you pass an invalid argument list to the :meth:`~ArgumentParser.parse_args` +method of an :class:`ArgumentParser`, it will exit with error info. + +If the user would like catch errors manually, the feature can be enable by setting +``exit_on_error`` to ``False``:: + + >>> parser = argparse.ArgumentParser(exit_on_error=False) + >>> parser.add_argument('--integers', type=int) + _StoreAction(option_strings=['--integers'], dest='integers', nargs=None, const=None, default=None, type=<class 'int'>, choices=None, help=None, metavar=None) + >>> try: + ... parser.parse_args('--integers a'.split()) + ... except argparse.ArgumentError: + ... print('Catching an argumentError') + ... + Catching an argumentError + +.. versionadded:: 3.9 + + The add_argument() method ------------------------- |