This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add optional argument for exit status in argparse.ArgumentParser.error
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6, Python 3.4, Python 3.5, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Ankit Goel, paul.j3, xtreak
Priority: normal Keywords:

Created on 2018-09-19 18:27 by Ankit Goel, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg325779 - (view) Author: Ankit Goel (Ankit Goel) Date: 2018-09-19 18:27
argparse.ArgumentParser.error currently uses a hard coded exit status (2).
An optional argument (with default value 2) would be useful to change the status without needing to overwrite the method in a subclass like here: 
https://github.com/pytest-dev/pytest/pull/3925
msg325852 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-09-20 10:41
Seems like a reasonable addition (Customization of writing to sys.stderr too) . It seems to have been inherited from optparse which has pretty much the same code with hardcoded exit code. The solution suggested is to catch SystemExit and provide custom status code or subclass ArgumentParser.

Googling "custom exit code ArgumentParser.error" gave me some discussions : 

* https://stackoverflow.com/questions/5943249/python-argparse-and-controlling-overriding-the-exit-status-code
* https://groups.google.com/forum/#!topic/comp.lang.python/NwsDljv_edk
* PEP discussion on sys.exit and stderr: https://www.python.org/dev/peps/pep-0389/#discussion-sys-stderr-and-sys-exit
* Open issue to customize stderr that might be helpful if someone wants to add a patch for custom exit code : https://bugs.python.org/issue9938

Thanks
msg325909 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2018-09-20 17:13
While I don't think this change will cause any backward compatibility issues, I wonder whether it does much good.

https://docs.python.org/3/library/argparse.html#exiting-methods

already documents the option of customizing `parser.exit` and `parser.error`

parser.exit has the optional status argument (default 0).

I don't see how an error method with optional status can be used without some sort of subclassing.  At least not for standard argparse errors.  It would only help for custom error calls. 

    parser.error('my own error message', status=0)

The usual errors are issued with a

    self.error(message)

call.

Unittest, 'test_argparse.py' has a ErrorRaisingArgumentParser class that customizes both error() and exit().  But its error catching code is a bit complicated.

Another SO reference on argparse unittesting:

https://stackoverflow.com/questions/39028204/using-unittest-to-test-argparse-exit-errors
History
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 78923
2018-09-20 17:13:27paul.j3setmessages: + msg325909
2018-09-20 10:41:04xtreaksetmessages: + msg325852
2018-09-20 10:27:21xtreaksetnosy: + xtreak
2018-09-19 22:25:53paul.j3setnosy: + paul.j3
2018-09-19 18:27:13Ankit Goelcreate