Parsing 在R中是否有处理命令行选项的包?
在R中是否有处理命令行选项的包 我知道Parsing 在R中是否有处理命令行选项的包?,parsing,command-line,r,options,getopt,Parsing,Command Line,R,Options,Getopt,在R中是否有处理命令行选项的包 我知道commandArgs,但它太基本了。它的结果基本上相当于C中的argc和argv,但我需要一些更重要的东西,就像C++中的boost::program\u options或perl中的GetOptions::Long 特别是,我想提前指定允许哪些选项,并在用户指定其他选项时给出错误消息 调用如下(使用用户选项--width=32--file=foo.txt): (请不要说,“你为什么不自己写呢,没那么难。”在其他语言中,你也不必自己写。对于内置解决方案,使
commandArgs
,但它太基本了。它的结果基本上相当于C
中的argc
和argv
,但我需要一些更重要的东西,就像C++
中的boost::program\u options
或perl
中的GetOptions::Long
特别是,我想提前指定允许哪些选项,并在用户指定其他选项时给出错误消息
调用如下(使用用户选项--width=32--file=foo.txt):
(请不要说,“你为什么不自己写呢,没那么难。”在其他语言中,你也不必自己写。对于内置解决方案,使用eval的commandArgs怎么样 测试.R
## 'trailingOnly=TRUE' means only parse args after '--args'
args=(commandArgs(trailingOnly=TRUE))
## Supply default arguments
if(length(args)==0){
print("No arguments supplied.")
##supply default values
a = 1
b = c(1,1,1)
}else{
for(i in 1:length(args)){
eval(parse(text=args[[i]]))
}
}
print(a*2)
print(b*3)
并调用它
R CMD BATCH --no-save --no-restore '--args a=1 b=c(2,5,6)' test.R test.out
使用eval的通常警告w.r.t当然适用
无耻地从这里偷来。太好了!可惜它不是R分布的一部分(至少在R2.8.1中不是)。
## 'trailingOnly=TRUE' means only parse args after '--args'
args=(commandArgs(trailingOnly=TRUE))
## Supply default arguments
if(length(args)==0){
print("No arguments supplied.")
##supply default values
a = 1
b = c(1,1,1)
}else{
for(i in 1:length(args)){
eval(parse(text=args[[i]]))
}
}
print(a*2)
print(b*3)
R CMD BATCH --no-save --no-restore '--args a=1 b=c(2,5,6)' test.R test.out