R package providing a command line optional and positional argument parser.
R
Switch branches/tags

README.rst

argparse: Command line optional and positional argument parser.

CRAN Status Badge Build Status Coverage Status

A command line parser to be used with Rscript to write "#!" shebang scripts that gracefully accept positional and optional arguments and automatically generate usage.

To install the development version use the following command:

devtools::install_github("argparse", "trevorld")

dependencies

The package has a Python dependency. It is easily satisfied if you have Python (version 3.2 or higher) on your PATH. Read the INSTALL file for more information if this doesn't describe you.

Additionally this package depends on the R packages proto, findpython, getopt, and rjson.

To run the unit tests you will need the suggested R package testthat and in order to build the vignette you will need the suggested R package knitr which in turn probably requires the system tool pandoc:

sudo apt-get install pandoc

example

> parser <- ArgumentParser(description='Process some integers')
> parser$add_argument('integers', metavar='N', type="integer", nargs='+',
+                    help='an integer for the accumulator')
> parser$add_argument('--sum', dest='accumulate', action='store_const',
+                    const='sum', default='max',
+                    help='sum the integers (default: find the max)')
> parser$print_help()
usage: PROGRAM [-h] [--sum] N [N ...]

Process some integers

positional arguments:
  N           an integer for the accumulator

optional arguments:
  -h, --help  show this help message and exit
  --sum       sum the integers (default: find the max)

Default args for ArgumentParser()$parse_args are commandArgs(TRUE) which is what you'd want for an Rscript but not for interactive use:

> args <- parser$parse_args(c("--sum", "1", "2", "3"))
> accumulate_fn <- get(args$accumulate)
> print(accumulate_fn(args$integers))
[1] 6