Skip to content

Commit

Permalink
fix: 'add_argument()' 'help' ending in a '"'
Browse files Browse the repository at this point in the history
* Fixes bug when `add_argument()` `help` values ended in a `"`.
  Thanks Oliver Dreschel (@oliverdreschel) for bug report.

closes #46
  • Loading branch information
trevorld committed Feb 15, 2023
1 parent 9bd079b commit 7c0ab75
Show file tree
Hide file tree
Showing 4 changed files with 18 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.2.1
Version: 2.2.2-0
Authors@R: c(person("Trevor L", "Davis", role=c("aut", "cre"),
email="[email protected]",
comment = c(ORCID = "0000-0001-6341-4639")),
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
argparse 2.2.2
==============

* Fixes bug when `add_argument()` `help` values ended in a `"` (#46).
Thanks Oliver Dreschel (@oliverdreschel) for bug report.

argparse 2.2.1
==============

Expand Down
7 changes: 6 additions & 1 deletion 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 <- paste0('"""', argument, '"""')
if (is.character(argument)) argument <- convert_character(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 All @@ -270,6 +270,11 @@ convert_argument <- function(argument, as_list = FALSE) {
argument
}

convert_character <- function(s) {
bool <- substr(s, nchar(s), nchar(s)) == '"'
ifelse(bool, paste0("'''", s, "'''"), paste0('"""', s, '"""'))
}

get_python_type <- function(type, proposed_arguments) {
python_type <- switch(type,
character = "str",
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-argparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ test_that("add_argument works as expected", {
expect_equal(arguments$label, c("a", "b"))
expect_equal(arguments$bool, c(FALSE, TRUE))

# Bug found by Oliver Dreschel (@oliverdreschel)
p <- ArgumentParser()
p$add_argument('--listlab', type='character', help='This is a helpstring,"Containing Quotes"')
expect_equal(p$parse_args()$listlab, NULL)

# Use R casting of logicals
p <- ArgumentParser()
p$add_argument("--bool", type = "logical", action = "store")
Expand Down

0 comments on commit 7c0ab75

Please sign in to comment.