From 17681ad4ceffe8ce6a1ab7c8dc370d12adf386ac Mon Sep 17 00:00:00 2001 From: Trevor L Davis Date: Thu, 4 Aug 2022 15:15:49 -0700 Subject: [PATCH] fix: Squish 'description' argument * `ArgumentParser()` will now quietly "squish" its `description` argument (which in some cases avoids an error) (#44). Thanks Arthur Gilly (@agilly) for bug report. closes #44 --- DESCRIPTION | 2 +- NEWS.md | 5 +++++ R/argparse.R | 8 ++++++++ tests/testthat/test-argparse.R | 4 ++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 898ca27..84ea4cc 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-1 +Version: 2.1.7-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 8a9dd2a..4a61f9e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,11 @@ argparse 2.1.7 we now use a quieter default error handler that doesn't output a trailing "Execution halted". * `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). + Thanks Arthur Gilly (@agilly) for bug report. argparse 2.1.6 ============== diff --git a/R/argparse.R b/R/argparse.R index 8e29e7a..0ab94c8 100644 --- a/R/argparse.R +++ b/R/argparse.R @@ -293,6 +293,14 @@ 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 0c6f2da..58ca6d7 100644 --- a/tests/testthat/test-argparse.R +++ b/tests/testthat/test-argparse.R @@ -205,6 +205,10 @@ test_that("parse_args() works as expected", { expect_equal(args$character, "1") expect_equal(args$numeric, 1.0) + # bug reported by Arthur Gilly + parser <- ArgumentParser(description="Description of tool.\nAuthor information.") + expect_true(is.list(parser$parse_args())) + # Bug found by Taylor Pospisil skip_on_os("windows") # Didn't work on Github Actions Windows skip_on_cran() # Once gave an error on win-builder