为什么使用 argparse 而不是 optparse?

发布于 2024-09-09 01:39:00 字数 191 浏览 8 评论 0原文

我注意到 Python 2.7 文档还包含另一个命令行解析模块。除了 getoptoptparse 之外,我们现在还有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

愿得七秒忆 2024-09-16 01:39:00

从 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/):

  • handling positional arguments
  • supporting sub-commands
  • allowing alternative option prefixes like + and /
  • handling zero-or-more and one-or-more style arguments
  • producing more informative usage messages
  • providing a much simpler interface for custom types and actions

More information is also in PEP 389, which is the vehicle by which argparse made it into the standard library.

酷炫老祖宗 2024-09-16 01:39:00

为什么我应该使用它而不是
选择解析?他们的新功能是我吗?
应该知道什么?

我认为@Nicholas 的答案很好地涵盖了这一点,但不是您开始的更多“元”问题:

为什么还有另一个命令行
解析模块已创建?

当任何有用的模块被添加到标准库中时,这是第一个问题:当出现一种更好但向后不兼容的提供相同类型功能的方法时,您该怎么办?

要么你坚持使用旧的和公认的超越的方式(通常当我们谈论复杂的包时:asyncore 与twisted,tkinter 与wx 或Qt,...),要么你最终以多种不兼容的方式来做同样的事情(XML恕我直言,解析器是比命令行解析器更好的例子——但是 email 包与处理类似问题的无数旧方法也相差不远;-)。

您可能会在文档中抱怨旧方法被“弃用”,但是(只要您需要保持向后兼容性)您就无法真正将它们删除,除非阻止大型、重要的应用程序迁移到较新的 Python 版本。

(第二个困境,与你的问题没有直接关系,用一句老话来概括:“标准库是好的软件包消亡的地方”......大约每年半发布一次,软件包不是很好, 非常稳定,不需要比这更频繁的发布,实际上可能会因为被“冻结”在标准库中而受到严重影响......但是,这确实是一个不同的问题) 。

Why should I use it instead of
optparse? Are their new features I
should know about?

@Nicholas's answer covers this well, I think, but not the more "meta" question you start with:

Why has yet another command-line
parsing module been created?

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).

青春有你 2024-09-16 01:39:00

添加 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?

摇划花蜜的午后 2024-09-16 01:39:00

街区里还有新孩子!

  • 除了已经提到的已弃用的 optparse 之外。 [请勿使用]
  • 还提到了 argparse,这是对于不愿意包含外部库的人的解决方案。
  • docopt 是一个值得关注的外部库,它使用文档字符串作为输入的解析器。
  • click 也是外部库,并使用装饰器来定义参数。 (我的资料推荐:Why Click
  • python-inquirer供选择重点工具并基于 Inquirer.js (repo)

如果您需要更深入的比较,请阅读这个然后你最终可能会使用docoptclick。感谢凯尔·珀登!

There are also new kids on the block!

  • Besides the already mentioned deprecated optparse. [DO NOT USE]
  • argparse was also mentioned, which is a solution for people not willing to include external libs.
  • docopt is an external lib worth looking at, which uses a documentation string as the parser for your input.
  • click is also external lib and uses decorators for defining arguments. (My source recommends: Why Click)
  • python-inquirer For selection focused tools and based on Inquirer.js (repo)

If you need a more in-depth comparison please read this and you may end up using docopt or click. Thanks to Kyle Purdon!

帝王念 2024-09-16 01:39:00

起初,我和 @fmark 一样不愿意从 optparse 切换到 argparse,因为:

  1. 我认为差异并没有那么大。
  2. 相当多的VPS仍然默认提供Python 2.6。

然后我看到这个文档,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:

  1. I thought the difference was not that huge.
  2. Quite some VPS still provides Python 2.6 by default.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文