From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Thu, 12 Sep 2019 15:43:14 +0000 (-0700) Subject: bpo-37908: Add an example of ArgumentParser.exit() (GH-15455) X-Git-Tag: v3.8.0rc1~128 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6dc3e61c511d3e13ce4eac86c8b6abdff58e3617;p=python bpo-37908: Add an example of ArgumentParser.exit() (GH-15455) Co-Authored-By: Brandt Bucher (cherry picked from commit b1a2abdb06408ffc4f13d6ff50351ad49c99afc0) Co-authored-by: Hai Shi --- diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 368b1cfebf..56bd64172f 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)