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

Adding support for actions #22

Open
Tezirg opened this issue May 2, 2017 · 2 comments
Open

Adding support for actions #22

Tezirg opened this issue May 2, 2017 · 2 comments

Comments

@Tezirg
Copy link

Tezirg commented May 2, 2017

In the python version some other actions are supported for options. https://docs.python.org/3/library/optparse.html#defining-options
I'm thinking of the "append" and "count" options that are missing in this library but are often used in opt args parsing.

Is it possible to add support for these ?

@trevorld
Copy link
Owner

trevorld commented May 2, 2017

I probably won't take the time to add an append and count action since the practical use cases seem kind of rare and this is the first time in seven years someone has written to me about them. What kind of use case did you have in mind?

If a patch were submitted which doesn't break any of package unit tests then I would likely accept one. Note that optparse is currently a wrapper of the R package getopt (which I didn't write but now maintain) which currently throws an error for options specified multiple times).

Please note that if you don't mind a Python dependency there is also an R package argparse that is a wrapper of that Python package. In the python world optparse for several years has been deprecated in favor of argparse.

@Tezirg
Copy link
Author

Tezirg commented May 3, 2017

Thank you for the reply. I ended up using the argparse package as you suggested. It does support these additional actions.
I also went to try and add the support into this project, but It requires a patch in both optparse and getopt. Note that it doesn't throw when repeating a flag, it just overides with the last matched value.

Here is what I did using argparse so you can see a use case:

main <- function(ac, av) {
  parser <- ArgumentParser(description='R wrapper script')
  parser$add_argument("-i", "--input", dest="inputs", type="character", action="append", help="Input file")
  parser$add_argument("-s", "--script", dest="script", type="character", required=TRUE, help="User script file to run")
  parser$add_argument("-p", "--params", dest="params", type="character", default='{ "isDefault": true }', help="Configuration parameters in JSON format")
  args <- parser$parse_args(av);

  #Parse JSON input to native R data structure
  args$params <- fromJSON(args$params);
  #Import input script file
  source(args$script);
  #Call user script
  userJob(args$params, args$inputs);
}

#Getting command line arguments
cmdArgs <- commandArgs(trailingOnly = TRUE);
##Passing that to a c-like main function
main(42, c("-s", "template.R", "-i", "test1.csv", "-i", "test2.csv"));

Here is what a unit test for this could look like:

# Test extension for action support
context("Extended action parameters")
test_that("Make_option action='append'", {
    option_list3 <- list(
            make_option(c("-c", "--char"), type="character", action="append", help="Test flag or the character type"),
            make_option(c("-i", "--int"), type="integer", action="append", help="Test flag or the integer type")
    )
    parser <- OptionParser(usage = "%prog [options]", option_list = option_list3)
    args <- parse_args(parser, args = c("--char", "A", "-c", "B", "-c", "C", "-i", 42, "--int", 666))
    
    print("parsed")
    print(args)
    
    #Testing number of values in lists
    expect_equal(length(args$int), 2);
    expect_equal(length(args$char), 3);
    #Testing some values
    expect_equal(args$int[[1]], 42);
    expect_equal(args$char[[1]], "B");
})

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