Skip to content

Parses cli inline markup and glue substitutions in text without evaluating the embedded R expressions, returning the symbol names referenced inside {...} interpolation blocks via base::all.vars().

Usage

cli_extract_vars(text, functions = TRUE, unique = TRUE)

Arguments

text

Character vector of cli format strings.

functions

Logical scalar passed to base::all.vars(): whether to include function names in addition to variable names. Defaults to TRUE.

unique

Logical scalar passed to base::all.vars(): whether to deduplicate extracted symbol names. Defaults to TRUE.

Value

Character vector of extracted symbol names.

Details

Static analysis tools (such as lintr::object_usage_linter()) can use cli_extract_vars() to discover variables and functions referenced in cli calls. Malformed cli strings or R expressions signal an error of class "cli_error" (with subclasses "cli_glue_error", "cli_style_error", "cli_parse_error", and "cli_pluralization_error"), so static analysis callers should wrap cli_extract_vars() in base::tryCatch().

If text is a named character vector (for example, as passed to cli_dl(), cli_li(), cli_bullets(), or cli_abort()), both names(text) and text are inspected.

Examples

cli_extract_vars(
  "{.arg x} has {length(x)} elements, but it should be {.emph {length(y)}}"
)
#> [1] "length" "x"      "y"     
cli_extract_vars(
  "{.arg x} has {length(x)} elements, but it should be {.emph {length(y)}}",
  functions = FALSE
)
#> [1] "x" "y"