Commit b19226fa authored by Dirk Eddelbuettel's avatar Dirk Eddelbuettel

Import Upstream version 1.20.1

parent c94c1d2b
Package: getopt
Type: Package
Title: C-like getopt behavior.
Version: 1.20.0
Title: C-Like 'getopt' Behavior
Version: 1.20.1
Author: Allen Day.
Contributions from Trevor L Davis.
Maintainer: Trevor L Davis <trevor.l.davis@stanford.edu>
Contributions from Trevor L Davis and Roman Zenka.
Maintainer: Trevor L Davis <trevor.l.davis@gmail.com>
URL: https://github.com/trevorld/getopt
Imports: stats
BugReports: https://github.com/trevorld/getopt/issues
Description: Package designed to be used with Rscript to write
``#!'' shebang scripts that accept short and long flags/options.
......@@ -14,8 +15,8 @@ Description: Package designed to be used with Rscript to write
support for default values, positional argument support, etc.
License: GPL (>= 2)
Suggests: testthat
Collate: 'getopt.R' 'utils.R'
Packaged: 2013-08-30 07:02:26 UTC; trevorld
RoxygenNote: 6.0.1
NeedsCompilation: no
Packaged: 2017-11-29 22:23:04 UTC; trevorld
Repository: CRAN
Date/Publication: 2013-08-30 10:36:29
Date/Publication: 2017-11-29 23:47:17 UTC
b234ee4d69f5fce4486a80fdaf4a4263 *COPYING
08656e23fb8bd07c69f68e7d5443f05a *DESCRIPTION
dd396c987d24860d37a42fc7541e77b5 *NAMESPACE
f651db85028ffd75fd6e59b496a413fe *NEWS
244e1fed9d7aaaffaf66976576c06f9d *R/getopt.R
633c7bb643b66fbcede364fca8c8e538 *DESCRIPTION
d400adb3d8a7edbac587ac399fdeb451 *NAMESPACE
df3d63cace1715e6ff923097ea414c18 *NEWS
8c78d6f8d7d6ca6db34c45529501cb11 *R/getopt.R
e374b85f40f7549cbdb66c9065c1f1ec *R/utils.R
252fbb7071cd1cc555ca81c8f537a6b9 *inst/tests/test-getopt.R
f692f19f2e09013c3d075f6d322473c0 *man/get_Rscript_filename.Rd
7edc52e347b20c53819d16cef5ff8086 *man/getopt.Rd
21469f658334a9ef307e44fcd49a1710 *man/sort_list.Rd
71912d95683c1891b4523acbc554da5e *tests/run-all.R
5a2cda954866c5d7604a71b09cf09cbd *exec/example.R
6f64f9d572b196c7a8b134220425a06a *man/get_Rscript_filename.Rd
c1d20843902006a2d52e480fe841037f *man/getopt.Rd
9ba3ea21bace8ecaa9b3687fa55f7efe *man/sort_list.Rd
35fe6c4707e36c320771156c47cc943f *tests/run-all.R
554652391069d23674fe3c317646b75a *tests/testthat/test-getopt.R
# Generated by roxygen2: do not edit by hand
export(get_Rscript_filename)
export(getopt)
export(sort_list)
import(stats)
getopt 1.20.1
=============
* Now explicitly imports the ``na.omit`` method from the ``stats`` package.
Thanks Derrick Oswald for bug report.
* Improved parsing for negative numbers preceded by a space instead of a '=' sign.
Thanks Roman Zenka for improved regular expression.
* Slightly more informative error message if `storage.mode` coercion results in an `NA`.
Thanks Roman Zenka for suggestion.
getopt 1.20.0
=============
* Type of "numeric" in spec automatically cast to "double".
......
This diff is collapsed.
#!/path/to/Rscript
library('getopt');
library('stats');
#get options, using the spec as defined by the enclosed list.
#we read the options from the default: commandArgs(TRUE).
spec = matrix(c(
'verbose', 'v', 2, "integer",
'help' , 'h', 0, "logical",
'count' , 'c', 1, "integer",
'mean' , 'm', 1, "double",
'sd' , 's', 1, "double"
), byrow=TRUE, ncol=4);
opt = getopt(spec);
# if help was asked for print a friendly message
# and exit with a non-zero error code
if ( !is.null(opt$help) ) {
cat(getopt(spec, usage=TRUE));
q(status=1);
}
#set some reasonable defaults for the options that are needed,
#but were not specified.
if ( is.null(opt$mean ) ) { opt$mean = 0 }
if ( is.null(opt$sd ) ) { opt$sd = 1 }
if ( is.null(opt$count ) ) { opt$count = 10 }
if ( is.null(opt$verbose ) ) { opt$verbose = FALSE }
#print some progress messages to stderr, if requested.
if ( opt$verbose ) { write("writing...",stderr()); }
#do some operation based on user input.
cat(paste(rnorm(opt$count,mean=opt$mean,sd=opt$sd),collapse="\n"));
cat("\n");
#signal success and exit.
q(status=0);
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{get_Rscript_filename}
\alias{get_Rscript_filename}
\title{Returns file name of calling Rscript}
\usage{
get_Rscript_filename()
get_Rscript_filename()
}
\value{
A string with the filename of the calling script. If not
found (i.e. you are in a interactive session) returns NA.
A string with the filename of the calling script.
If not found (i.e. you are in a interactive session) returns NA.
}
\description{
\code{get_Rscript_filename} returns the file name of
calling Rscript
\code{get_Rscript_filename} returns the file name of calling Rscript
}
This diff is collapsed.
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{sort_list}
\alias{sort_list}
\title{Recursively sorts a list}
\usage{
sort_list(unsorted_list)
sort_list(unsorted_list)
}
\arguments{
\item{unsorted_list}{A list.}
\item{unsorted_list}{A list.}
}
\value{
A sorted list.
A sorted list.
}
\description{
\code{sort_list} returns a sorted list
\code{sort_list} returns a sorted list
}
library("testthat")
library("getopt")
test_package("getopt")
test_check("getopt")
......@@ -68,6 +68,42 @@ test_that("numeric is cast to double", {
getopt(spec, c("-c", "-55.0"))
})
test_that("negative numbers are handled correctly", {
# Issue if negative number preceded by space instead of '=' reported by Roman Zenka
spec = matrix(c("count", "c", 1, "integer"), ncol=4, byrow=TRUE)
expect_equal(getopt(spec, c("-c", "5"))$count, 5)
spec = matrix(c("count", "c", 1, "integer"), ncol=4, byrow=TRUE)
expect_equal(getopt(spec, c("-c", "-5"))$count, -5)
spec = matrix(c("count", "c", 1, "integer"), ncol=4, byrow=TRUE)
expect_equal(getopt(spec, c("--count=-1E5"))$count, -1E5)
spec = matrix(c("count", "c", 1, "double"), ncol=4, byrow=TRUE)
expect_equal(getopt(spec, c("-c", "-5"))$count, -5)
spec = matrix(c("count", "c", 1, "double"), ncol=4, byrow=TRUE)
expect_equal(getopt(spec, c("-c", "-1e5"))$count, -1e5)
spec = matrix(c("count", "c", 1, "double"), ncol=4, byrow=TRUE)
expect_equal(getopt(spec, c("--count=-1.2e5"))$count, -1.2e5)
spec = matrix(c("count", "c", 1, "double"), ncol=4, byrow=TRUE)
expect_equal(getopt(spec, c("--count", "-1e5"))$count, -1e5)
})
test_that("more helpful warnings upon incorrect input", {
# Give more pointed warning upon wildly incorrect input
spec = matrix(c("count", "c", 1, "integer"), ncol=4, byrow=TRUE)
expect_warning(getopt(spec, c("-c", "hello"))$count, paste("integer expected, got", dQuote("hello")))
spec = NULL
expect_error(getopt(spec, ""), 'argument "spec" must be non-null.')
spec = c("foo", "f", 0)
expect_error(getopt(spec, ""), 'or a character vector with length divisible by 4, rtfm')
spec = matrix(c("foo", "f", 0, "integer"), ncol=2)
expect_error(getopt(spec, ""), '"spec" should have at least 4 columns.')
spec = matrix(c("foo", "f", 0, "integer", "bar", "b", 0, "integer"), ncol=8)
expect_error(getopt(spec, ""), '"spec" should have no more than 6 columns.')
})
test_that("don't throw error if multiple matches match one argument fully", {
# test if partial name matches fully,
# still throw error if multiple matches and doesn't match both fully
......@@ -85,4 +121,62 @@ context("Test sort_list")
test_that("sort_list works as expected", {
expect_equal(sort_list(list(a = 3, b = 2)), sort_list(list(b = 2, a = 3)))
expect_false(identical(sort_list(list(b = 3, a = 2)), list(b = 3, a = 2)))
expect_false(identical(sort_list(list(b = list(b = 3, c = 2), a = 2)),
list(b = list(c = 2, b = 3), a = 2)))
})
context("Use h flag for non-help")
test_that("Use h flag for non help", {
spec = matrix(c( 'foo' , 'h', 0, "logical"), ncol=4, byrow=TRUE)
expect_equal(getopt(spec, c('-h')), sort_list(list(ARGS=character(0), foo=TRUE)))
spec = matrix(c( 'foo' , 'h', 0, "logical",
'help', 'h', 0, "logical"), ncol=4, byrow=TRUE)
expect_error(getopt(spec, c('-h')), "redundant short names for flags")
spec = matrix(c( 'foo' , 'f', 0, "logical",
'foo', 'h', 0, "logical"), ncol=4, byrow=TRUE)
expect_error(getopt(spec, c('-h')), "redundant long names for flags")
})
context("Optional usage strings")
test_that("Optional usage strings work as expected", {
spec = matrix(c(
'foo' , 'f', 0, "logical", "foo usage",
'foobar' , 'b', 1, "character", "foobar usage",
'biz' , 'z', 2, "logical", "biz usage",
'number' , 'n', 1, "numeric", "number usage",
'help' , 'h', 0, "logical", "help"
), ncol=5, byrow=TRUE)
expect_output(cat(getopt(spec, usage=TRUE)), "foobar usage")
})
context("More tests to get coverage up")
test_that("tests to get coverage up", {
spec = matrix(c(
'foo' , 'f', 0, "logical", "foo usage",
'foobar' , 'b', 1, "character", "foobar usage",
'biz' , 'z', 2, "logical", "biz usage",
'number' , 'n', 1, "numeric", "number usage",
'help' , 'h', 0, "logical", "help"
), ncol=5, byrow=TRUE)
expect_error(getopt(spec, "--whodunit"), 'long flag "whodunit" is invalid')
expect_error(getopt(spec, "--foo=4"), 'long flag "foo" accepts no arguments')
## expect_error(getopt(spec, c("--foo", "4")), 'long flag "foo" accepts no arguments')
expect_equal(getopt(spec, "--biz", "4"), sort_list(list(ARGS=character(0), biz=TRUE)))
expect_warning(getopt(spec, c("-n", "bar")), paste('double expected, got', dQuote("bar")))
expect_warning(getopt(spec, c("--number=bar")), paste('double expected, got', dQuote("bar")))
expect_error(getopt(spec, "-n"), 'flag "n" requires an argument')
expect_error(getopt(spec, "-p"), 'short flag "p" is invalid')
expect_error(getopt(spec, "-nh"), 'short flag "n" requires an argument, but has none')
expect_error(getopt(spec, "-fn"), 'flag "n" requires an argument')
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment