R/cli.r

Defines functions cli.call

cli.call = function(fun, doc, args = commandArgs(TRUE)) {
  fun = get(fun, mode = "function", envir = getNamespace("rtcl"))

  tryCatch({
    x = docopt::docopt(doc, args = args, strict = TRUE, quoted_args = TRUE)
  }, error = function(e) {
    cat(e$message, "\n", file = stderr())
    quit(save = "no", status = 1L)
  })

  x = x[!vlapply(x, is.null)]
  names(x) = sub(x = names(x), pattern = "--", replacement = "", fixed = TRUE)
  names(x) = gsub(x = names(x), pattern = "-", replacement = ".", fixed = TRUE)
  names(x) = gsub(x = names(x), pattern = "[()<>\\[\\]]", replacement = "", perl = TRUE)

  tryCatch({
    do.call(fun, x)
  }, error = function(e) {
    cat(crayon::red(sprintf("Error: %s\n", e$message)), file = stderr())
    quit(save = "no", status = 2L)
  })

  quit(save = "no", status = 0L)
}
rdatsci/rt documentation built on Dec. 6, 2020, 5:31 p.m.