paul j3 added the comment:
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.
----------
nosy: +paul.j3
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue9938>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com