Skip to content

Commit b1a2abd

Browse files
shihai1991brandtbucher
authored andcommitted
bpo-37908: Add an example of ArgumentParser.exit() (GH-15455)
Co-Authored-By: Brandt Bucher <[email protected]>
1 parent 4210ad5 commit b1a2abd

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Doc/library/argparse.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2029,7 +2029,14 @@ Exiting methods
20292029
.. method:: ArgumentParser.exit(status=0, message=None)
20302030

20312031
This method terminates the program, exiting with the specified *status*
2032-
and, if given, it prints a *message* before that.
2032+
and, if given, it prints a *message* before that. The user can override
2033+
this method to handle these steps differently::
2034+
2035+
class ErrorCatchingArgumentParser(argparse.ArgumentParser):
2036+
def exit(self, status=0, message=None):
2037+
if status:
2038+
raise Exception(f'Exiting because of an error: {message}')
2039+
exit(status)
20332040

20342041
.. method:: ArgumentParser.error(message)
20352042

0 commit comments

Comments
 (0)