
Steven D'Aprano wrote:
On Sun, 4 Oct 2009 04:46:19 am Yuvgoog Greenle wrote:
I just think that if a parser error is causing the SystemExit, I would rather catch a parser error than catching a SystemExit for the sake of readability. It saves me the comments:
# Catching SystemExit because parse_args() throws SystemExit on parser errors.
But why are you catching the error? As a general rule, you *want* your command line app to exit if it can't understand the arguments you pass to it. What else can it do? Guess what you wanted?
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. I require a particular IPython exception type in order for the error to be recognized correctly in the %magic subsystem. 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. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco