为什么使用 argparse 而不是 optparse?
我注意到 Python 2.7 文档还包含另一个命令行解析模块。除了 getopt
和 optparse
之外,我们现在还有 argparse
。
为什么又创建了一个命令行解析模块?为什么我应该使用它而不是 optparse
?有我应该了解的新功能吗?
I noticed that the Python 2.7 documentation includes yet another command-line parsing module. In addition to getopt
and optparse
we now have argparse
.
Why has yet another command-line parsing module been created? Why should I use it instead of optparse
? Are there new features that I should know about?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从 python
2.7
开始,optparse
< /a> 已弃用,希望将来会消失。由于其上列出的所有原因,
argparse
更好原始页面 (https://code.google.com/archive/p/argparse/):+
和/
等替代选项前缀,更多信息也位于 PEP 389,它是 argparse 进入标准库的工具。
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/):+
and/
More information is also in PEP 389, which is the vehicle by which
argparse
made it into the standard library.我认为@Nicholas 的答案很好地涵盖了这一点,但不是您开始的更多“元”问题:
当任何有用的模块被添加到标准库中时,这是第一个问题:当出现一种更好但向后不兼容的提供相同类型功能的方法时,您该怎么办?
要么你坚持使用旧的和公认的超越的方式(通常当我们谈论复杂的包时:asyncore 与twisted,tkinter 与wx 或Qt,...),要么你最终以多种不兼容的方式来做同样的事情(XML恕我直言,解析器是比命令行解析器更好的例子——但是
email
包与处理类似问题的无数旧方法也相差不远;-)。您可能会在文档中抱怨旧方法被“弃用”,但是(只要您需要保持向后兼容性)您就无法真正将它们删除,除非阻止大型、重要的应用程序迁移到较新的 Python 版本。
(第二个困境,与你的问题没有直接关系,用一句老话来概括:“标准库是好的软件包消亡的地方”......大约每年半发布一次,软件包不是很好, 非常稳定,不需要比这更频繁的发布,实际上可能会因为被“冻结”在标准库中而受到严重影响......但是,这确实是一个不同的问题) 。
@Nicholas's answer covers this well, I think, but not the more "meta" question you start with:
That's the dilemma number one when any useful module is added to the standard library: what do you do when a substantially better, but backwards-incompatible, way to provide the same kind of functionality emerges?
Either you stick with the old and admittedly surpassed way (typically when we're talking about complicated packages: asyncore vs twisted, tkinter vs wx or Qt, ...) or you end up with multiple incompatible ways to do the same thing (XML parsers, IMHO, are an even better example of this than command-line parsers -- but the
email
package vs the myriad old ways to deal with similar issues isn't too far away either;-).You may make threatening grumbles in the docs about the old ways being "deprecated", but (as long as you need to keep backwards compatibility) you can't really take them away without stopping large, important applications from moving to newer Python releases.
(Dilemma number two, not directly related to your question, is summarized in the old saying "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, not needing releases any more often than that, can actually suffer substantially by being "frozen" in the standard library... but, that's really a different issue).
添加 Python 的最佳理由是其 PEP:PEP 389:argparse - New命令行解析模块,特别是标题为 为什么 getopt 和 optparse 还不够?
The best source for rationale for a Python addition would be its PEP: PEP 389: argparse - New Command Line Parsing Module, in particular, the section entitled, Why aren't getopt and optparse enough?
街区里还有新孩子!
如果您需要更深入的比较,请阅读这个然后你最终可能会使用docopt或click。感谢凯尔·珀登!
There are also new kids on the block!
If you need a more in-depth comparison please read this and you may end up using docopt or click. Thanks to Kyle Purdon!
起初,我和 @fmark 一样不愿意从 optparse 切换到 argparse,因为:
然后我看到这个文档,argparse 优于 optparse,特别是在谈论生成有意义的帮助消息时: http://argparse.googlecode.com/svn/trunk/doc/argparse-vs-optparse.html
然后我看到“argparse 与 optparse" 作者:@Nicholas,说我们可以在 python <2.7 中使用 argparse (是的,我以前不知道这一点。)
现在我的有两个问题得到了很好的解决。我写这篇文章希望它能帮助其他有类似心态的人。
At first I was as reluctant as @fmark to switch from optparse to argparse, because:
Then I saw this doc, argparse outperforms optparse, especially when talking about generating meaningful help message: http://argparse.googlecode.com/svn/trunk/doc/argparse-vs-optparse.html
And then I saw "argparse vs. optparse" by @Nicholas, saying we can have argparse available in python <2.7 (Yep, I didn't know that before.)
Now my two concerns are well addressed. I wrote this hoping it will help others with a similar mindset.