Author paul.j3
Recipients a_b, bethard, docs@python, eric.araujo, ethan.furman, ezio.melotti, jayt, paul.j3, r.david.murray, tanqazx, xuanji
Date 2013-07-01.20:05:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The exit and error methods are mentioned in the 3.4 documentation, but there are no examples of modifying them. Exiting methods
    ArgumentParser.exit(status=0, message=None)
    ArgumentParser.error(message) has a subclass that redefines these methods, though I think it is more complex than necessary.

    class ErrorRaisingArgumentParser(argparse.ArgumentParser):

In , part of , which creates a parser mode that is closer to optparse in style, I simply use:

    def error(self, message):
        usage = self.format_usage()
        raise Exception('%s%s'%(usage, message))
    ArgumentParser.error = error

to catch errors. a Javascript port of argparse, adds a 'debug' option to the ArgumentParser, that effectively redefines this error method.  They use that extensively in testing.

Another approach is to trap the sysexit.  Ipython does that when argparse is run interactively.

Even the simple try block works, though the SystemExit 2 has no information about the error.

        args = parser.parse_args('X'.split())
    except SystemExit as e:

Finally, plac ( ) is a pypi package that is built on argparse.  It has a well developed interactive mode, and integrates threads and multiprocessing.
Date User Action Args
2013-07-01 20:05:20paul.j3setrecipients: + paul.j3, bethard, ezio.melotti, eric.araujo, r.david.murray, docs@python, ethan.furman, jayt, xuanji, tanqazx, a_b
2013-07-01 20:05:20paul.j3setmessageid: <>
2013-07-01 20:05:20paul.j3linkissue9938 messages
2013-07-01 20:05:19paul.j3create