Title: argparse should offer an alternative to SystemExit in case a parse fails
Type: enhancement Stage: resolved
Components: Library (Lib) Versions:
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, geophile, paul.j3, rhettinger, xtreak
Priority: normal Keywords:

Created on 2020-01-14 20:36 by geophile, last changed 2020-01-14 20:59 by geophile. This issue is now closed.

Messages (4)
msg359991 - (view) Author: Jack Orenstein (geophile) Date: 2020-01-14 20:36
If parse_args fails, SystemExit is raised, carrying an exit code of 2, and the help message is printed. For an embedded usage of argparse, this behavior is undesirable.

I am writing an interactive console application, using argparse to parse input. When a parse fails, I would like to print an error message and continue, not terminate the program. Currently, I need to catch SystemExit to be able to do this, which has obvious problems, (e.g., what if something other that argparse is causing the exit?)

I'd like to see some way to specify alternative behavior, e.g. raise an exception of a given type.
msg359994 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-01-14 20:52
Does overriding exit help here?
msg359995 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-01-14 20:53
Maybe argparse could raise an exception derived from SystemExit, then you could catch that.
msg359997 - (view) Author: Jack Orenstein (geophile) Date: 2020-01-14 20:59
Yes! I didn't know about that method, thank you.
Date User Action Args
2020-01-14 20:59:52geophilesetstatus: open -> closed

messages: + msg359997
stage: resolved
2020-01-14 20:53:32eric.smithsetnosy: + eric.smith
messages: + msg359995
2020-01-14 20:52:18xtreaksetnosy: + xtreak, paul.j3
messages: + msg359994
2020-01-14 20:45:32zach.waresetnosy: + rhettinger
2020-01-14 20:36:54geophilecreate