ArgumentParser {argparse}R Documentation

Create a command line parser

Description

ArgumentParser crates a parser object that acts as a wrapper to Python's argparse module

Usage

ArgumentParser(..., python_cmd = getOption("python_cmd",
  find_python_cmd(required_modules = c("argparse", "json | simplejson"))))

Arguments

...

Arguments cleaned and passed to Pythons argparse.ArgumentParser()

python_cmd

The python executable for argparse to use. Must have argparse and json modules (i.e. Python (>= 2.7)). Default is python

Value

ArgumentParser returns a parser object which contains an add_argument function to add arguments to the parser, a parse_args function to parse command line arguments into a list, a print_help and print_usage function to print usage information. See code examples, package vignette, and corresponding python module for more information on how to use it.

Acknowledgement

A big thanks to Martin Diehl for a bug report.

References

Python's argparse library, which this package is based on, is described here: http://docs.python.org/library/argparse.html

Examples


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

[Package argparse version 1.0.4 Index]