重复选项列表中指定的参数

时间:2015-01-15 14:42:32

标签: docopt

是否可以采取以下措施:

Usage: 
  program submit [options]

Options:
  --user <email>            Users email address
  --documents <document>...  Documents to submit (can enter multiple)

submit --user Austin --documents doc1.txt --documents doc2.txt 

然后会提供以下JSON:

{
  submit: True,
  --user: "Austin",
  --documents: ["doc1.txt", "doc2.txt"]
}

我知道重复可选参数是可能的,我真的更愿意在选项描述中指定,而不是必须这样做:

Usage:
  program submit [--user <email>] [--documents <document>]...

如果有可能,有人可以告诉我如何实现这一目标吗?

1 个答案:

答案 0 :(得分:0)

这是一种黑客行为,但它应该有效:

"""
Usage:
  program submit [options]

Options:
  --user <email>            Users email address
  --documents <document>...  Documents to submit (can enter multiple)

submit --user Austin --documents doc1.txt --documents doc2.txt
"""

from docopt import docopt

if __name__ == '__main__':
    arguments = docopt(__doc__.replace('[options]',
                       '[options] [--documents <document>]...'))
    print(arguments)