trevorld/r-optparse

Wrap long help text

nh3 opened this issue · 2 comments

nh3 commented

Would it be possible to make the following changes to wrap long help text?

r-optparse/R/optparse.R

Lines 338 to 339 in 26b6627

cat("\n\t\t")
cat(sub("%default", .as_string(option@default), option@help))

cat('\n')
cat(paste(strwrap(sub("%default", .as_string(option@default), option@help), prefix='\t\t'), collapse='\n'))

Of course, one can supply pre-formatted text to help, but it seems laborious to call a formatter for every option compared with this change of two lines of code here?

nh3 commented

Alternatively, perhaps do something like this to achieve both flexibility and backward compatibility?

default_formatter <- function(text) {
    paste0("\t\t", text)
}
...
...
parse_args <- function(object, args = commandArgs(trailingOnly = TRUE), 
                    print_help_and_exit = TRUE, positional_arguments = FALSE,
                    convert_hyphens_to_underscores = FALSE,
                    help_formatter=default_formatter) {
...
...
           print_help(object, help_formatter)
...
...
}
...
...
print_help <- function(object, formatter) {
...
...
        cat("\n")
        cat(formatter(sub("%default", .as_string(option@default), option@help)))
        cat("\n\n")
...
...
}

Looking at Python's optparse documentation it'd be more idiomatic to make it an argument to OptionParser and have the default function (to format all the help) be called IndentedHelpFormatter) (but putting the new formatter argument at the end so as to not break reverse compatibility):

OptionParser <- function(usage = "usage: %prog [options]", option_list=list(),
                            add_help_option=TRUE, prog=NULL, 
                            description="", epilogue="", formatter=IndentedHelpFormatter) {
    ....
} 
print_help <- function(object) { object@formatter(object) }