Skip to content

Instantly share code, notes, and snippets.

@gyli
Created September 19, 2014 01:21
Show Gist options
  • Save gyli/277143a29e40b2d6e86a to your computer and use it in GitHub Desktop.
Save gyli/277143a29e40b2d6e86a to your computer and use it in GitHub Desktop.
Input arguments (flags like) through command line to R
#Rscript filename.R --host= --dbname= --user= --password=
options<-paste0("--",c("host","dbname","user","password"))
args <- commandArgs(TRUE)
if(length(args)!=4)stop("Please input the full options of host, dbname, user and password.")
db.options<-function(option){
option.location <- grepl(option,args)
if(sum(option.location)!=1)
stop(paste0("Something wrong with ", option, "."))
if(!grepl("=",args[option.location]))
stop(paste0("Value needed in ", option, "."))
gsub(paste0("^",option,"="),"",args[option.location])
}
config<-vector()
for(option in options){
config[gsub("^--","",option)]<-db.options(option)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment