[R] getopt does not work as expected!

Juergen Rose rose at rz.uni-potsdam.de
Fri Mar 30 23:09:53 CEST 2012


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))




More information about the R-help mailing list