Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve arg_check0 error with one permitted value #1635

Open
aronatkins opened this issue Jun 22, 2023 · 2 comments · May be fixed by #1733
Open

improve arg_check0 error with one permitted value #1635

aronatkins opened this issue Jun 22, 2023 · 2 comments · May be fixed by #1733

Comments

@aronatkins
Copy link

arg_check* could produce better feedback when there is only one allowed argument value.

Today: `chosen` must be one of "banana", not "apple".
Suggestion: `chosen` must be "banana", not "apple".

doit <- function() {
  chosen <- "apple"
  available <- c("banana")
  rlang::arg_match(chosen, available)
}
doit()
#> Error in `doit()`:
#> ! `chosen` must be one of "banana", not "apple".
#> Backtrace:
#>     ▆
#>  1. ├─global doit()
#>  2. │ └─rlang::arg_match(chosen, available)
#>  3. │   └─rlang::arg_match0(arg, values, error_arg, error_call = error_call)
#>  4. └─rlang:::stop_arg_match(w, values = x, error_arg = y, error_call = z)
#>  5.   └─rlang::abort(msg, call = error_call, arg = error_arg)

Created on 2023-06-22 with reprex v2.0.2

@aronatkins aronatkins changed the title improve arg_check0 error with one improve arg_check0 error with one permitted value Jun 22, 2023
@olivroy olivroy linked a pull request Jul 11, 2024 that will close this issue
@fh-mthomson
Copy link

Perhaps a duplicate of the same underlying issue?

test_fun <- function(one_allowed = c("1", "2", "3"), multiple = FALSE) {
  rlang::arg_match(arg = one_allowed, multiple = multiple)
}

# bug: should give clear error that only one value is allowed
test_fun(c("1", "2"))
#> Error in `test_fun()`:
#> ! `one_allowed` must be one of "1", "2", or "3", not "1".

# correct:
test_fun("1")
#> [1] "1"
test_fun("2")
#> [1] "2"
test_fun(c("1", "2"), multiple = TRUE)
#> [1] "1" "2"

Created on 2024-11-25 with reprex v2.1.1

@fh-mthomson
Copy link

Oddly, if you pass the entire set of values, it passes? 🤔

test_fun <- function(one_allowed = c("1", "2", "3"), multiple = FALSE) {
  rlang::arg_match(arg = one_allowed, multiple = multiple)
}

test_fun(c("1", "2", "3"))
#> [1] "1"

Created on 2024-11-25 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants