-
Notifications
You must be signed in to change notification settings - Fork 70
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
Suggestion: cli_abort_if_not()
#672
Comments
I agree. I would love to see something like this made available in Here for example is some cli I wrote today that migrates off of if (!rlang::inherits_any(x, "data.frame")) {
cli::cli_abort(
"Expected {.cls data.frame} found {.obj_type_friendly {x}}",
call = call
)
} else if (!rlang::is_scalar_character(name)) {
cli::cli_abort(
"{.arg name} must be a scalar character vector.",
call = call
)
} else if (!rlang::is_scalar_character(title)) {
cli::cli_abort(
"{.arg title} must be a scalar character vector.",
call = call
)
} else if (!nzchar(name)) {
cli::cli_abort(
"{.arg name} must not be empty.",
call = call
)
} else if (!nzchar(title)) {
cli::cli_abort(
"{.arg name} must not be empty.",
call = call
)
} If a cli_stopifnot(
"Expected {.cls data.frame} found {class(x)}" = rlang::inherits_any(x, "data.frame"),
"{.arg name} must be a scalar character vector." = rlang::is_scalar_character(name),
"{.arg title} must be a scalar character vector." = rlang::is_scalar_character(title),
"{.arg name} must not be empty." = nzchar(name),
"{.arg name} must not be empty." = nzchar(title),
call = call
) |
Worth noting that rlang has an open (albeit fairly old) issue about whether to implement something like this. |
This is a good idea, and we might have something like this at some point, but not in the short term, unfortunately. |
If I'm using cli in a package, often I avoid
stopifnot()
because the unformatted error messages feel jarring compared with those produced bycli_abort()
and friends. I'm guessing cli has nostopifnot()
equivalent because {rlang} doesn't have one either, but I would really like to see something like this added as I often just end up implementing it myself:Created on 2024-02-21 with reprex v2.0.2
Many thanks for all the work on this amazing package!
The text was updated successfully, but these errors were encountered: