Reputation: 4916
I'm developing an R package that includes a command line interface. When building the package, I would like to avoid parsing the command line arguments (build flags) since my command line argument parser doesn't recognize these build-related arguments and produces an error.
To overcome this issue, I'm currently using an approach where my build flags are hard coded to an if statement before trying to parse the arguments:
if (length(commandArgs(trailingOnly = TRUE)) > 0 &&
!(grepl("--no-multiarch", commandArgs(trailingOnly = TRUE)))) {
argv <- GetCmdlineArguments()
DoStuff(argv$parsed.argument)
}
Another approach which I haven't experimented with yet includes putting the argument parsing in a different R file which is ignored by the build via .Rbuildignore
. This would, however, lead to an unfavorable situation where an extra file is needed for each R file that has a command line interface.
Is there more elegant and robust way of detecting if the package is being built instead of actually executed from the command line by the user?
Upvotes: 1
Views: 227
Reputation: 368389
I do not fully understand what you are trying to achieve, but allow me to provide some context:
Rscript
and r
from littler both of which facilitate command-line useinst/scripts/
or inst/examples
Could you not just do the same?
Upvotes: 1