Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap long help text #30

Closed
nh3 opened this issue Apr 4, 2019 · 2 comments
Closed

Wrap long help text #30

nh3 opened this issue Apr 4, 2019 · 2 comments

Comments

@nh3
Copy link

nh3 commented Apr 4, 2019

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
Copy link
Author

nh3 commented Apr 4, 2019

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")
...
...
}

@trevorld
Copy link
Owner

trevorld commented Apr 4, 2019

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) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants