Skip to content

Commit

Permalink
feat: 'parse_intermixed_args()' and 'parse_known_intermixed_args()'
Browse files Browse the repository at this point in the history
* We now support the following `ArgumentParser()` methods:

  * `parse_intermixed_args()` (#45)
  * `parse_known_intermixed_args()` (#45)

closes #45
  • Loading branch information
trevorld committed Aug 5, 2022
1 parent 5a50940 commit 7cfe646
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Authors@R: c(person("Trevor L", "Davis", role=c("aut", "cre"),
email="[email protected]",
comment = c(ORCID = "0000-0001-6341-4639")),
person("Allen", "Day", role="ctb", comment="Some documentation and examples ported from the getopt package."),
person("Python Software Foundation", role="ctb", comment="Some documentation from the optparse Python module."),
person("Python Software Foundation", role="ctb", comment="Some documentation from the argparse Python module."),
person("Paul", "Newell", role="ctb"))
Description: A command line parser to
be used with Rscript to write "#!" shebang scripts that gracefully
Expand Down
9 changes: 7 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
argparse 2.2.0
==============

* We now support the `format_help()`, `format_usage()`, and `set_defaults()` methods (#43).
Suggestion of @oliverbothe.
* We now support the following `ArgumentParser()` methods:

* `format_help()`
* `format_usage()`
* `parse_intermixed_args()` (#45)
* `parse_known_intermixed_args()` (#45)
* `set_defaults()` (#43). Suggestion of @oliverbothe.

* When an error is thrown by `ArgumentParser()$parse_args()` and `interactive()` is `FALSE`
and `getOption("error")` is `NULL` then
Expand Down
14 changes: 14 additions & 0 deletions R/argparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,27 @@ Parser <- R6Class("Parser", # nolint
output <- private$python_code$run(new_code)
parse_args_output(output)
},
parse_intermixed_args = function(args = commandArgs(TRUE)) {
new_code <- c(sprintf("args = %s.parse_intermixed_args([%s])", private$name,
paste(sprintf("'%s'", args), collapse = ", ")),
"print(json.dumps(args.__dict__, sort_keys=True))")
output <- private$python_code$run(new_code)
parse_args_output(output)
},
parse_known_args = function(args = commandArgs(TRUE)) {
new_code <- c(sprintf("args_remainder = %s.parse_known_args([%s])", private$name,
paste(sprintf("'%s'", args), collapse = ", ")),
"print(json.dumps((args_remainder[0].__dict__, args_remainder[1])))")
output <- private$python_code$run(new_code)
parse_args_output(output)
},
parse_known_intermixed_args = function(args = commandArgs(TRUE)) {
new_code <- c(sprintf("args_remainder = %s.parse_known_intermixed_args([%s])", private$name,
paste(sprintf("'%s'", args), collapse = ", ")),
"print(json.dumps((args_remainder[0].__dict__, args_remainder[1])))")
output <- private$python_code$run(new_code)
parse_args_output(output)
},
format_help = function() {
paste(private$python_code$run(sprintf("%s.print_help()", private$name)),
collapse = "\n")
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-argparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ test_that("parse_known_args() works as expected", {

})

test_that("parse_intermixed_args() works as expected", {
skip_if_not(detects_python())
parser <- ArgumentParser()
parser$add_argument('--foo')
parser$add_argument('cmd')
parser$add_argument('rest', nargs='*', type='integer')
args <- strsplit('doit 1 --foo bar 2 3', ' ')[[1]]
args <- parser$parse_intermixed_args(args)
expect_equal(args$cmd, 'doit')
expect_equal(args$foo, 'bar')
expect_equal(args$rest, 1:3)

args <- strsplit('doit 1 --foo bar 2 3 -n 4', ' ')[[1]]
a_r <- parser$parse_known_intermixed_args(args)
expect_equal(a_r[[1]]$cmd, 'doit')
expect_equal(a_r[[1]]$foo, 'bar')
expect_equal(a_r[[1]]$rest, 1:3)
expect_equal(a_r[[2]], c('-n', '4'))
})

test_that("set_defaults() works as expected", {
skip_if_not(detects_python())
parser <- ArgumentParser()
Expand Down

0 comments on commit 7cfe646

Please sign in to comment.