trevorld/r-optparse

Convert hyphens to underscores.

Closed this issue · 5 comments

audy commented

Just a suggestion.

Python's argparse automatically converts hyphens to underscores.

I think this behavior would suit optparse because column names containing hyphens can't be subsetted the same way as underscores:

library(optparse)

option_list <-  c(make_option('--some-option'))

parser <- OptionParser(option_list = option_list)

options <- parse_args(parser)

print(options$some-option) # crashes

print(options$[,'some-option']) # ugly

print(options$some_option) # nice

What do you think? I could write the code and send you a PR if you agree.

Well, you can still do:

print(options$`some-option`)

Although that is still a little awkward. A quick look online shows that optparse users are defining options like --some-option so I'm hesitant to make a change that breaks their scripts since I've marked optparse as a stable API since late 2012.

audy commented

If only R had versioned dependencies.

You could make it backwords compatible by adding the hyphenated option. But that could still break things.

Yeah, not sure if it is worth implementing at this point since it could break people's programs and too minor of a change to justify creating a optparse2 package. Would have been a great idea 4-5 years ago.

NB. the argparse package already does this (although it has a Python dependency):

> library("argparse")
Loading required package: proto
> parser = ArgumentParser()
> parser$add_argument("--test-arg")  
> parser$parse_args(c("--test-arg", "what"))
$test_arg
[1] "what"

Too bad this won't get fixed, I'll go with the workaround then: print(options$some-option)

In the developmental version of optparse I've added an option convert_hyphens_to_underscores to parse_args which does what you want. I've also added the convenience warpper parse_args2 which wraps parse_args setting convert_hyphens_to_underscores to TRUE and positional_arguments to TRUE.