parse_args: Parse command line options.

Description Usage Arguments Value Acknowledgement Author(s) References See Also Examples

Description

parse_args parses command line options using an OptionParser instance for guidance.

Usage

1
2
3
4
  parse_args(object,
    args = commandArgs(trailingOnly = TRUE),
    print_help_and_exit = TRUE,
    positional_arguments = FALSE)

Arguments

object

An OptionParser instance.

args

A character vector containing command line options to be parsed. Default is everything after the Rscript program in the command line. If positional_arguments is TRUE then parse_args will only look for positional arguments at the end of this vector.

print_help_and_exit

Whether parse_args should call print_help to print out a usage message and exit the program. Default is TRUE.

positional_arguments

Whether parse_args should look for and return a character vector of positional arguments. Default is FALSE.

Value

Returns a list containing option values if positional_arguments is FALSE (the default). Otherwise returns a list with field options containing our option values as well as another field args which contains a vector of positional arguments.

Acknowledgement

A big thanks to Steve Lianoglou for a bug report and patch; Juan Carlos Borrás for a bug report; Jim Nikelski for a bug report and patch; Ino de Brujin and Benjamin Tyner for a bug report; Jonas Zimmermann for bug report; Miroslav Posta for bug reports.

Author(s)

Trevor Davis.

References

Python's optparse library, which inspired this package, is described here: http://docs.python.org/library/optparse.html

See Also

OptionParser print_help

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
27
28
# example from vignette
option_list <- list(
   make_option(c("-v", "--verbose"), action="store_true", default=TRUE,
       help="Print extra output [default]"),
   make_option(c("-q", "--quietly"), action="store_false",
       dest="verbose", help="Print little output"),
   make_option(c("-c", "--count"), type="integer", default=5,
       help="Number of random normals to generate [default %default]",
       metavar="number"),
   make_option("--generator", default="rnorm",
       help = "Function to generate random deviates [default \"%default\"]"),
   make_option("--mean", default=0,
       help="Mean if generator == \"rnorm\" [default %default]"),
   make_option("--sd", default=1, metavar="standard deviation",
       help="Standard deviation if generator == \"rnorm\" [default %default]")
   )
parse_args(OptionParser(option_list = option_list), args = c("--sd=3", "--quietly"))

# example from vignette using positional arguments
option_list2 <- list(
   make_option(c("-n", "--add_numbers"), action="store_true", default=FALSE,
       help="Print line number at the beginning of each line [default]")
   )
parser <- OptionParser(usage = "%prog [options] file", option_list=option_list2)

parse_args(parser, args = c("--add_numbers", "example.txt"), positional_arguments = TRUE)

parse_args(parser, args = c("-add_numbers", "example.txt"), positional_arguments = TRUE)

Example output

$verbose
[1] FALSE

$count
[1] 5

$generator
[1] "rnorm"

$mean
[1] 0

$sd
[1] 3

$help
[1] FALSE

$options
$options$add_numbers
[1] TRUE

$options$help
[1] FALSE


$args
[1] "example.txt"

$options
$options$add_numbers
[1] FALSE

$options$help
[1] FALSE


$args
[1] "-add_numbers" "example.txt" 

optparse documentation built on May 2, 2019, 6:27 p.m.