-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow repeated instances of same flag/option on command line #36
Comments
My answer from before still stands:
Note the upstream
|
I also run into the same problem. Simultaneously I would like to keep Thus I came up with a workaround, which can be useful for other people landing on this issue. The function combines all repeated arguments ( combine.repeated.opt = function(obj, args, sep = "|") {
opts <- obj@options
opts <- opts[sapply(opts, function(x) x@type == "character")]
for (opt in opts) {
n <- length(args)
values <- character(0)
to.remove <- integer(0)
x <- opt@short_flag
if (!is.na(x)) {
pattern <- paste0("^",x,"$")
i <- grep(pattern, args)
i <- i[i+1 <= n]
values <- c(values, args[i+1])
to.remove <- c(to.remove, c(i,i+1))
}
x <- opt@long_flag
pattern <- paste0("^",x,"$")
i <- grep(pattern, args)
i <- i[i+1 <= n]
values <- c(values, args[i+1])
to.remove <- c(to.remove, c(i,i+1))
pattern <- paste0("^",x,"=")
i <- grep(pattern, args)
values <- c(values,sub(pattern,"",args[i]))
to.remove <- c(to.remove, c(i))
if (length(values) > 1) {
i = min(to.remove)-1
args = args[-to.remove]
n = length(args)
newargs = c(opt@long_flag, paste0(values, collapse=sep))
args = c(
args[seq_len(i)],
newargs,
args[seq_len(n-i)+i]
)
}
}
args
} Example of usage: require(optparse)
options <- list(
make_option(c("-i", "--include"), action="store", type="character", help="add an include directory"),
make_option(c("-o", "--output"), action="store", type="character", help="output file")
)
# args <- commandArgs()
args <- c("program", "-i", "/usr/include", "-o", "file", "-i", "/usr/X11/include")
parser <- OptionParser(option_list=options)
args <- combine.repeated.opt(parser, args)
args <- parse_args2(parser, args = args)
includes <- strsplit(args$options$include,split="[|]")[[1]] This workaround doesn't affect any usage without repeated options. @trevorld If you think you'd like this (or similar) solution in the package I can contribute it trough a pull-request. In the package it could be implemented as an option to |
|
many Unix commands allow the same option to be repeated on the command line. for example, the more instances of the debug flag
-d
, the more verbose the debugging output. or, for the C compiler, each instance of-I
specifies a different directory in which to look for#include
files.also, for some applications, it's important that the options be "presented" to the program in the same order in which they appear on the command line.
it would be nice if
optparse
supported this style. here's an example, using callback:bonus points for extending the
action
parameter with"append"
and"count"
actions so one doesn't always need to usecallback
. (though the github's helpful "Similar issues" bit shows me #22 already asked for these.)The text was updated successfully, but these errors were encountered: