Skip to content

Commit 6dc3e61

Browse files
bpo-37908: Add an example of ArgumentParser.exit() (GH-15455)
Co-Authored-By: Brandt Bucher <[email protected]> (cherry picked from commit b1a2abd) Co-authored-by: Hai Shi <[email protected]>
1 parent a0d4aac commit 6dc3e61

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Doc/library/argparse.rst

+8-1
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,14 @@ Exiting methods
20012001
.. method:: ArgumentParser.exit(status=0, message=None)
20022002

20032003
This method terminates the program, exiting with the specified *status*
2004-
and, if given, it prints a *message* before that.
2004+
and, if given, it prints a *message* before that. The user can override
2005+
this method to handle these steps differently::
2006+
2007+
class ErrorCatchingArgumentParser(argparse.ArgumentParser):
2008+
def exit(self, status=0, message=None):
2009+
if status:
2010+
raise Exception(f'Exiting because of an error: {message}')
2011+
exit(status)
20052012

20062013
.. method:: ArgumentParser.error(message)
20072014

0 commit comments

Comments
 (0)