parse_args: Parse arguments with a parser.

Description Usage Arguments Value Examples

View source: R/argparser.R

Description

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

Usage

1
parse_args(parser, argv = commandArgs(trailingOnly = TRUE))

Arguments

parser

an arg.parser object

argv

a character vector to parse (arguments and values should already be split by whitespace)

Value

a list with argument values

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
p <- arg_parser('pi')
p <- add_argument(p, "--digits",
  help="number of significant digits to print", default=7)

## Not run: 
# If arguments are passed from the command line,
# then we would use the following:
argv <- parse_args(p)

## End(Not run)

# 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)

## Not run: 
# 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

## End(Not run) 

argparser documentation built on March 8, 2021, 9:07 a.m.