I am using optparse
in to parse arguments in R. Then I call the R code from command line and pass arguments as follows Rscript myscript.R -a xx -b yy
. It works well, but I am a bit confused with the usage of 1-letter arguments like -a
, -b
, etc.
In my case I have 20 arguments… It's very inconvenient to assign a letter to each argument.
make_option(c("-o", "--output_path"), type="character"),
make_option(c("-t", "--data_type"), type="character")
If I use complete names --output_path
from command line, I get an error.
How to solve this issue?
I don't have any problems using the short or long names. When I specify both versions I can call with either
toargs <- function(x) strsplit(x, " ")[[1]][-(1:2)]
option_list <- list(make_option(c("-o", "--output_path"), type="character"))
parser <- OptionParser("test", option_list)
parse_args(parser, toargs("Rscript myscript.R --output_path xxx"))
# $output_path
# [1] "xxx"
# $help
# [1] FALSE
parse_args(parser, toargs("Rscript myscript.R -o xxx"))
# $output_path
# [1] "xxx"
# $help
# [1] FALSE
and it works with just a long version
option_list <- list(make_option("--output_path", type="character"))
parser <- OptionParser("test", option_list)
parse_args(parser, toargs("Rscript myscript.R --output_path xxx"))
# $output_path
# [1] "xxx"
# $help
# [1] FALSE
parse_args(parser, toargs("Rscript myscript.R -o xxx"))
# Error : short flag "o" is invalid
One letter arguments are not mandatory; they are optional. But you must have a long name.
Tested with optparse_1.6.4