From 7cfe646468dbe81b78c9dae3f889d0184bb0454e Mon Sep 17 00:00:00 2001 From: Trevor L Davis Date: Thu, 4 Aug 2022 23:01:23 -0700 Subject: [PATCH] feat: 'parse_intermixed_args()' and 'parse_known_intermixed_args()' * We now support the following `ArgumentParser()` methods: * `parse_intermixed_args()` (#45) * `parse_known_intermixed_args()` (#45) closes #45 --- DESCRIPTION | 2 +- NEWS.md | 9 +++++++-- R/argparse.R | 14 ++++++++++++++ tests/testthat/test-argparse.R | 20 ++++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 98da043..9e14ab5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,7 +6,7 @@ Authors@R: c(person("Trevor L", "Davis", role=c("aut", "cre"), email="trevor.l.davis@gmail.com", 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 diff --git a/NEWS.md b/NEWS.md index 604fb59..4d98c44 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/argparse.R b/R/argparse.R index aa4b06f..8b265e5 100644 --- a/R/argparse.R +++ b/R/argparse.R @@ -144,6 +144,13 @@ 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 = ", ")), @@ -151,6 +158,13 @@ Parser <- R6Class("Parser", # nolint 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") diff --git a/tests/testthat/test-argparse.R b/tests/testthat/test-argparse.R index 3128b08..05c1be3 100644 --- a/tests/testthat/test-argparse.R +++ b/tests/testthat/test-argparse.R @@ -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()