[Python-checkins] bpo-37908: Add an example of ArgumentParser.exit() (GH-15455)

Miss Islington (bot) webhook-mailer at python.org
Thu Sep 12 11:43:18 EDT 2019


https://github.com/python/cpython/commit/6dc3e61c511d3e13ce4eac86c8b6abdff58e3617
commit: 6dc3e61c511d3e13ce4eac86c8b6abdff58e3617
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-09-12T08:43:14-07:00
summary:

bpo-37908: Add an example of ArgumentParser.exit() (GH-15455)


Co-Authored-By: Brandt Bucher <brandtbucher at gmail.com>
(cherry picked from commit b1a2abdb06408ffc4f13d6ff50351ad49c99afc0)

Co-authored-by: Hai Shi <shihai1992 at gmail.com>

files:
M Doc/library/argparse.rst

diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index 368b1cfebf05..56bd64172f80 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -2001,7 +2001,14 @@ Exiting methods
 .. method:: ArgumentParser.exit(status=0, message=None)
 
    This method terminates the program, exiting with the specified *status*
-   and, if given, it prints a *message* before that.
+   and, if given, it prints a *message* before that. The user can override
+   this method to handle these steps differently::
+
+    class ErrorCatchingArgumentParser(argparse.ArgumentParser):
+        def exit(self, status=0, message=None):
+            if status:
+                raise Exception(f'Exiting because of an error: {message}')
+            exit(status)
 
 .. method:: ArgumentParser.error(message)
 



More information about the Python-checkins mailing list