Skip to content

Commit

Permalink
fix: Squish 'description' argument
Browse files Browse the repository at this point in the history
* `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
  • Loading branch information
trevorld committed Aug 4, 2022
1 parent 10dbb46 commit 17681ad
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
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-1
Version: 2.1.7-2
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: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
==============
Expand Down
8 changes: 8 additions & 0 deletions R/argparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-argparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 17681ad

Please sign in to comment.