Skip to content

Instantly share code, notes, and snippets.

@patr1ckm
Created December 11, 2017 18:47
Show Gist options
  • Save patr1ckm/5d997f46f99180ab5910fc1122569783 to your computer and use it in GitHub Desktop.
Save patr1ckm/5d997f46f99180ab5910fc1122569783 to your computer and use it in GitHub Desktop.
docopt.R demo
#!/usr/local/bin/Rscript
'usage: my_prog.R [-a -r -m <msg>]
options:
-a Add
-r Remote
-m <msg> Message' -> doc
library(docopt)
opts <- docopt(doc)
system("touch alog.txt")
prev_msg <- readLines("alog.txt")
where <- if (opts[["-r"]]) "REMOTE: " else "LOCAL: "
msg <- if (!is.null(opts[["-m"]])) opts[["-m"]] else "<empty>"
final_msg <- c(prev_msg, paste0(where, msg))
if (opts[["-a"]]) cat(final_msg, file = "alog.txt", sep="\n", append = FALSE)
cat(final_msg, sep = "\n")
@patr1ckm
Copy link
Author

Example usage:

chmod +x file.R
./file.R 
./file.R -a -m 'hello'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment