jeremystan / aargh

Easily Expose R Functions to Command Line Arguments
Other
48 stars 2 forks source link

Print the R help from the function if --help is called #2

Open jeremystan opened 7 years ago

jeremystan commented 7 years ago

Is there some way to get the help for an R function as a string and pass that to argparse? Or alternatively redirect --help flags to just use ?f?

Dasonk commented 7 years ago

I'm not at a computer I can easily test this but if you just want to get the help from a function as text you can use tools::Rd2txt to create a text file from the Rd files in a package. That's basically what happens when your help_type is set to 'text' with a bit of additional magic to use an appropriate pager to display the file. I'm guessing the issue. Although that might be beyond what you care about - I was assuming you'd want more control over what gets printed but if you just want to allow the output to be displayed in the console you could use a similar trick that I use to display help files in html vignettes in my docstring package. If you do this:

console_pager <- function(x, ...){
    input <- readLines(x)
    # Had some issues with _ getting displayed
    # in the output console output which
    # messed up rendering in the created html vignette
    # So remove that before outputting.
    input <- gsub("_", "", input)
    cat(paste(input,collapse="\n"), "\n")}
options(pager=console_pager)

then having the help file display directly in a console works just fine.

It seems to me that one might want to write their own documentation if using aargh though since you might be doing something that isn't just a direct call to a built in function. My docstring package might be useful to use in conjunction with aargh in those cases. If that sounds like something you think users might want to do and have trouble getting it working I'd be more than happy to help with that.