cli icon indicating copy to clipboard operation
cli copied to clipboard

`vec-last` ignored when only 2 elements are provided

Open JosiahParry opened this issue 11 months ago • 1 comments

cli_vec() is very helpful when informing uses that they should choose one of a set of values. Using vec-last is great because it allows us to say or {last-item} meaning it is one of the options not all of them. However, when only two elements are provided cli_vec() always says "and".

Can it be fixed such that vec-last is used when the vector length is > 1?

v <- cli::cli_vec(
  c("foo", "bar", "foobar"),
  style = list("vec-last" = ", or ")
)

cli::cli_text("Must be one of: {v}.")
#> Must be one of: foo, bar, or foobar.

v <- cli::cli_vec(
  c("foo", "bar"),
  style = list("vec-last" = " or ")
)

cli::cli_text("Must be one of: {v}.")
#> Must be one of: foo and bar.

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

JosiahParry avatar Mar 21 '24 14:03 JosiahParry

Hi! I would also appreciate a fix for this problem. Thanks for the great package and all the work you are putting in.

Kind regards Adrian

a-maldet avatar May 22 '24 09:05 a-maldet

Fixing this is probably a breaking change.

There is also a workaround already:

v <- cli::cli_vec(
  c("foo", "bar"),
  style = list("vec-last" = " or ", "vec-sep2" = " or ")
)
cli::cli_text("Must be one of: {v}.")
Must be one of: foo or bar.

which is also appropriate if you want Oxford comma:

v <- cli::cli_vec(
  c("foo", "bar"),
  style = list("vec-last" = ", or ", "vec-sep2" = " or ")
)
cli::cli_text("Must be one of: {v}.")

Another workaround is to use the .or class:

v <- c("foo", "bar")
cli::cli_text("Must be one of: {.or {v}}.")
Must be one of: foo or bar.

gaborcsardi avatar Aug 28 '24 10:08 gaborcsardi

Closed by #718.

gaborcsardi avatar Aug 28 '24 10:08 gaborcsardi