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 <1372709120.22.0.917495801817.issue9938@psf.upfronthosting.co.za>
In-reply-to
Content
The exit and error methods are mentioned in the 3.4 documentation, but there are no examples of modifying them.

    16.4.5.9. Exiting methods
    ArgumentParser.exit(status=0, message=None)
    ArgumentParser.error(message)

test_argparse.py has a subclass that redefines these methods, though I think it is more complex than necessary.

    class ErrorRaisingArgumentParser(argparse.ArgumentParser):

In http://bugs.python.org/file30204/test_intermixed.py , part of http://bugs.python.org/issue14191 , 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.

https://github.com/nodeca/argparse 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.

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

Finally, plac ( https://pypi.python.org/pypi/plac ) is a pypi package that is built on argparse.  It has a well developed interactive mode, and integrates threads and multiprocessing.
History
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: <1372709120.22.0.917495801817.issue9938@psf.upfronthosting.co.za>
2013-07-01 20:05:20paul.j3linkissue9938 messages
2013-07-01 20:05:19paul.j3create