Skip to content

Commit

Permalink
feat: 'set_defaults()', 'format_help()', and 'format_usage()'
Browse files Browse the repository at this point in the history
* We now support the `format_help()`, `format_usage()`, and `set_defaults()` methods (#43).
  Suggestion of @oliverbothe.

closes #43
  • Loading branch information
trevorld committed Aug 5, 2022
1 parent 17681ad commit 5a50940
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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="[email protected]",
comment = c(ORCID = "0000-0001-6341-4639")),
Expand Down
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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".
Expand Down
16 changes: 16 additions & 0 deletions R/argparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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", ...)))
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-argparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "+",
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 5a50940

Please sign in to comment.