Suggestion: `cli_abort_if_not()`

Open
#672 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
cli

Research direction

Start by reading the existing cli_abort behavior and comparing it with R's stopifnot() and rlang's related facilities. The issue names no repository files or tests, so locate the current error helpers first, then verify the requested equivalent against the provided cli_abort_if_not() examples and their formatted messages.

Written by the indexing model from the issue text.

Description

feature

If I'm using cli in a package, often I avoid stopifnot() because the unformatted error messages feel jarring compared with those produced by cli_abort() and friends. I'm guessing cli has no stopifnot() 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:

cli_abort_if_not <- function(..., .call = .envir, .envir = parent.frame(), .frame = .envir) {
  for (i in seq_len(...length())) {
    if (!all(...elt(i))) {
      cli::cli_abort(
        ...names()[i], 
        .call = .call, 
        .envir = .envir, 
        .frame = .frame
      )
    }
  }
  invisible(NULL)
}

cli_abort_if_not(
  "No problem" = TRUE,
  "Some {.emph important} issue" = FALSE,
  "Another issue" = FALSE
)
#> Error:
#> ! Some important issue
#> Backtrace:
#>     ▆
#>  1. └─global cli_abort_if_not(...)
#>  2.   └─cli::cli_abort(...)
#>  3.     └─rlang::abort(...)

Created on 2024-02-21 with reprex v2.0.2

Many thanks for all the work on this amazing package!

Dominant language
R
Stars
726
Forks
94
Avg merge
3h 35m
Merged PRs (30d)
1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from r-lib/cli

All issues in r-lib/cli

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.