Commit f1556ec3 authored by Dirk Eddelbuettel's avatar Dirk Eddelbuettel

Import Upstream version 1.15

parent 59f61a77
Package: getopt
Type: Package
Title: C-like getopt behavior.
Version: 1.14
Date: 2008-04-29
Version: 1.15
Date: 2010-04-28
Author: Allen Day <allenday@ucla.edu>
Maintainer: Allen Day <allenday@ucla.edu>
Description: Use this with Rscript to write ``#!'' shebang scripts that accept short and long flags/options.
License: GPL
Packaged: Wed Apr 30 02:13:57 2008; allenday
Description: Use this with Rscript to write ``#!'' shebang scripts that
accept short and long flags/options.
License: GPL (>= 2)
Packaged: 2010-04-28 08:46:22 UTC; root
Repository: CRAN
Date/Publication: 2010-04-28 09:19:33
......@@ -54,6 +54,7 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=strsplit(commandArgs(
flag.optional.argument = 2;
result = list();
result$ARGS = vector(mode="character");
#no spec. fail.
if ( is.null(spec) ) {
......@@ -85,7 +86,7 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=strsplit(commandArgs(
if ( length(unique(spec[,col.long.name])) != length(spec[,col.long.name]) ) {
stop(paste('redundant long names for flags (column ',col.long.name,').',sep=''));
}
if ( length(unique(spec[,col.short.name])) != length(spec[,col.short.name]) ) {
if ( length(na.omit(unique(spec[,col.short.name]))) != length(na.omit(spec[,col.short.name])) ) {
stop(paste('redundant short names for flags (column ',col.short.name,').',sep=''));
}
......@@ -228,6 +229,9 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=strsplit(commandArgs(
#invalid opt
if ( current.flag == 0 ) {
stop(paste('"', optstring, '" is not a valid option, or does not support an argument', sep=''));
#TBD support for positional args
#if ( debug ) print(paste('"', optstring, '" not a valid option. It is appended to getopt(...)$ARGS', sep=''));
#result$ARGS = append(result$ARGS, optstring);
# some dangling flag, handle it
} else if ( current.flag > 0 ) {
......@@ -236,9 +240,21 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=strsplit(commandArgs(
peek.optstring = opt[i + 1];
if ( debug ) print(paste(' peeking ahead at: "',peek.optstring,'"',sep=''));
#got an argument. attach it, increment the index, and move on to the next option. we don't allow arguments beginning with '-'.
if ( substr(peek.optstring, 1, 1) != '-' ) {
if ( debug ) print(' consuming argument');
#got an argument. attach it, increment the index, and move on to the next option. we don't allow arguments beginning with '-' UNLESS
#specfile indicates the value is an "integer" or "double", in which case we allow a leading dash (and verify trailing digits/decimals).
if ( substr(peek.optstring, 1, 1) != '-' |
#match negative double
( substr(peek.optstring, 1, 1) == '-'
& regexpr('^-[0123456789]*\\.?[0123456789]+$',peek.optstring) > 0
& spec[current.flag, col.mode]== 'double'
) |
#match negative integer
( substr(peek.optstring, 1, 1) == '-'
& regexpr('^-[0123456789]+$',peek.optstring) > 0
& spec[current.flag, col.mode]== 'integer'
)
) {
if ( debug ) print(paste(' consuming argument *',peek.optstring,'*',sep=''));
storage.mode(peek.optstring) = spec[current.flag, col.mode];
result[spec[current.flag, col.long.name]] = peek.optstring;
......
......@@ -109,6 +109,10 @@ should set v=3.
6. Support partial-but-unique string match on options, e.g. "--verb" and
"--verbose" both match long flag "--verbose".
7. No support for mixing in positional arguments or extra arguments that don't
match any options. For example, you can't do "my.R --arg1 1 foo bar baz" and
recover "foo", "bar", "baz" as a list. Likewise for "my.R foo --arg1 1 bar baz".
}
\value{ }
\references{ }
......@@ -166,6 +170,7 @@ spec = c(
'sd' , 's', 1, "double",
'output' , 'O', 1, "character"
);
opt = getopt(spec, c('-c', '-1', '-m', '-1.2'));
opt = getopt(spec, c('-v', '-m', '3'));
opt = getopt(spec, c('-m', '3', '-v'));
opt = getopt(spec, c('-m', '3', '-v', '2', '-v'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment