Python-docopt和argparse之间的区别

Python-docopt和argparse之间的区别,python,command-line-arguments,argparse,docopt,Python,Command Line Arguments,Argparse,Docopt,我必须编写一个命令行界面,我已经看到我可以使用docopt和argparse 我想知道两者之间的主要区别是什么,以便我能够作出明智的选择 请坚持事实。我不想要哇。docopt。如此美丽。非常有用。Docopt解析文档字符串,而argparse通过创建对象实例并通过函数调用向其添加行为来构造其解析 argparse示例: parser = argparse.ArgumentParser() parser.add_argument("operation", help="mathematical op

我必须编写一个命令行界面,我已经看到我可以使用
docopt
argparse

我想知道两者之间的主要区别是什么,以便我能够作出明智的选择


请坚持事实。我不想要哇。docopt。如此美丽。非常有用。

Docopt解析文档字符串,而argparse通过创建对象实例并通过函数调用向其添加行为来构造其解析

argparse示例:

parser = argparse.ArgumentParser()
parser.add_argument("operation", help="mathematical operation that will be performed", 
    choices=['add', 'subtract', 'multiply', 'divide'])
parser.add_argument("num1", help="the first number", type=int)
parser.add_argument("num2", help="the second number", type=int)
args = parser.parse_args()
"""Calculator using docopt

Usage:
  calc_docopt.py <operation> <num1> <num2>
  calc_docopt.py (-h | --help)

Arguments:
  <operation>  Math Operation
  <num1>       First Number
  <num2>       Second Number

Options:
  -h, --help     Show this screen.

"""
from docopt import docopt

if __name__ == '__main__':
    arguments = docopt(__doc__, version='Calculator with docopt')
    print(arguments)
docopt示例:

parser = argparse.ArgumentParser()
parser.add_argument("operation", help="mathematical operation that will be performed", 
    choices=['add', 'subtract', 'multiply', 'divide'])
parser.add_argument("num1", help="the first number", type=int)
parser.add_argument("num2", help="the second number", type=int)
args = parser.parse_args()
"""Calculator using docopt

Usage:
  calc_docopt.py <operation> <num1> <num2>
  calc_docopt.py (-h | --help)

Arguments:
  <operation>  Math Operation
  <num1>       First Number
  <num2>       Second Number

Options:
  -h, --help     Show this screen.

"""
from docopt import docopt

if __name__ == '__main__':
    arguments = docopt(__doc__, version='Calculator with docopt')
    print(arguments)
使用docopt的计算器 用法: calc_docopt.py calc_docopt.py(-h |--帮助) 论据: 数学运算 第一个数字 第二个数字 选项: -h、 --帮助显示此屏幕。 """ 从docopt导入docopt 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': 参数=docopt(\uuuu doc\uuuu,version='Calculator with docopt') 打印(参数)
注意,docopt使用
用法:
选项:
部分进行解析。此处
参数:
仅为方便最终用户而提供。

argparse
位于python默认库中,因此不会向程序添加任何额外的依赖项。区别主要在于编写代码的方式。使用
argparse
可以为插件添加钩子,以便它们可以将自己的argumnet添加到程序中。例如,我们使用这个


docopt
是一个第三方模块,提供了解析参数的简单方法。我个人喜欢
docopt
,因为它简单,但我并不是说它在所有情况下都是最好的。在他们的文档中,他们提到使用
docopt
可以使用比使用
argparse

时更多的参数传递组合

对argparse、docopt和click本身进行了很好的比较


是Python的另一个命令行解析实用程序。

argparse
在默认的Python包中,而
docopt
是一个单独的模块。在使用了一年之后,我认为值得一提的是,由于docopt是最新的,不是Python核心的一部分,它可能无法在较旧的系统上使用。i、 e,这不是Debian哮喘病(而是Jessie)。这也是为什么你不应该使用它。。。