Skip to content

Instantly share code, notes, and snippets.

@wookietreiber
Last active February 17, 2017 13:22
Show Gist options
  • Save wookietreiber/c80bb4d18759a61df76eb5b928f01ea1 to your computer and use it in GitHub Desktop.
Save wookietreiber/c80bb4d18759a61df76eb5b928f01ea1 to your computer and use it in GitHub Desktop.
library(optparse)
# ------------------------------------------------------------------------------
# defaults
# ------------------------------------------------------------------------------
default.nthreads <- 1
default.verbose <- FALSE
# ------------------------------------------------------------------------------
# parsing arguments
# ------------------------------------------------------------------------------
options <- list (
make_option(
opt_str = c("-t", "--threads"),
dest = "nthreads",
type = "integer",
default = default.nthreads,
help = paste0("number of threads to use, defaults to ", default.nthreads),
metavar = "4"),
make_option(
opt_str = c("-v", "--verbose"),
action = "store_true",
default = default.verbose,
help = "print more output on what's happening")
)
parser <- OptionParser(
usage = "Rscript %prog [options] input output",
option_list = options,
description = "\nan awesome R script",
epilogue = "use with caution, the awesomeness might slap you in the face!"
)
cli <- parse_args(parser, positional_arguments = 2)
# ------------------------------------------------------------------------------
# assign a few shortcuts
# ------------------------------------------------------------------------------
verbose <- cli$options$verbose
input <- cli$args[1]
output <- cli$args[2]
# ------------------------------------------------------------------------------
# actual program
# ------------------------------------------------------------------------------
if (verbose) {
cat("[info] listing options now ...\n")
}
cat(paste0("number of threads: ", cli$options$nthreads, "\n"))
cat(paste0("input: ", input, "\n"))
cat(paste0("output: ", output, "\n"))
if (verbose) {
cat("[info] end of script\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment