Why is the argparse module so inflexible?

Joshua Landau joshua.landau.ws at gmail.com
Thu Jun 27 09:08:29 EDT 2013


On 27 June 2013 13:54, Andrew Berg <robotsondrugs at gmail.com> wrote:
> I've begun writing a program with an interactive prompt, and it needs to parse input from the user. I thought the argparse module would be
> great for this, but unfortunately it insists on calling sys.exit() at any sign of trouble instead of letting its ArgumentError exception
> propagate so that I can handle it. Overriding ArgumentParser.error doesn't really help since methods like parse_known_args just send a
> message to be relayed to the user as an argument after swallowing ArgumentError (which does have useful information in its attributes). If I
> wanted to figure out what actually caused the exception to be raised, I'd have to parse the message, which is ugly at best. I understand
> that most people do want argparse to just display a message and terminate if the arguments supplied aren't useful, but there's a lot of
> potential in the module that is crippled outside the main use case. I have to wonder why a module that is meant to be imported would ever
> call sys.exit(), even if that is what the caller would normally do if presented with an exception.

>>> import sys
>>> try: sys.exit()
... except SystemExit: pass
...
>>>

That said, you might want to try docopt [http://docopt.org/] if you
have qualms with ArgParse.



More information about the Python-list mailing list