Python 使用docopt包含多个参数列表
我将python应用程序用作命令行工具,即特性。使用该库很容易实现命令。 但是,目前我无法找到实现以下要求的方法: 文档字符串是:Python 使用docopt包含多个参数列表,python,command-line-interface,command-line-arguments,docopt,Python,Command Line Interface,Command Line Arguments,Docopt,我将python应用程序用作命令行工具,即特性。使用该库很容易实现命令。 但是,目前我无法找到实现以下要求的方法: 文档字符串是: """ aTXT tool Usage: aTXT <source>... [--ext <ext>...] Options: --ext message """ docopt输出的结果字典如下: {'--ext': True, '<ext>': [], '<source>': [
"""
aTXT tool
Usage:
aTXT <source>... [--ext <ext>...]
Options:
--ext message
"""
docopt输出的结果字典如下:
{'--ext': True,
'<ext>': [],
'<source>': ['a', 'b', 'c', 'e', 'f']}
{'--ext': True,
'<ext>': ['e', 'f', 'g'],
'<source>': ['a', 'b', 'c']}
{'--ext':True,
'': [],
'':['a','b','c','e','f']}
但是,我需要具备以下条件:
{'--ext': True,
'<ext>': [],
'<source>': ['a', 'b', 'c', 'e', 'f']}
{'--ext': True,
'<ext>': ['e', 'f', 'g'],
'<source>': ['a', 'b', 'c']}
{'--ext':True,
'':['e','f','g'],
'':['a','b','c']}
如何继续?我无法找到将列表直接传递到Docopt参数字典的方法。不过,我已经找到了一个解决方案,允许我将字符串传递到Docopt,然后将该字符串转换为列表 您的Docoptdoc存在问题,我对其进行了修订,以便测试针对您案例的解决方案。这段代码是用Python 3.4编写的 命令行:
$python3 gitHubTest.py a,b,c -e 'e,f,g'
gitHubTest.py
"""
aTXT tool
Usage:
aTXT.py [options] (<source>)
Options:
-e ext, --extension=ext message
"""
from docopt import docopt
def main(args) :
if args['--extension'] != None:
extensions = args['--extension'].rsplit(sep=',')
print (extensions)
if __name__ == '__main__':
args = docopt(__doc__, version='1.00')
print (args)
main(args)
“”“
aTXT工具
用法:
aTXT.py[选项]()
选项:
-e ext,--extension=ext消息
"""
从docopt导入docopt
def主(args):
如果参数['--扩展']!=无:
extensions=args['--extension'].rsplit(sep=',')
打印(扩展)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
args=docopt(\uuuu doc\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
打印(args)
主(args)
返回:
{
'--extension': 'e,f,g',
'<source>': 'a,b,c'
}
['e', 'f', 'g']
{
“--扩展名”:“e,f,g”,
'''a,b,c'
}
['e','f','g']
在main()中创建的变量“extensions”现在是您希望传入的列表 我希望有一种方法可以正确地使用docopt。。。据我所知,拥有像OP wants这样的多个列表是完全合法的。argparse也有可能: