【问题标题】:R optparse error with command line arguments带有命令行参数的 R optparse 错误
【发布时间】:2017-10-01 04:11:57
【问题描述】:

由于某种原因,此脚本中的 optparse 用法中断:

test.R:

#!/usr/bin/env Rscript
library("optparse")

option_list <- list(
    make_option(c("-n", "--name"), type="character", default=FALSE,
                dest="report_name", help="A different name to use for the file"),
    make_option(c("-h", "--height"), type="numeric", default=12,
                dest = "plot_height", help="Height for plot [default %default]",
                metavar="plot_height"),
    make_option(c("-w", "--width"), type="numeric", default=10,
                dest = "plot_width", help="Width for plot [default %default]",
                metavar="plot_width")
)

opt <- parse_args(OptionParser(option_list=option_list), positional_arguments = TRUE)
print(opt)

report_name <- opt$options$report_name
plot_height <- opt$options$plot_height
plot_width <- opt$options$plot_width

input_dir <- opt$args[1] # input directory

我收到此错误:

    $ ./test.R --name "report1" --height 42 --width 12 foo
Error in getopt(spec = spec, opt = args) :
  redundant short names for flags (column 2).
Calls: parse_args -> getopt
Execution halted

但是,如果我从该行中删除 "-h"

make_option(c("--height"), type="numeric", default=12,
                    dest = "plot_height", help="Height for plot [default %default]"

它似乎工作正常;

$ ./test.R --name "report1" --height 42 --width 12 foo
$options
$options$report_name
[1] "report1"

$options$plot_height
[1] 42

$options$plot_width
[1] 12

$options$help
[1] FALSE


$args
[1] "foo"

有什么想法吗?

我正在使用 R 3.3.0 和 optparse_1.3.2 (getopt_1.20.0)

【问题讨论】:

    标签: r optparse


    【解决方案1】:

    -h 标志由 optparse 保留(描述为 optparse 的一个功能,不在 getopt 中,from the getopt.R source file on Github):

    在 optparse 包中实现的某些功能在 getopt 中不可用:

    2。遇到“-h”时自动生成帮助选项并打印帮助文本

    因此,当用户指定-h 时,标志唯一性的sanity check 将失败。但是,issue tracker 似乎没有提到需要为这种情况创建更好的错误消息。

    最后,请注意 optparse 似乎在调用 getopt,因为它们的作者相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-30
      • 2019-06-19
      • 2013-08-13
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多