[Python-Dev] PEP 389: argparse - new command line parsing module

Steven D'Aprano steve at pearwood.info
Sun Oct 4 03:29:23 CEST 2009

On Sun, 4 Oct 2009 08:21:46 am Robert Kern wrote:

> There are uses of argparse outside of command line apps. For example,
> I use it to parse --options for IPython %magic commands. Of course, I
> just subclass ArgumentParser and replace the .error() method.

Exactly. There are uses for catching SystemExit, which is why it's an 
exception and not an unconditional exit. But they're rare, and not 
difficult to deal with: in your case, having argparse raise a subclass 
of SystemExit won't help you, you would still need to subclass, and in 
other cases, you can just catch SystemExit.

> The other use case, as mentioned earlier, was for debugging your
> parser on the interactive prompt. A custom subclass may also be able
> to hold more machine-readable information about the error than the
> formatted error message, but I don't have any particular use cases
> about what may be the most useful.

Nobody has requested that the exception expose more information. They've 
requested that argparse paint the SystemExit a slightly different shade 
of yellow to the colour it already is -- this is pure bike-shedding.

Subclassing SystemExit just in case someday in the indefinite future 
there comes a need to add extra information to the exception falls foul 
of You Ain't Gonna Need It. Keep it simple -- if, someday, such a need 
becomes apparent, then subclass.

Steven D'Aprano

More information about the Python-Dev mailing list