From b1a2abdb06408ffc4f13d6ff50351ad49c99afc0 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 12 Sep 2019 10:34:24 -0500 Subject: [PATCH] bpo-37908: Add an example of ArgumentParser.exit() (GH-15455) Co-Authored-By: Brandt Bucher --- Doc/library/argparse.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index c2cf7d320b..6dffd2e932 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -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) -- 2.50.1