# command_args() is recommended over direct use of scribeCommandArgs$new()
ca <- command_args(c(1, 2, 3, "--verbose"))
ca$add_argument("--verbose", action = "flag")
ca$add_argument("...", "values", info = "values to add", default = 0.0)
args <- ca$parse()
if (args$verbose) {
message("Adding ", length(args$values), " values")
}
sum(args$values)
# $parse() returns a named list, which means scribeCommandArgs can function
# as a wrapper for calling R functions inside Rscript
ca <- command_args(c("mean", "--size", 20, "--absolute"))
ca$add_argument("fun", action = "list")
ca$add_argument("--size", default = 5L)
ca$add_argument("--absolute", action = "flag")
args <- ca$parse()
my_function <- function(fun, size, absolute = FALSE) {
fun <- match.fun(fun)
x <- sample(size, size, replace = TRUE)
res <- fun(x)
if (absolute) res <- abs(res)
res
}
do.call(my_function, args)
Run the code above in your browser using DataLab