Skip to content

vec-last ignored when only 2 elements are provided #681

Description

@JosiahParry

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

Activity

  1. a-maldet commented on May 22, 2024

    @a-maldet

    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

  2. added
    bugan unexpected problem or unintended behavior
    on Jun 20, 2024
  3. gaborcsardi commented on Aug 28, 2024

    @gaborcsardi
    Member

    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.
    
  4. gaborcsardi commented on Aug 28, 2024

    @gaborcsardi
    Member

    Closed by #718.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviortidy-dev-day 🤓Tidyverse Developer Day rstd.io/tidy-dev-day

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions