diff --git a/DESCRIPTION b/DESCRIPTION index 9e14ab5..10e9874 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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="trevor.l.davis@gmail.com", comment = c(ORCID = "0000-0001-6341-4639")), diff --git a/NEWS.md b/NEWS.md index 4d98c44..b48b76d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/argparse.R b/R/argparse.R index 8b265e5..28bd2df 100644 --- a/R/argparse.R +++ b/R/argparse.R @@ -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" @@ -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)) { diff --git a/tests/testthat/test-argparse.R b/tests/testthat/test-argparse.R index 05c1be3..ca1e1aa 100644 --- a/tests/testthat/test-argparse.R +++ b/tests/testthat/test-argparse.R @@ -40,10 +40,10 @@ 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", { @@ -51,17 +51,17 @@ test_that("convert_..._to_arguments works as expected", { # 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")