如何在docopt中多次指定一个可选参数

and*_*s-h 9 python docopt

我想以一种方式设计我的命令行应用程序,让我们称之为注释,可以多次指定,例如,

$ ./my_app.py --comment="Comment 1" --comment="Comment 2"
Run Code Online (Sandbox Code Playgroud)

可以用docopt完成吗?我检查了docopt主页,但找不到对同一可选参数的多个出现的任何引用.

tgr*_*ray 5

作为参考,官方文档可以在 github找到

要回答您的特定问题,您可以...在可选选项中使用省略号,[--my-option]并指定您的选项接受一个参数。

[--my-option=ARG]...[--my-option=<arg>]...

例子:

"""
Usage:
    my_program [--comment=ARG]... FILE

Arguments:
    FILE       An argument for passing in a file.

Options:
    --comment  Zero or more comments
"""
Run Code Online (Sandbox Code Playgroud)

通过指定它来[--comment=<arg>]...确保 opt['--comment'] 是所有指定注释的列表。

执行: my_program --comment=ASDF --comment=QWERTY my_file

造成:

if __name__ == '__main__':
    opts = docopt(__doc__)
    opts['--comment'] == ['ASDF', 'QWERTY']
    opts['FILE'] == 'my_file'
Run Code Online (Sandbox Code Playgroud)