Suggest alternative name by minimizing Levenshtein edit distance between valid and invalid arguments

cmd_help_flags_similar(
  command_flag_names,
  flags,
  .fun = NULL,
  distance_cutoff = 3L
)

Arguments

command_flag_names

character vector of valid names (can be output of cmd_help_parse_flags)

flags

a vector names correspond to values to be checked against command_flag_names

.fun

optional function to apply to command_flag_names and flags before checking their values. If using a function to rename flags after cmd_list_interp, use that same function here. Can be useful for parsing help lines into R-friendly variable names for user-convenience. Can be function or rlang-style formula definition (ie .fun = ~{foo(.x)} is the same as .fun = function(x){foo(x)}). Note: if command_flag_names need additional parsing after cmd_help_parse_flags, it is best to do that preprocessing before passing them to this function.

distance_cutoff

Levenshtein edit distance beyond which to suggest ??? instead of most similar argument (default = 3). Setting this too liberally will result in nonsensical suggestions.

Value

named vector where names are names from flags and their values are the suggested best match from command_flag_names

Examples

# with a flagsList, need to pass names() flagsList <- list("output" = "somevalue", "missplld" = "anotherValue") cmd_help_flags_similar(c("output", "misspelled"), names(flagsList))
#> missplld #> "misspelled"
command_flags <- c("long-flag-name") flags <- c("long_flag_naee") cmd_help_flags_similar(command_flags, flags, .fun = ~{gsub("-", "_", .x)})
#> long_flag_naee #> "long_flag_name"
# returns NULL if no errors cmd_help_flags_similar(c("test"), "test")
#> NULL