Open utterances-bot opened 9 months ago
I'm just starting with cli, so this post is excellent! I have two questions:
Can you explain why you need rlang
as a dependency if you use cli_abort()
? What happens if you don't add it. Also, what is the guidance on cli_abort()
? Do you wrap it in an if condition to test an assertion?
Any advise on how to wrap long CLI text to 80 chars in your code (styler guideline), but not in the console. I notice paste()
works (and respects the glue syntax):
cli::cli_alert(paste(
"This {.val text} is longer than 80 characters in my code, so I wrap it with",
"{.fun paste}."
))
:wave: @peterdesmet! Thanks for your comment. Cc @drmowinckels
You get an error (unless rlang has been installed via some other dependencies). cli_abort()
unconditionally calls rlang::abort()
https://github.com/r-lib/cli/blob/e7a62defc0442afba235042d29b3e608d5345831/R/rlang.R#L45
Not sure what best practice is but it also works with c()
cli::cli_alert(c(
"This {.val text} is longer than 80 characters in my code, so I wrap it with",
"{.fun paste}."
))
#> → This "text" is longer than 80 characters in my code, so I wrap it with`paste()`.
Created on 2023-12-22 with reprex v2.0.2
Do you wrap it in an if condition to test an assertion?
I'm not sure I understand the question. Does https://www.njtierney.com/post/2023/12/06/long-errors-smell/ help at all?
if
to test assertions is likely better explained here. Thanks for the Code Smell article, will read that.c()
works in cli_alert()
, it generates multiple lines in e.g. cli_abort()
. So paste()
to the rescue:library(cli)
cli::cli_abort(c(
"This {.val text} is longer than 80 characters in my code, so I wrap it with",
"{.fun paste}."
))
#> Error:
#> ! This "text" is longer than 80 characters in my code, so I wrap it with
#> `paste()`.
#> Backtrace:
#> ▆
#> 1. └─cli::cli_abort(...)
#> 2. └─rlang::abort(...) at cli/R/rlang.R:45:3
cli::cli_abort(paste(
"This {.val text} is longer than 80 characters in my code, so I wrap it with",
"{.fun paste}."
))
#> Error:
#> ! This "text" is longer than 80 characters in my code, so I wrap it with
#> `paste()`.
#> Backtrace:
#> ▆
#> 1. └─cli::cli_abort(...)
#> 2. └─rlang::abort(...) at cli/R/rlang.R:45:3
Created on 2023-12-22 with reprex v2.0.2
@peterdesmet you can also write a multi-line string instead of paste()
, line breaks are ignored:
cli::cli_alert(
"This {.val text} is longer than 80 characters in my code, so I just write it in
two lines, no need to use {.fun paste}."
)
My question regarding using if to test assertions is likely better explained https://github.com/frictionlessdata/frictionless-r/issues/163#issuecomment-1865897243. Thanks for the Code Smell article, will read that.
Ah yes then the blog post will be relevant for sure as it discusses the creation of helper functions to check arguments.
Also I see https://vctrs.r-lib.org/reference/vec_assert.html but it has a "questioning" lifecycle badge.
Cliff notes about the cli package - R-hub blog
https://blog.r-hub.io/2023/11/30/cliff-notes-about-cli/