Add an argument to a parser.

Share:

Description

This function adds an argument to an arg.parser object and returns the modified object.

Usage

1
2
add_argument(parser, arg, help, default = NULL, type = NULL, nargs = NULL,
  flag = NULL, short = NULL)

Arguments

parser

an arg.parser object

arg

argument name (use no prefix for positional arguments, -- or - prefix for optional arguments or flags)

help

help description for the argument

default

default value for the argument [default: NA]

type

variable type of the argument (which can be inferred from default); assumed to be character otherwise

nargs

number of argument values (which can be inferred from default); set to Inf for an indefinite number; an optional argument with an indefinite number of values may need to be followed by another optional argument or flag (e.g. --) to separate the indefinite optional argument from possible position arguments

flag

whether argument is a flag (and does not consume a value) [default: FALSE]

short

short-form for flags and positional arguments; short-forms can be assigned automatically based on the first character of the argument name, unless a conflict arises with an existing short-form; to avoid conflicts, add the argument as early as possible

Details

This function supports multiple arguments in a vector. To ensure that the argument variable type is set correctly, either specify type directly or supply default argument values as a list. Argument names that contain dash - in the stem are converted to _.

Value

an arg.parser object with the argument added

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
p <- arg_parser("A text file modifying program")

# Add a positional argument
p <- add_argument(p, "input", help="input file")

# Add an optional argument
p <- add_argument(p, "--output", help="output file", default="output.txt")

# Add a flag
p <- add_argument(p, "--append", help="append to file", flag=TRUE)

# Add multiple arguments together
p <- add_argument(p,
    c("ref", "--date", "--sort"),
    help = c("reference file", "date stamp to use", "sort lines"),
    flag = c(FALSE, FALSE, TRUE))

# Print the help message
print(p)