[英]including more than one list of arguments with docopt
I'm using for my python application as a command line tool, the feature docopt library . 我正在使用我的python应用程序作为命令行工具,功能docopt库 。 It's easy to implement the commands with that library.
使用该库实现命令很容易。 But, for now I couldn't find out the way to accomplish the following requirement:
但是,目前我无法找到完成以下要求的方法:
the docstring is: 文档字符串是:
"""
aTXT tool
Usage:
aTXT <source>... [--ext <ext>...]
Options:
--ext message
"""
from shell, I want to write something like this: 从shell,我想写这样的东西:
atxt a b c --ext e f g
The result dictionary from docopt output is the following: docopt输出的结果字典如下:
{'--ext': True,
'<ext>': [],
'<source>': ['a', 'b', 'c', 'e', 'f']}
But, I need to have the following: 但是,我需要具备以下条件:
{'--ext': True,
'<ext>': ['e', 'f', 'g'],
'<source>': ['a', 'b', 'c']}
How do I proceed? 我该怎么办?
I have not been able to find a way of passing a list directly into the Docopt argument dictionary. 我无法找到将列表直接传递到Docopt参数字典的方法。 However, I have worked out a solution that has allowed me to pass a string into Docopt, then convert that string into a list.
但是,我已经找到了一个解决方案,它允许我将字符串传递给Docopt,然后将该字符串转换为列表。
There are issues with your Docopt doc and I revised them so that I could test the solution specific to your case. 您的Docopt 文档存在问题,我对其进行了修订,以便我可以测试特定于您的案例的解决方案。 This code was written in Python 3.4 .
此代码是用Python 3.4编写的。
command line : 命令行 :
$python3 gitHubTest.py a,b,c -e 'e,f,g'
gitHubTest.py 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)
returns : 回报:
{
'--extension': 'e,f,g',
'<source>': 'a,b,c'
}
['e', 'f', 'g']
The variable 'extensions' created in main() is now the list you were hoping to pass in. 在main()中创建的变量'extensions'现在是您希望传入的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.