Learn R Programming

argparser (version 0.7.2)

parse_args: Parse arguments with a parser.

Description

This function uses an arg.parser object to parse command line arguments or a character vector.

Usage

parse_args(parser, argv = NULL)

Value

a list with argument values

Arguments

parser

an arg.parser object

argv

a character vector to parse (arguments and values should already be split by whitespace); if NULL, values will be obtained from argv if argv exists in the global scope, or from commandArgs(trailingOnly=TRUE).

Examples

Run this code
p <- arg_parser('pi')
p <- add_argument(p, "--digits",
  help="number of significant digits to print", default=7)

if (FALSE) {
# If arguments are passed from the command line,
# then we would use the following:
argv <- parse_args(p)
}

# For testing purposes, we can pass a character vector:
argv <- parse_args(p, c("-d", "30"))

# Now, the script runs based on the passed arguments
digits <- if (argv$digits > 22) 22 else argv$digits
print(pi, digits=digits)

if (FALSE) {
# We can also save an argument list for later use
saveRDS(argv, "arguments.rds")

# To use the saved arguments, use the --opts argument at the command line
#$ ./script.R --opts arguments.rds
} 

Run the code above in your browser using DataLab