-
Notifications
You must be signed in to change notification settings - Fork 11
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
Comments
I probably won't take the time to add an If a patch were submitted which doesn't break any of package unit tests then I would likely accept one. Note that Please note that if you don't mind a Python dependency there is also an R package |
Thank you for the reply. I ended up using the argparse package as you suggested. It does support these additional actions. 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");
}) |
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 ?
The text was updated successfully, but these errors were encountered: