为什么使用 argparse 而不是 optparse? [英] Why use argparse rather than optparse?
问题描述
我注意到 Python 2.7 文档包含另一个命令行解析模块.除了 getopt
和 optparse
之外,我们现在还有 argparse
.
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
.
为什么还要创建另一个命令行解析模块?为什么我应该使用它而不是 optparse
?是否有我应该了解的新功能?
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?
推荐答案
从 python 2.7
开始,optparse
已弃用,希望将来会消失.
As of python 2.7
, optparse
is deprecated, and will hopefully go away in the future.
argparse
出于各种原因更好在其原始页面上列出 (https://code.google.com/archive/p/argparse/):
- 处理位置参数
- 支持子命令
- 允许替代选项前缀,如
+
和/
- 处理零个或多个和一个或多个样式参数
- 生成更多信息丰富的使用消息
- 为自定义类型和操作提供更简单的界面
更多信息也在 PEP 389 中,它是由哪个 argparse
使它成为标准库.
More information is also in PEP 389, which is the vehicle by which argparse
made it into the standard library.
这篇关于为什么使用 argparse 而不是 optparse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!