classification
Title: Document exit() from parse_args
Type: enhancement Stage:
Components: Documentation Versions: Python 3.8, Python 3.7, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: CharlesMerriam, docs@python, paul.j3, r.david.murray
Priority: normal Keywords:

Created on 2017-09-29 16:54 by CharlesMerriam, last changed 2017-10-12 19:19 by paul.j3.

Messages (3)
msg303341 - (view) Author: Charles Merriam (CharlesMerriam) Date: 2017-09-29 16:54
It is unexpected to testers and users to ArgParse that it terminates the process; one usually expects an error code.  Fix by modifying documentation to section 16.4.4 The parse_args() Method. 

Change the words "Return the populated namespace." to "Return the populated namespace, or exit with a status 0 if the help message is printed, or exit with status 2 if an invalid argument was parsed."
msg303344 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-09-29 17:23
I think this is reasonable, but do note that this is covered in 16.4.4.2, and the fact that help exits is actually a property of help, not parse_args.  That is, the docs are correct and complete as they stand, but I agree that it would be helpful to have the summary include this information as well.
msg304283 - (view) Author: paul j3 (paul.j3) * Date: 2017-10-12 19:19
And the actual exit is via `parse.error` and `parse.exit`, which are documented in 16.4.5.9.

When run interactively in Ipython, exits (including the help) are captured and displayed with:

In [896]: parser.parse_args()
usage: ipython3 [-h] [--one | --two | --six]
ipython3: error: unrecognized arguments: --pylab --nosep --term-title --InteractiveShellApp.pylab_import_all=False
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

The exit makes unittesting a challenge.  'test_argparse.py' resolves this by using a subclassed parser, one that changes the error/exit, and also redirects output.
History
Date User Action Args
2017-10-12 19:19:21paul.j3setnosy: + paul.j3
messages: + msg304283
2017-09-29 17:23:01r.david.murraysetnosy: + r.david.murray

messages: + msg303344
versions: + Python 2.7, - Python 3.5, Python 3.6
2017-09-29 16:54:02CharlesMerriamcreate