find_args: Locate the position of arguments passed from commandline

Description Usage Arguments Value Note Examples

Description

This function is intended to find the position of commandline arguments. Under usual circumstances this is called from other functions but has been exported for the user. It is intentionally restricted to only take 2 possible argument indicators, but does not in itself restrict the format of those argument. These restrictions should be checked before calling the function. The check itself is performed in optimized c++ code.

Usage

1
find_args(args = commandArgs(TRUE), sarg, larg)

Arguments

args

vector of commandline arguments. Defaults to commandArgs(TRUE)

sarg

short argument indicator (one letter arguments). No default

larg

long argument indicator (one or more letter).

Value

An integer vector indicating the positions that started with either sarg or larg. If both are provided the integer indicates larg over sarg to avoid any confusion between "-" and "–" (or equivalent). The vector contains an attribute 'argLen' indicating the end position for the argument indicator.

Note

The function checks whether the argument indicator is followed by an empty space and throws an error if this is the case. This follows the output syntax from commandArg, which cannot have "spaces" in arguments unless quoted.

The function does not check for other types of invalid arguments (duplicates, invalid symbol names etc.). These checks should be caught with pattern matching.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Simple argument finding
find_args(c('--abcd', '-d', 'hello', '--ff'), '-', '--')
find_args(c('--abcd', '-d', 'hello', '--ff'), '-')
find_args(c('--abcd', '-d', 'hello', '--ff'), '--')
# Example with help
find_args('-h', '-', '--')
find_args('--help', '-', '--')
# Note that this does not find help:
find_args('--help', '-')
# Example with alternative indicator
find_args('/h', , '/')

## Not run: 
# Example of an error:
find_args(c('-- ', '-d', 'hello', '--ff'), '-', '--')

## End(Not run)

Bijaelo/cmdline.arguments documentation built on June 12, 2021, 1:42 p.m.