gpt4 book ai didi

python - 使用 docopt 包含多个参数列表

转载 作者:行者123 更新时间:2023-12-02 06:58:09 24 4
gpt4 key购买 nike

我正在使用docopt library .

我找不到完成以下要求的方法:

文档字符串是:

"""
aTXT tool

Usage:
aTXT <source>... [--ext <ext>...]

Options:
--ext message

"""

从 shell 中,我想写这样的东西:

atxt a b c --ext e f g

来自 docopt 输出的结果字典如下:

 {'--ext': True,
'<ext>': [],
'<source>': ['a', 'b', 'c', 'e', 'f']}

但是,我需要它如下:

 {'--ext': True,
'<ext>': ['e', 'f', 'g'],
'<source>': ['a', 'b', 'c']}

我该如何继续?

最佳答案

我无法找到将列表直接传递到 Docopt 参数字典中的方法。不过,我已经制定了一个解决方案,允许我将字符串传递到 Docopt,然后将该字符串转换为列表。

您的 Docopt __doc__ 存在问题,我对其进行了修改,以便我可以测试针对您的案例的解决方案。此代码是用 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)

它返回:

{
'--extension': 'e,f,g',
'<source>': 'a,b,c'
}
['e', 'f', 'g']

main() 中创建的变量 extensions 现在是您希望传入的列表。

关于python - 使用 docopt 包含多个参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29313995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com