I have the following script (also attached):
#!/usr/bin/Rscript
spec=matrix(c(
'verbose', 'v', 1, "integer",
'help' , 'h', 0, "logical"
),ncol=4, byrow=TRUE)
spec.dim=dim(spec)
spec.opt.long=spec[,1]
spec.opt.short=spec[,2]
spec.opt.l <- spec.dim[1]
infile <- "test.dat"
args=commandArgs(TRUE); l=length(args)
self = commandArgs()[1];
usage <- function() {
print(sprintf("Usage: %s [--verbose|-v] [--help,-h]
[infile.dat]"),self)
print(sprintf(" default infile.dat = '%
s'",infile))
}
library('getopt')
opt = getopt(spec,debug=TRUE);
if ( !is.null(opt$help) ) {
## get the script name (only works when invoked with Rscript).
usage()
print("------ before 'q(status=0)' ------")
q(status=0);
}
if ( is.null(opt$verbose ) ) { opt$verbose = 0 }
if ( is.na(opt$verbose ) ) { opt$verbose = 0 }
print(sprintf("%s: do something with %s!",self,infile))
If I run this script from the command line, I get the output:
rose at moose:/home_moose/rose/Txt/src/Test/R(24)$ ./getopt_test.R
[1] "/usr/lib64/R/bin/exec/R: do something with test.dat!"
Why is commandArgs()[1] = "/usr/lib64/R/bin/exec/R" and not
"getopt_test.R" as described by the help (?getopt)?
rose at moose:/home_moose/rose/Txt/src/Test/R(25)$ ./getopt_test.R -h
[1] "processing -h"
[1] " short option: -h"
[1] "Usage: getopt_test.R [--verbose|-v] [--help,-h] [infile.dat]"
[1] " default infile.dat = 'test.dat'"
[1] "------ before 'q(status=0)' ------"
rose at moose:/home_moose/rose/Txt/src/Test/R(26)$ ./getopt_test.R --help
[1] "processing --help"
[1] " long option: --help"
[1] " dangling flag"
[1] "Usage: getopt_test.R [--verbose|-v] [--help,-h] [infile.dat]"
[1] " default infile.dat = 'test.dat'"
[1] "------ before 'q(status=0)' ------"
rose at moose:/home_moose/rose/Txt/src/Test/R(27)$ ./getopt_test.R -b
[1] "processing -b"
[1] " short option: -b"
Fehler in getopt(spec, debug = TRUE) : short flag "b" is invalid
Ausf?hrung angehalten
Last three commands worked as expected.
rose at moose:/home_moose/rose/Txt/src/Test/R(28)$ ./getopt_test.R --b
[1] "processing --b"
[1] " long option: --b"
[1] " dangling flag"
Fehler in getopt(spec, debug = TRUE) : flag "b" requires an argument
Ausf?hrung angehalten
That the command stopped is OK, but I expected the error message like
"long flag "b" is invalid".
rose at moose:/home_moose/rose/Txt/src/Test/R(29)$ ./getopt_test.R bla.dat
[1] "processing bla.dat"
Fehler in getopt(spec, debug = TRUE) :
"bla.dat" is not a valid option, or does not support an argument
Ausf?hrung angehalten
This behaviour I don't like at all. Why getopt does not distinguish
beetween options starting with alt least one hyphen and normal arguments
starting without hyphen? How it is possible to handle such a situation?
Have I myself to split the arguments into two lists, one with hyphen and
one without hyphen, and then pass only this list with hyphen to getopt?
Or is there a better solution?
Regards Juergen
-------------- next part --------------
#!/usr/bin/Rscript
spec=matrix(c(
'verbose', 'v', 1, "integer",
'help' , 'h', 0, "logical"
),ncol=4, byrow=TRUE)
spec.dim=dim(spec)
spec.opt.long=spec[,1]
spec.opt.short=spec[,2]
spec.opt.l <- spec.dim[1]
infile <- "test.dat"
args=commandArgs(TRUE); l=length(args)
self = commandArgs()[1];
usage <- function() {
print(sprintf("Usage: getopt_test.R [--verbose|-v] [--help,-h]
[infile.dat]"))
print(sprintf(" default infile.dat =
'%s'",infile))
}
library('getopt')
opt = getopt(spec,debug=TRUE);
#opt = getopt(spec);
#print(opt,usage=TRUE)
## help was asked for.
if ( !is.null(opt$help) ) {
## get the script name (only works when invoked with Rscript).
self = commandArgs()[1];
## print a friendly message and exit with a non-zero error code
usage()
print("------ before 'q(status=0)' ------")
q(status=0);
}
## set some reasonable defaults for the options that are needed,
## but were not specified.
if ( is.null(opt$verbose ) ) { opt$verbose = 0 }
if ( is.na(opt$verbose ) ) { opt$verbose = 0 }
print(sprintf("%s: do something with %s!",self,infile))
Am Mittwoch, den 28.03.2012, 14:08 +0200 schrieb Juergen Rose:> I have the following script (also attached): >...> Ausf?hrung angehalten > > This behaviour I don't like at all. Why getopt does not distinguish > beetween options starting with alt least one hyphen and normal arguments > starting without hyphen? How it is possible to handle such a situation? > Have I myself to split the arguments into two lists, one with hyphen and > one without hyphen, and then pass only this list with hyphen to getopt? > Or is there a better solution?I now extended getopt to mygetopt (look at the attached file), which is now able to distinguish between options and remaining arguments. But my solution is longish. I hope somebody can give me a shorter solution. -------------- next part -------------- #!/usr/bin/Rscript spec=matrix(c( 'verbose', 'v', 1, "integer", 'help' , 'h', 0, "logical", 'debug', 'd', 2, "integer" ),ncol=4, byrow=TRUE) spec.dim=dim(spec) spec.opt.long=spec[,1] spec.opt.short=spec[,2] spec.opt.type=spec[,3] spec.opt.l <- spec.dim[1] infile <- "test.dat" args=commandArgs(TRUE); l.args=length(args) self = commandArgs()[1]; usage <- function() { print(sprintf("Usage: getopt_test.R [--verbose|-v] [--help,-h] [infile.dat]")) print(sprintf(" default infile.dat = '%s'",infile)) } library('getopt') i.arg=0 i.opt=0 args.new=c() opts=c() i=1 if(l.args>=1) { while (i <= l.args) { print(sprintf("i=%2d l.args=%2d",i,l.args)) print(sprintf("i=%2d args[[%2d]]=%-20s substring(args[[i]],1,1)=|%s|", i,i,args[[i]],substring(args[[i]],1,1))) if (substring(args[[i]],1,1) == "-") { i.opt <- i.arg+1 opts=c(opts,args[[i]]) if (spec.opt.type[i]==1) { if (i+1 <= l.args) { if (substring(args[[i+1]],1,1) != "-") { i <- i+1 opts=c(opts,args[[i]]) } else { print(sprintf("ERROR in parameters, missing value for option %s", args[[i]])); q(status=1) } } else { print(sprintf("ERROR in parameters")); q(status=1) } } if (spec.opt.type[i]==2) { if (i+1 <= l.args) { if (substring(args[[i+1]],1,1) != "-") { i <- i+1 opts=c(opts,args[[i]]) } } } i <- i+1 } else { i.arg <- i.arg+1 args.new=c(args.new,args[[i]]) } } } opt = getopt(spec,opt=opts,debug=TRUE); #opt = getopt(spec); #print(opt,usage=TRUE) ## help was asked for. if ( !is.null(opt$help) ) { ## get the script name (only works when invoked with Rscript). self = commandArgs()[1]; ## print a friendly message and exit with a non-zero error code usage() print("------ before 'q(status=0)' ------") q(status=0); } ## set some reasonable defaults for the options that are needed, ## but were not specified. print("before 1st print(opt$verbose)"); print(opt$verbose) if ( is.null(opt$verbose ) ) { opt$verbose = 0 } if ( is.na(opt$verbose ) ) { opt$verbose = 0 } print("before 2nd print(opt$verbose)"); print(opt$verbose) print(sprintf("%s: do something with %s!",self,infile))
> Why is commandArgs()[1] = "/usr/lib64/R/bin/exec/R" and not > "getopt_test.R" as described by the help (?getopt)?That used to work but R changed the behavior of the commandArgs() function a couple of years ago, I guess the orginal author of getopt didn't update his example then. This will do the trick: args <- commandArgs() file_index <- grepl("--file=", args) self <- gsub("--file=", "", args[file_index])> This behaviour I don't like at all. Why getopt does not distinguish > beetween options starting with alt least one hyphen and normal arguments > starting without hyphen? How it is possible to handle such a situation? > Have I myself to split the arguments into two lists, one with hyphen and > one without hyphen, and then pass only this list with hyphen to getopt? > Or is there a better solution?The author of getopt started but did not finish adding positional arguments to ``getopt``. Howevor, positional arguments have been supported in the ``optparse`` package for over a year if you specify ``positional_args=TRUE`` to the ``parse_args`` function. - Trevor Davis
Seemingly Similar Threads
- Pkg creation: Sweave: multiple files vignette: Error in R CMD check
- commandArgs()
- scripting/littler: How to call function named iteratively (`f1`, `f2`, …)?
- make check fails if called with --without-recommended-packages
- namespace crash on S3method("as.ff",function)