From 1a20610d6cbb23290204e31409306a08936afd52 Mon Sep 17 00:00:00 2001 From: Trevor L Davis Date: Mon, 18 Jul 2022 13:32:30 -0700 Subject: [PATCH] fix: Quieter default non-interactive error handler * When an error is thrown by `ArgumentParser()$parse_args()` and `interactive()` is `FALSE` and `getOption("error")` is `NULL` then we now use a quieter default error handler that doesn't output a trailing "Execution halted". follow-up on #40 --- DESCRIPTION | 2 +- NEWS.md | 7 +++++++ R/argparse.R | 7 ++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 013e579..fc9c4bd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: argparse Type: Package Title: Command Line Optional and Positional Argument Parser -Version: 2.1.6 +Version: 2.1.7-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 6994a85..3a98bc9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +argparse 2.1.7 +============== + +* When an error is thrown by `ArgumentParser()$parse_args()` and `interactive()` is `FALSE` + and `getOption("error")` is `NULL` then + we now use a quieter default error handler that doesn't output a trailing "Execution halted". + argparse 2.1.6 ============== diff --git a/R/argparse.R b/R/argparse.R index 37cec44..f18379e 100644 --- a/R/argparse.R +++ b/R/argparse.R @@ -358,6 +358,10 @@ find_python_cmd <- function(python_cmd = NULL) { python_cmd } +quieter_error_handler <- function(e) { + quit('no', status = 1, runLast = FALSE) +} + pa_stop <- function(message, r_note) { msg <- paste(c(r_note, message), collapse = "\n") cnd <- errorCondition(msg, @@ -368,7 +372,8 @@ pa_stop <- function(message, r_note) { } else { signalCondition(cnd) cat(message, sep = "\n", file = stderr()) - opt <- options(show.error.messages = FALSE) + opt <- options(error = getOption("error", quieter_error_handler), + show.error.messages = FALSE) on.exit(options(opt)) stop(cnd) }