diff --git a/DESCRIPTION b/DESCRIPTION index 84ea4cc..98da043 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: argparse Type: Package Title: Command Line Optional and Positional Argument Parser -Version: 2.1.7-2 +Version: 2.2.0-0 Authors@R: c(person("Trevor L", "Davis", role=c("aut", "cre"), email="trevor.l.davis@gmail.com", comment = c(ORCID = "0000-0001-6341-4639")), diff --git a/NEWS.md b/NEWS.md index 4a61f9e..604fb59 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,9 @@ -argparse 2.1.7 +argparse 2.2.0 ============== +* We now support the `format_help()`, `format_usage()`, and `set_defaults()` methods (#43). + Suggestion of @oliverbothe. + * When an error is thrown by `ArgumentParser()$parse_args()` and `interactive()` is `FALSE` and `getOption("error")` is `NULL` then we now use a quieter default error handler that doesn't output a trailing "Execution halted". diff --git a/R/argparse.R b/R/argparse.R index 0ab94c8..aa4b06f 100644 --- a/R/argparse.R +++ b/R/argparse.R @@ -151,6 +151,14 @@ Parser <- R6Class("Parser", # nolint 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") + }, + format_usage = function() { + paste(private$python_code$run(sprintf("%s.print_usage()", private$name)), + collapse = "\n") + }, print_help = function() { cat(private$python_code$run(sprintf("%s.print_help()", private$name)), sep = "\n") invisible(NULL) @@ -159,6 +167,14 @@ Parser <- R6Class("Parser", # nolint cat(private$python_code$run(sprintf("%s.print_usage()", private$name)), sep = "\n") invisible(NULL) }, + get_default = function(...) { + stop("We don't currently support `get_default()`") + }, + set_defaults = function(...) { + private$python_code$append(sprintf("%s.set_defaults(%s)", private$name, + convert_..._to_arguments("add_argument", ...))) + invisible(NULL) + }, add_argument = function(...) { private$python_code$append(sprintf("%s.add_argument(%s)", private$name, convert_..._to_arguments("add_argument", ...))) diff --git a/tests/testthat/test-argparse.R b/tests/testthat/test-argparse.R index 58ca6d7..3128b08 100644 --- a/tests/testthat/test-argparse.R +++ b/tests/testthat/test-argparse.R @@ -26,6 +26,8 @@ test_that("print_help works as expected", { expect_output(parser$print_help(), "optional arguments:|options:") expect_output(parser$print_help(), "Process some integers.") expect_output(parser$print_usage(), "usage:") + expect_true(grepl("Process some integers.", parser$format_help())) + expect_true(grepl("usage:", parser$format_usage())) # Request/bug by PlasmaBinturong parser$add_argument("integers", metavar = "N", type = "integer", nargs = "+", @@ -151,7 +153,15 @@ test_that("parse_known_args() works as expected", { }) +test_that("set_defaults() works as expected", { + skip_if_not(detects_python()) + parser <- ArgumentParser() + parser$set_defaults(bar=42) + args <- parser$parse_args(c()) + expect_equal(args$bar, 42) + # expect_equal(parser$get_default("bar"), 42) +}) test_that("ArgumentParser works as expected", { skip_if_not(detects_python())