为什么使用argparse而不是optparse? [英] Why use argparse rather than optparse?

查看:753
本文介绍了为什么使用argparse而不是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?

推荐答案

从2.7开始,optparse已弃用,未来。

As of 2.7, optparse is deprecated, and will hopefully go away in the future.

argparse更适合原始网页上列出的所有原因( http://code.google.com/p/argparse/ ):

argparse is better for all the reasons listed on its original page (http://code.google.com/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

详情请参阅 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆