[Python-Dev] PEP 389: argparse - new command line parsing module

Nick Coghlan ncoghlan at gmail.com
Wed Sep 30 02:25:48 CEST 2009


Greg Ewing wrote:
> skip at pobox.com wrote:
>> I have never completely wrapped my brain around optparse.  Getopt I
>> just remember.
> 
> Seems to me that optparse and argparse are fairly similar
> in their APIs, and that argparse isn't going to be significantly
> easier to fit in one's brain than optparse.
> 
> There's an art to coming up with an API that makes simple
> things easy and other things possible. I'm not convinced that
> argparse represents a subsantial enough advancement in
> that art to justify replacing optparse with it in the stdlib,
> and thereby forcing everyone to learn a similar-but-different
> API.

As someone that has written multiple optparse based utility scripts, I
would say that yes, argparse is a *huge* improvement. Several things
that I implemented for my own use in a rather clumsy fashion
(subcommands, aggregated parsers, non-interspersed arguments) have far
more elegant support built directly into argparse.

For the getopt-vs-opt/argparse discussion, I believe the major
distinction is in the relative balance between procedural and
declarative style of argument handling.

getopt is very procedural - you define a minimal amount regarding the
options you accept, but then do the bulk of the command line processing
yourself

optparse is declarative to some degree, but forces you to drop back to a
procedural style to handle arguments and subcommands.

argparse takes things even further in a declarative direction by adding
explicit support for positional arguments and subcommands.

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-Dev mailing list