From 7c0ab756fe3911375afddeb20a3ef263dc8fe91c Mon Sep 17 00:00:00 2001 From: Trevor L Davis Date: Wed, 15 Feb 2023 10:03:54 -0800 Subject: [PATCH] fix: 'add_argument()' 'help' ending in a '"' * Fixes bug when `add_argument()` `help` values ended in a `"`. Thanks Oliver Dreschel (@oliverdreschel) for bug report. closes #46 --- DESCRIPTION | 2 +- NEWS.md | 6 ++++++ R/argparse.R | 7 ++++++- tests/testthat/test-argparse.R | 5 +++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0fd4589..55d14e2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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="trevor.l.davis@gmail.com", comment = c(ORCID = "0000-0001-6341-4639")), diff --git a/NEWS.md b/NEWS.md index edb5c72..f08e8cd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 ============== diff --git a/R/argparse.R b/R/argparse.R index 4235894..5696a67 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 <- 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" @@ -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", diff --git a/tests/testthat/test-argparse.R b/tests/testthat/test-argparse.R index ca1e1aa..a04cf04 100644 --- a/tests/testthat/test-argparse.R +++ b/tests/testthat/test-argparse.R @@ -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")