---
title: "Argument Matching"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Argument Matching}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```


# Ignoring case


## `base`

The base function `match.arg()` is good, but it doesn't offer the possiblity to ignore case during argument matching. Sometimes it's good to ignore case; for example, if you're matching the arguments `c("yes", "no")`, there's no need to worry about case. 

```{r base-ignore-case, error=TRUE}
base::match.arg("Y", c("yes", "no"))
```


## `strex`

The default behaviour of `strex::match_arg()` is to observe case, but case ignorance can be turned on with `ignore_case = TRUE`.
    
```{r strex-ignore-case, error=TRUE}
strex::match_arg("Y", c("yes", "no"))
strex::match_arg("Y", c("yes", "no"), ignore_case = TRUE)
```


# Error Messages

You can begin to see above that the error message from `strex::match_arg()` are more informative and nicely formatted. Here are a few more examples.


## No matches 

```{r no-matches, error=TRUE}
choices <- c("Apples", "Pears", "Bananas", "Oranges", "Avocados", "Apricots")
match.arg("Q", choices)
strex::match_arg("Q", choices)
```


## Multiple matches

```{r multiple-matches, error=TRUE}
match.arg("A", choices)
strex::match_arg("A", choices)
```


## Wrong `arg` length

```{r arg-too-long, error=TRUE}
match.arg(c("A", "a"), choices)
strex::match_arg(c("A", "a"), choices)
```


## Duplicate elements in `choices`
```{r choices-duplicate, error=TRUE}
choices <- c(choices, "Pears")
match.arg("P", choices)
strex::match_arg("P", choices)
```



