The Python 2.7 documentation provides yet another command-line parsing module, which I discovered. We now have argparse in addition to getopt and optparse.
Why have we written yet another command-line parsing module? Why should I choose it over optparse? Are there any new features I should be aware of?
Asked by fmark
As of python 2.7, optparse is deprecated, and will hopefully go away in the future.
argparse is better for all the reasons listed on its original page (https://code.google.com/archive/p/argparse/):
PEP 389, the vehicle by which argparse made it into the standard library, has more details.
Answered by Nicholas Knight
I believe @Nicholas’ response covers this well, but not the larger “meta” question you begin with:
When a useful module is added to the standard library, the first challenge is what to do when a significantly superior, but backwards-incompatible, alternative to offer the same functionality develops.
Either you stick with the tried-and-true method (usually when dealing with complex packages: asyncore vs twisted, tkinter vs wx or Qt,…) or you try something new. else you wind up with several incompatible ways to perform the same thing (XML parsers, IMHO, are a better illustration than command-line parsers — but the email package vs. the myriad old ways to deal with comparable difficulties isn’t far behind;-).
You can make ominous grumbles in the documentation about the old approaches being “deprecated,” but you can’t truly take them away without preventing huge, essential apps from upgrading to newer Python releases (as long as backwards compatibility is required).
(Dilemma number two, which isn’t directly related to your question, is summed up in the old adage that “the standard library is where good packages go to die”… with releases every year and a half or so, packages that aren’t very, very stable and don’t require releases more frequently than that can actually suffer significantly by being “frozen” in the standard library… but that’s a different issue).
Answered by Alex Martelli
PEP 389: argparse – New Command Line Parsing Module, in particular the section titled, Why aren’t getopt and optparse enough?, is the greatest source for rationale for a Python addition.
Answered by Ned Batchelder
There are also some newcomers on the scene!
Please read this for a more in-depth comparison, and you may end up using docopt or click. Kyle Purdon is to be commended!
Answered by lony
I was hesitant to move from optparse to argparse at first, just like @fmark, because:
Then I came across this doc, which claims that argparse trumps optparse when it comes to providing meaningful assistance messages: http://argparse.googlecode.com/svn/trunk/doc/argparse-vs-optparse.html
Then I discovered @Nicholas’ “argparse vs. optparse,” which claimed that argparse would be available in Python 2.7. (I didn’t realize that previously.)
My two main issues have now been addressed. I wrote this in the hopes of assisting those who share my viewpoint.
Answered by RayLuo
Post is based on https://stackoverflow.com/questions/3217673/why-use-argparse-rather-than-optparse