Skip to content

Commit

Permalink
fix: Descriptions/epilogs with newlines in them
Browse files Browse the repository at this point in the history
fixes #44
  • Loading branch information
trevorld committed Aug 5, 2022
1 parent 7cfe646 commit f22b70c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 18 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.2.0-0
Version: 2.2.0-2
Authors@R: c(person("Trevor L", "Davis", role=c("aut", "cre"),
email="[email protected]",
comment = c(ORCID = "0000-0001-6341-4639")),
Expand Down
3 changes: 1 addition & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ argparse 2.2.0
* `add_argument()` now allows "numeric" as an alias for "double" for the `type` argument (#42).
Suggestion of @dariober.

* `ArgumentParser()` will now quietly "squish" its `description` argument
(which in some cases avoids an error) (#44).
* `ArgumentParser()` now handles `description` arguments with newlines in them (#44).
Thanks Arthur Gilly (@agilly) for bug report.

argparse 2.1.6
Expand Down
10 changes: 1 addition & 9 deletions R/argparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ parse_args_output <- function(output) {

# @param argument argument to be converted from R to Python
convert_argument <- function(argument, as_list = FALSE) {
if (is.character(argument)) argument <- shQuote(argument, type = "sh")
if (is.character(argument)) argument <- paste0('"""', argument, '"""')
if (is.numeric(argument)) argument <- as.character(argument)
if (is.logical(argument)) argument <- ifelse(argument, "True", "False")
if (is.null(argument)) argument <- "None"
Expand Down Expand Up @@ -323,14 +323,6 @@ convert_..._to_arguments <- function(mode, ...) { # nolint
formatter_class <- argument_list[[ii]]
proposed_arguments[ii] <- sprintf("formatter_class=%s", formatter_class)
}
# Convert whitespace to single space in description
if (mode == "ArgumentParser" && any(grepl("description=", proposed_arguments))) {
ii <- grep("description=", proposed_arguments)
description <- argument_list[[ii]]
description <- trimws(gsub("\\s+", " ", description))
description <- shQuote(description)
proposed_arguments[ii] <- sprintf("description=%s", description)
}
# Set right default prog name if not specified, if possible
# Do last to not screw up other fixes with prog insertion
if (mode == "ArgumentParser" && needs_prog(argument_names)) {
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-argparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,28 @@ test_that("print_help works as expected", {

test_that("convert_argument works as expected", {
skip_if_not(detects_python())
expect_equal(convert_argument("foobar"), "'foobar'")
expect_equal(convert_argument("foobar"), '"""foobar"""')
expect_equal(convert_argument(14.9), "14.9")
expect_equal(convert_argument(c(12.1, 14.9)), "(12.1, 14.9)")
expect_equal(convert_argument(c("a", "b")), "('a', 'b')")
expect_equal(convert_argument(c("a", "b")), '("""a""", """b""")')
})

test_that("convert_..._to_arguments works as expected", {
skip_if_not(detects_python())
# test in mode "add_argument"
c.2a <- function(...) convert_..._to_arguments("add_argument", ...)
waz <- "wazzup"
expect_equal(c.2a(foo = "bar", hello = "world"), "foo='bar', hello='world'")
expect_equal(c.2a(foo = "bar", waz), "foo='bar', 'wazzup'")
expect_equal(c.2a(foo = "bar", hello = "world"), 'foo="""bar""", hello="""world"""')
expect_equal(c.2a(foo = "bar", waz), 'foo="""bar""", """wazzup"""')
expect_equal(c.2a(type = "character"), "type=str")
expect_equal(c.2a(default = TRUE), "default=True")
expect_equal(c.2a(default = 3.4), "default=3.4")
expect_equal(c.2a(default = "foo"), "default='foo'")
expect_equal(c.2a(default = "foo"), 'default="""foo"""')
# test in mode "ArgumentParser"
c.2a <- function(...) convert_..._to_arguments("ArgumentParser", ...)
expect_match(c.2a(argument_default = FALSE), "argument_default=False")
expect_match(c.2a(argument_default = 30), "argument_default=30")
expect_match(c.2a(argument_default = "foobar"), "argument_default='foobar'")
expect_match(c.2a(argument_default = "foobar"), 'argument_default="""foobar"""')
expect_match(c.2a(foo = "bar"), "^prog='PROGRAM'|^prog='test-argparse.R'")
expect_match(c.2a(formatter_class = "argparse.ArgumentDefaultsHelpFormatter"),
"formatter_class=argparse.ArgumentDefaultsHelpFormatter")
Expand Down

0 comments on commit f22b70c

Please sign in to comment.