无法使用 docopt 包传递两个数字参数

Can't pass two numerical arguments with docopt package

在使用 R 创建命令行工具时,我决定使用 docopt package。它适用于传递标志,但我不知道如何传递两个数值。见以下代码:

#! /usr/bin/Rscript

'usage: ./test.R [-lr <low> -hr <high>]

options:
 -h --help         Shows this screen
 -lr --low <low>         Passes low risk investiment
 -hr --high <high>        Passes high risk investiment' -> doc

library(docopt)
# retrieve the command-line arguments
opts <- docopt(doc)
# what are the options? Note that stripped versions of the parameters are added to the returned list
cat(opts$high)
cat(opts$low)
str(opts) 

每当我尝试使用 ./test.R -lr 2000 -hr 4000 运行 时,它都会警告我正在加载方法包,returns 没有别的。

首先,-h 被指定了两次:一次是 "help",另一次是 "high",所以你会 运行 遇到问题。为了解决这个问题,我将对短参数使用大写字母。其次,选项的参数必须是 <angular-brackets> 或大写,所以 -lr 不起作用。 (显然它还需要一个 space 在选项和它的参数之间。)我将把它扩展为与长选项相同的命名参数。

此外(虽然可能不是严格要求),我认为逗号有助于澄清事情。 (编辑:显然 docopt.R 不喜欢前导 ./ 的用法,所以我更新了输出。)

usage: test.R [-L <low> -H <high>]

options:
 -h, --help                 Shows this screen
 -L <low>, --low <low>     Passes low risk investiment
 -H <high>, --high <high>  Passes high risk investiment

(我发现 docopthttp://docopt.org/. I found that their interactive docopt demo 的要求也很有效。)