Say I'm developing an R package, and have a function 'add', documented with Roxygen comments
#' Simple Addition
#'
#' Casts arguments to numeric, adds them together, then prints results
#'
#' @param a first number
#' @param b second number
#'
#' @return sum of a and b
#' @export
#'
#' @examples
#' add(2+2) # returns 4
add <- function(a, b){
a <- as.numeric(a)
b <- as.numeric(b)
return(a+b)
}
What is the easiest way to create a commandline interface (with sensible usage / help messages) for this function. Preferably these commandline interfaces would be distributed in the same R package as the functions they wrap.
Current Solution
I've played around with both optparse and docopt and think both are really great. My current solution is to create a separate CLI scripts/functions that calls the non-CLI functions (i store these in an inst/ dir so distributed with package). The big downside is I end up duplicating a lot of text documenting the CLI wrapper. For the above example, I might make a docopt as follows:
library(docopt)
library(package_containing_add_function)
'Simple Addition
Description:
Casts arguments to numeric, adds them together, then prints results
Usage:
add <a> <b>
Returns:
sum of a and b
Example:
add 2 2 --> returns 4
' -> doc
arguments <- docopt(doc)
add(as.numeric(arguments$a), as.numeric(arguments$b))
To do this for lots of functions, especially as the number of arguments increases, is time consuming and tricky to maintain (there are 2 sets of nearly identical text to maintain in order to get good help messages). Is there a more efficient way to create commandline bindings from documented package functions where I do less duplication of documentation for but get similar results (useful help/usage messages)?