diff options
author | Hai Shi <shihai1992@gmail.com> | 2019-09-12 15:34:24 (GMT) |
---|---|---|
committer | Stéphane Wirtel <stephane@wirtel.be> | 2019-09-12 15:34:24 (GMT) |
commit | b1a2abdb06408ffc4f13d6ff50351ad49c99afc0 (patch) | |
tree | 71b26c86d9af9de7cdedd70cc51132aafc0e0899 /Doc | |
parent | 4210ad5ebd5769f585035e022876e161cd0e9a3e (diff) | |
download | cpython-b1a2abdb06408ffc4f13d6ff50351ad49c99afc0.zip cpython-b1a2abdb06408ffc4f13d6ff50351ad49c99afc0.tar.gz cpython-b1a2abdb06408ffc4f13d6ff50351ad49c99afc0.tar.bz2 |
bpo-37908: Add an example of ArgumentParser.exit() (GH-15455)
Co-Authored-By: Brandt Bucher <brandtbucher@gmail.com>
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/argparse.rst | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index c2cf7d3..6dffd2e 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) |