MatchArg: Argument Matching with Default Fallback

View source: R/19_MatchArg.R

MatchArgR Documentation

Argument Matching with Default Fallback

Description

A robust argument matching function that supports exact matching, partial matching, and provides sensible defaults when no match is found. This is an internal utility function not intended for direct use by package users.

Usage

MatchArg(arg, choices, default = choices[1L], call = rlang::caller_env(), ...)

Arguments

arg

The argument to match against choices. Can be NULL or a character vector.

choices

A character vector of valid choices to match against.

default

The default value to return if no match is found and arg is NULL. Defaults to the first element of choices.

call

caller env

...

No usage

Details

This function provides a more flexible alternative to base::match.arg() with the following matching strategy:

  1. If arg is NULL, returns the default value

  2. Attempts exact matching using base::match()

  3. Falls back to partial matching using base::pmatch()

  4. If no match found and default is not NULL, returns default

  5. Otherwise, throws an informative error with valid choices

The function uses rlang::caller_env() for accurate error reporting in the context where the function was called.

Value

Returns the matched choice from the choices vector. If no match is found and arg is NULL, returns the default value. If no match is found and arg is not NULL, throws an informative error.

See Also

match for exact matching \ pmatch for partial matching \ caller_env for calling environment context

Examples

## Not run: 
# Internal usage examples
MatchArg("app", c("apple", "banana", "application"))  # Returns "apple"
MatchArg(NULL, c("red", "green", "blue"))             # Returns "red" (default)
MatchArg("gr", c("red", "green", "blue"))             # Returns "green"

# Would error: MatchArg("invalid", c("valid1", "valid2"))

## End(Not run)


SigBridgeRUtils documentation built on Sept. 29, 2026, 5:09 p.m.