argparse: Simple command line argument parser

View source: R/argparse.R

argparseR Documentation

Simple command line argument parser

Description

Simple command line argument parser: returns a named list with arguments and values

Usage

argparse(line = commandArgs(trailingOnly = TRUE), opts = NULL)

Arguments

opts

a list of lists, each of one has tree argument: 'name' (a character eg 'example' is for –example), 'default' (a value if nothing is specified at command line, eg TRUE), 'f' (the coercer function to assure the input is validated and of the proper type)

Examples

## Beginning of an Rscript
#!/usr/bin/Rscript --vanilla --quiet

# No opts, no validation
args <- lbmisc::argparse()

# With opts, validation
opts <- list(
   list(name = 'a_character' , default = "hello" , f = as.character),
   list(name = 'a_logical'   , default = "TRUE"  , f = as.logical),
   list(name = 'an_integer'  , default = "1L"    , f = as.integer),
   list(name = 'a_numeric'   , default = "1.5"   , f = as.numeric)
)
args <- lbmisc::argparse(opts = opts)
## print the effect of validation
print(str(args))

## with from the command line
## rscript --a_character --a_logical FALSE --an_integer 2 --a_numeric


lbraglia/lbmisc documentation built on March 28, 2024, 10:03 a.m.