Why is the argparse module so inflexible?

Roy Smith roy at panix.com
Thu Jun 27 09:08:05 EDT 2013


In article <mailman.3924.1372337705.3114.python-list at python.org>,
 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.
>
> [...] there's a lot of potential in the module that is crippled 
> outside the main use case.

Having used (and written) a number of different ways of dealing with CLI 
parsing (in several languages), I can tell you that argparse is pretty 
cool.  CLI parsing is amazingly complicated.  Argparse turns that into a 
job which is only moderately complicated and slightly annoying.  
Compared to the alternatives, that's a big win.

Can you give us a concrete example of what you're trying to do?

You might look into "type=".  It's normally used for things like 
"type=int" or "type=float", but it could give it any user-defined 
function as a type and this essentially becomes a hook to insert your 
own code into the middle of the processing.  Sometimes that can be 
warped into doing all sorts of useful things.



More information about the Python-list mailing list