argparser/0000755000176200001440000000000015147243711012245 5ustar liggesusersargparser/tests/0000755000176200001440000000000014311037545013405 5ustar liggesusersargparser/tests/testthat/0000755000176200001440000000000014426404023015241 5ustar liggesusersargparser/tests/testthat/test_vector.R0000644000176200001440000000106714311037545017735 0ustar liggesusersparser <- arg_parser("MWE") parser <- add_argument(parser, arg = c("--method", "--grid-length", "--problem-type"), help = c("arg1.","arg2.", "arg3."), default = list(NA, 400, "search"), type = c("character", "integer", "character"), nargs = c(1, 1, 1), flag = c(FALSE, FALSE, FALSE), short = c("-m", "-l", "-t") ) test_that("argument vector is parsed correctly", { argv <- parse_args(parser, c("-m", "novel", "-l", "400", "-t", "binary")) expect_equal(argv$method, "novel") expect_equal(argv$grid_length, 400) expect_equal(argv$problem_type, "binary") }) argparser/tests/testthat/test_missing.R0000644000176200001440000000227314311037545020104 0ustar liggesusersp <- arg_parser("Test") p <- add_argument(p, "--arg1", help = "arg1", type = "numeric") p <- add_argument(p, "--arg2", help = "arg2", type = "integer") test_that("numerics are parsed correctly", { argv <- parse_args(p) expect_true(is.na(argv$arg1)) expect_true(is.na(argv$arg2)) expect_equal(parse_args(p, c("--arg1", "1e-9"))$arg1, 1e-9) expect_equal(parse_args(p, c("--arg1", "1.5e9"))$arg1, 1.5e9) expect_equal(parse_args(p, c("--arg1", "-1234.5"))$arg1, -1234.5) expect_error(parse_args(p, c("--arg1", "A"))) }) test_that("integers are parsed correctly", { expect_equal(parse_args(p, c("--arg2", "100"))$arg2, 100) expect_equal(parse_args(p, c("--arg2", "-99"))$arg2, -99) expect_error(parse_args(p, c("--arg2", "A"))) expect_error(parse_args(p, c("--arg2", "4.3"))) expect_error(parse_args(p, c("--arg2", "1.0e-9"))) }) test_that("missing arguments are accepted", { argv <- parse_args(p, c("--arg1", "1.5", "--arg2", "2")) expect_equal(argv$arg1, 1.5) expect_equal(argv$arg2, 2) argv <- parse_args(p, c("--arg1", "3")) expect_equal(argv$arg1, 3) expect_true(is.na(argv$arg2)) argv <- parse_args(p, c("--arg2", "4")) expect_equal(argv$arg2, 4) expect_true(is.na(argv$arg1)) }) argparser/tests/testthat/test_s4.R0000644000176200001440000000151714311037545016761 0ustar liggesusers# Define greeting type with a defined set of valid values setClass("greeting_type") setMethod("coerce", c(from = "ANY", to = "greeting_type"), function(from, to) { if(!from %in% c("hello", "hey", "hi")) { stop("Invalid type value for greeting_type: ", from) } from } ) p <- arg_parser("parser with S4 type") p <- add_argument(p, arg = "--number", help = "a number", type = "numeric") p <- add_argument(p, arg = "--greeting", help = "hello | hey | hi", type = "greeting_type") test_that("parser with S4 type parses correctly", { expect_equal( parse_args(p, c("--number", "4.2"))$number, 4.2 ) expect_match( parse_args(p, c("--greeting", "hello"))$greeting, "hello" ) expect_match( parse_args(p, c("--greeting", "hey"))$greeting, "hey" ) expect_error( parse_args(p, c("--greeting", "bye")) ) }) argparser/MD50000644000176200001440000000276615147243711012570 0ustar liggesusers06fb0fc526404a67e53d9987b56fe435 *COPYING 9753d20e6e25d0cad4374d84ce311d95 *DESCRIPTION b867be11946058fcafd23846b5080924 *NAMESPACE 62e4b4d5d1d2825f577b1edd31abf1f6 *R/argparser-package.R a9b3246fde6d16e10c0423f3f89b8a12 *R/argparser.R 22669c0361417c9ed9a9dad7e32e82e9 *R/deprecated.R d879d2597f222c26198cdfe89fccad81 *R/include.R 7add22662d64a5b510cffa72bb1e47b8 *R/zzz.R 1272f6d8437ded653f988e6bd353769b *README.md dae39cc819468b45a04c48b04a5a5a60 *demo/00Index 9f2b395f86e0c52022eed568d8c3aea1 *demo/prime.R 11aa36e4f2a99258e486c59842b161de *demo/round-pipe.R 094bacf41b901f3db5a0447e7431fa93 *demo/round.R 5ffb115ef817709348902b11783a8166 *demo/sum.R fc86a0532ef47705ebc5b394e9b3b8af *demo/sum2.R 382c64afc4b2eb4c162e6f228820d3d9 *demo/underscore.R 9d23bc7902b27a709d46599393f5ca3b *man/add.argument.Rd 07370e7d04289a6d6791e2d91b9f93a9 *man/add_argument.Rd 04168fb0bec5fae748b9682bd8830d16 *man/arg.parser.Rd e1dd48b4ce0cd5d307b4c80bfd163c92 *man/arg_parser.Rd 0926b0a7262d70c1bff6a3f7c39cd63e *man/argparser-package.Rd ff3e98c31c14d016589da393b41fede6 *man/include.Rd d865590c6b641465d592467c132483a4 *man/parse.args.Rd 472a6139731573d80174819254b0a1d5 *man/parse_args.Rd da1020b217e08decbf55973f37d822e7 *man/print.arg.parser.Rd 59176950381a36eb5c98236da2426088 *man/show_arg_labels.Rd 1f5d0ccf9ba7a0e08e6d4543c0ae4ffb *man/spaces.Rd abc5b0cf58fc5d3da452530a4645146c *tests/testthat/test_missing.R 26935b0b237d0e5111bb9c7a343be001 *tests/testthat/test_s4.R f316c5b1f8c9cf1712f20f221dff1088 *tests/testthat/test_vector.R argparser/R/0000755000176200001440000000000014603267655012457 5ustar liggesusersargparser/R/include.R0000644000176200001440000000043214311037545014211 0ustar liggesusers#' Include R script file #' #' Include R script with behaviour similar to C++ \code{#include #' "header.h"}, by searching in the directory where the #' current script file resides. #' #' @param file name #' @export include <- function(file) { source(file, local=parent.frame()) } argparser/R/deprecated.R0000644000176200001440000000443214311037545014672 0ustar liggesusers#' Create an argument parser. #' #' This function is deprecated. Use \code{arg_parser} instead. #' #' @param description description of the program #' @param name name of the program #' @return a new \code{arg.parser} object #' @export arg.parser <- function(description, name=NULL) { warning("Use arg_parser() instead: arg.parser() is deprecated and will be removed in argparser-0.4."); arg_parser(description, name) } #' Add an argument to a parser. #' #' This function is deprecated. Use \code{add_argument} instead. #' #' @param parser an \code{arg.parser} object #' @param arg argument name (use no prefix for positional arguments, #' \code{--} or \code{-} prefix for optional arguments or flags) #' @param help help description for the argument #' @param default default value for the argument [default: NA] #' @param type variable type of the argument (which can be inferred from #' \code{default}), assumed to be \code{character} otherwise #' @param flag whether argument is a flag (and does not consume a value) #' [default: FALSE] #' @param short short-form for flags and positional arguments; #' short-forms can be assigned automatically based on the first #' character of the argument name, unless a conflict arises with #' an existing short-form; to avoid conflicts, add the argument #' as early as possible #' @return an \code{arg.parser} object with the argument added #' @export add.argument <- function( parser, arg, help, default=NULL, type=NULL, flag=NULL, short=NULL ) { warning("Use add_argument() instead: add.argument() is deprecated and will be removed in argparser-0.4."); add_argument(parser, arg, help, default, type, flag, short) } #' Parse arguments with a parser. #' #' This function is deprecated. Use \code{parse_args} instead. #' #' @param parser an \code{arg.parser} object #' @param argv a character vector to parse (arguments and values should #' already be split by whitespace) #' @return a list with argument values #' @export parse.args <- function(parser, argv=commandArgs(trailingOnly=TRUE)) { warning("Use parse_args() instead: parse.args() is deprecated and will be removed in argparser-0.4."); parse_args(parser, argv) } argparser/R/zzz.R0000644000176200001440000000011014311037545013414 0ustar liggesusers.onLoad <- function(libname, pkgname) { options(argparser.delim=",") } argparser/R/argparser-package.R0000644000176200001440000000115114603267655016157 0ustar liggesusers#' Command-line argument parser #' #' \code{argparser} provides functions for parsing command-line arguments. #' #' To use the parser, #' \enumerate{ #' \item create an \code{arg.parser} object with \code{\link{arg_parser}}; #' \item add arguments to the parser with \code{\link{add_argument}}; #' \item call \code{\link{parse_args}} to parse the command line arguments. #' } #' To execute the script, invoke \code{Rscript}. #' Alternatively on Linux, insert a shebang on the first line #' (\code{#!/usr/bin/env Rscript}) and \code{chmod +x} the script, #' #' @keywords internal "_PACKAGE" #' @import methods NULL argparser/R/argparser.R0000644000176200001440000005450214603266601014564 0ustar liggesusers#' Create an argument parser. #' #' This function creates an \code{arg.parser} object. It infers the program #' name from the file name of the invoked script. #' #' The argument parser will be created by default with two arguments: #' \code{--help} and \code{--opts}. The latter argument can be used for #' loading a list of argument values that are saved in a RDS file. #' #' @param description description of the program #' @param name name of the program #' @param hide.opts hide the \code{--opts} argument #' @return a new \code{arg.parser} object #' @export #' #' @examples #' p <- arg_parser("A test program") #' arg_parser <- function(description, name=NULL, hide.opts=FALSE) { # set default name if (is.null(name)) { # extract file name from command arguments, which will be empty # if the program is not invoked as a script prefix <- "--file="; name <- basename(sub(prefix, "", grep(paste(prefix, "(.+)", sep=""), commandArgs(), value=TRUE) )); } if (length(name) == 0) name <- "