From 6b3e70c29612961b1ba85050978de854c01c7107 Mon Sep 17 00:00:00 2001 From: Trevor L Davis Date: Thu, 25 Nov 2021 09:32:03 -0800 Subject: [PATCH] fix: Extraneous error message in usage output in Python 3.10 * Fixes extraneous error message in usage output when using Python 3.10. Python 3.10's `argparse` usage messages were tweaked in a way that weren't fully backwards compatible with earlier Python versions. closes #36 --- DESCRIPTION | 2 +- NEWS.md | 7 +++++++ R/argparse.R | 2 +- tests/testthat/test-argparse.R | 7 +++---- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4f2c1de..4f8a165 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: argparse Type: Package Title: Command Line Optional and Positional Argument Parser -Version: 2.1.2 +Version: 2.1.3 Authors@R: c(person("Trevor L", "Davis", role=c("aut", "cre"), email="trevor.l.davis@gmail.com"), person("Allen", "Day", role="ctb", comment="Some documentation and examples ported from the getopt package."), person("Python Software Foundation", role="ctb", comment="Some documentation from the optparse Python module."), diff --git a/NEWS.md b/NEWS.md index fc73e5e..2e05123 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +argparse 2.1.3 +============== + +* Prevents extraneous error message from being included in usage message + when using Python 3.10 (#36). + Thanks @dariober for bug report. + argparse 2.1.2 ============== diff --git a/R/argparse.R b/R/argparse.R index 0c898d6..9bf23a2 100644 --- a/R/argparse.R +++ b/R/argparse.R @@ -190,7 +190,7 @@ Parser <- R6Class("Parser", # nolint parse_args_output <- function(output) { if (grepl("^usage:", output[1])) { has_positional_arguments <- any(grepl("^positional arguments:", output)) - has_optional_arguments <- any(grepl("^optional arguments:", output)) + has_optional_arguments <- any(grepl("^optional arguments:|^options:", output)) if (has_positional_arguments || has_optional_arguments) { .print_message_and_exit(output, "help requested:") } else { diff --git a/tests/testthat/test-argparse.R b/tests/testthat/test-argparse.R index 12454ab..a2ca5be 100644 --- a/tests/testthat/test-argparse.R +++ b/tests/testthat/test-argparse.R @@ -26,7 +26,7 @@ test_that("print_help works as expected", { skip_if_not(detects_python()) parser <- ArgumentParser(description = "Process some integers.") expect_output(parser$print_help(), "usage:") - expect_output(parser$print_help(), "optional arguments:") + expect_output(parser$print_help(), "optional arguments:|options:") expect_output(parser$print_help(), "Process some integers.") expect_output(parser$print_usage(), "usage:") @@ -324,7 +324,6 @@ test_that("Paths that quit()", { help <- system2(cmd, c("scripts/test_help.R", "--help"), stdout = TRUE, stderr = TRUE) - expect_equal(help, - c("usage: scripts/test_help.R [-h]", "", "optional arguments:", - " -h, --help show this help message and exit")) + expect_equal("usage: scripts/test_help.R [-h]", help[2]) + expect_equal(" -h, --help show this help message and exit", help[5]) })