]> granicus.if.org Git - python/commitdiff
bpo-37908: Add an example of ArgumentParser.exit() (GH-15455)
authorHai Shi <shihai1992@gmail.com>
Thu, 12 Sep 2019 15:34:24 +0000 (10:34 -0500)
committerStéphane Wirtel <stephane@wirtel.be>
Thu, 12 Sep 2019 15:34:24 +0000 (16:34 +0100)
Co-Authored-By: Brandt Bucher <brandtbucher@gmail.com>
Doc/library/argparse.rst

index c2cf7d320b3f8cb5d9b657ea56a421238e4bdbfc..6dffd2e9325e11a7f00f3cb9cf081faf2b448bb4 100644 (file)
@@ -2029,7 +2029,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)