Commit dd291e0d authored by Dirk Eddelbuettel's avatar Dirk Eddelbuettel

Import Upstream version 1.20.3

parent 7313f011
Package: getopt
Type: Package
Title: C-Like 'getopt' Behavior
Version: 1.20.2
Author: Allen Day.
Contributions from Trevor L Davis and Roman Zenka.
Maintainer: Trevor L Davis <trevor.l.davis@gmail.com>
URL: https://github.com/trevorld/getopt
Version: 1.20.3
Authors@R: c(person("Trevor L", "Davis", role=c("aut", "cre"), email="trevor.l.davis@gmail.com"),
person("Allen", "Day", role="aut", comment="Original package author"),
person("Roman", "Zenka", role="ctb"))
URL: https://github.com/trevorld/r-getopt
Imports: stats
BugReports: https://github.com/trevorld/getopt/issues
BugReports: https://github.com/trevorld/r-getopt/issues
Description: Package designed to be used with Rscript to write
``#!'' shebang scripts that accept short and long flags/options.
Many users will prefer using instead the packages optparse or argparse
which add extra features like automatically generated help option and usage,
support for default values, positional argument support, etc.
License: GPL (>= 2)
Suggests: testthat
RoxygenNote: 6.0.1
Suggests: covr, testthat
RoxygenNote: 6.1.1
NeedsCompilation: no
Packaged: 2018-02-16 04:46:12 UTC; trevorld
Packaged: 2019-03-22 19:27:49 UTC; trevorld
Author: Trevor L Davis [aut, cre],
Allen Day [aut] (Original package author),
Roman Zenka [ctb]
Maintainer: Trevor L Davis <trevor.l.davis@gmail.com>
Repository: CRAN
Date/Publication: 2018-02-16 11:18:58 UTC
Date/Publication: 2019-03-22 20:10:03 UTC
b234ee4d69f5fce4486a80fdaf4a4263 *COPYING
a2538666cbeb4f1a012a9ccbe7e430cb *DESCRIPTION
4fa994bf12089b1576a146f57637af98 *DESCRIPTION
d400adb3d8a7edbac587ac399fdeb451 *NAMESPACE
83dbf2fb9391e80d1210814704413cae *NEWS
7a0ae3a4fa75f47310e5837f4c996d7a *R/getopt.R
207afc894101f9cf06056ff778fc5e0a *NEWS.md
a40a1a79c4d4770d9232c30a7951a07e *R/getopt.R
5121860755a766ac2c250aa5909680bf *R/utils.R
5a2cda954866c5d7604a71b09cf09cbd *exec/example.R
d4b58fcd686375f5884c3bb8d7428d32 *README.md
74d919a70588c23dcd41197ec49cd73f *exec/example.R
6f64f9d572b196c7a8b134220425a06a *man/get_Rscript_filename.Rd
c1d20843902006a2d52e480fe841037f *man/getopt.Rd
69fda93b3a4098d70c5994d809b1dc5a *man/getopt.Rd
9ba3ea21bace8ecaa9b3687fa55f7efe *man/sort_list.Rd
35fe6c4707e36c320771156c47cc943f *tests/run-all.R
b4854c3eb3288028b222bf3bb54c1b6e *tests/testthat/test-getopt.R
5d60af18504ba5aaf38be3d049eb4a7d *tests/testthat/test-getopt.R
getopt 1.20.3
=============
* Now by default ``getopt`` won't override a user specified ``opt`` argument if ``argv`` is in the global environment.
Will continue to use ``argv`` as a default for ``opt`` if it is in the global environment and the user does not specify an ``opt`` argument (for ``littler`` compatibility).
getopt 1.20.2
=============
* Now allows one to pass an empty string to a character option.
Thanks Matthew Flickinger for bug report.
getopt 1.20.1
=============
......
# Copyright (c) 2008-2010 Allen Day
# Copyright (c) 2011-2015, 2017-2018 Trevor L. Davis <trevor.l.davis@gmail.com>
# Copyright (c) 2011-2018 Trevor L. Davis <trevor.l.davis@gmail.com>
#
# This file is free software: you may copy, redistribute and/or modify it
# under the terms of the GNU General Public License as published by the
......@@ -123,7 +123,9 @@
#' The terms \emph{option}, \emph{flag}, \emph{long flag}, \emph{short flag},
#' and \emph{argument} have very specific meanings in the context of this
#' document. Read the ``Description'' section for definitions.
#' @param opt This defaults to the return value of \link{commandArgs}(TRUE).
#' @param opt This defaults to the return value of \link{commandArgs}(TRUE) unless
#' \code{argv} is in the global environment in which case it uses that instead
#' (this is for compatibility with littler).
#'
#' If R was invoked directly via the ``R'' command, this corresponds to all
#' arguments passed to R after the ``--args'' flag.
......@@ -145,8 +147,8 @@
#'
#' #!/path/to/Rscript
#' library('getopt')
#' #get options, using the spec as defined by the enclosed list.
#' #we read the options from the default: commandArgs(TRUE).
#' # 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",
......@@ -163,29 +165,33 @@
#' q(status=1)
#' }
#'
#' #set some reasonable defaults for the options that are needed,
#' #but were not specified.
#' # 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.
#' # print some progress messages to stderr, if requested.
#' if ( opt$verbose ) { write("writing...",stderr()) }
#'
#' #do some operation based on user input.
#' # 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)
#' # signal success and exit.
#' # q(status=0)
#'
#' @import stats
getopt = function (spec=NULL,opt=commandArgs(TRUE),command=get_Rscript_filename(),usage=FALSE,debug=FALSE) {
getopt = function (spec=NULL,opt=NULL,command=get_Rscript_filename(),usage=FALSE,debug=FALSE) {
# littler compatibility - map argv vector to opt
if (exists("argv", where = .GlobalEnv, inherits = FALSE)) {
opt = get("argv", envir = .GlobalEnv)
if (is.null(opt)) {
if (exists("argv", where = .GlobalEnv, inherits = FALSE)) {
opt = get("argv", envir = .GlobalEnv) #nocov
} else {
opt = commandArgs(TRUE)
}
}
ncol=4
......@@ -359,10 +365,6 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=get_Rscript_filename(
if ( length(rowmatch) == 0 ) {
stop(paste('short flag "', this.flag, '" is invalid', sep=''))
#short flag is ambiguous, matches too many options
} else if ( length(rowmatch) > 1 ) {
stop(paste('short flag "', this.flag, '" is ambiguous', sep=''))
#short flag has an argument, but is not the last in a compound flag string
} else if ( j < length(these.flags) & spec[rowmatch,col.has.argument] == flag.required.argument ) {
stop(paste('short flag "', this.flag, '" requires an argument, but has none', sep=''))
......@@ -428,7 +430,7 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=get_Rscript_filename(
# storage.mode(peek.optstring) = spec[current.flag, col.mode]
mode = spec[current.flag, col.mode]
tryCatch(storage.mode(peek.optstring) <- mode,
warning = function(w) {warning(paste(mode, "expected, got", dQuote(peek.optstring)))})
warning = function(w) {warning(paste(mode, "expected, got", dQuote(peek.optstring)))}) #nocov
result[spec[current.flag, col.long.name]] = peek.optstring
i = i + 1
......@@ -449,9 +451,9 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=get_Rscript_filename(
storage.mode(x) = spec[current.flag, col.mode]
result[spec[current.flag, col.long.name]] = x
} else {
stop(paste("This should never happen.",
"Is your spec argument correct? Maybe you forgot to set",
"ncol=4, byrow=TRUE in your matrix call?"))
stop(paste("This should never happen.", #nocov
"Is your spec argument correct? Maybe you forgot to set", #nocov
"ncol=4, byrow=TRUE in your matrix call?")) #nocov
}
}
#trailing flag without required argument
......@@ -470,7 +472,7 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=get_Rscript_filename(
storage.mode(x) = spec[current.flag, col.mode]
result[spec[current.flag, col.long.name]] = x
} else {
stop("this should never happen (2). please inform the author.")
stop("this should never happen (2). please inform the author.") #nocov
}
} #no dangling flag, nothing to do.
......
getopt
======
[![CRAN Status Badge](https://www.r-pkg.org/badges/version/getopt)](https://cran.r-project.org/package=getopt)
[![Build Status](https://travis-ci.org/trevorld/r-getopt.svg?branch=master)](https://travis-ci.org/trevorld/r-getopt)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/trevorld/r-getopt?branch=master&svg=true)](https://ci.appveyor.com/project/trevorld/r-getopt)
[![Coverage Status](https://img.shields.io/codecov/c/github/trevorld/r-getopt.svg)](https://codecov.io/github/trevorld/r-getopt?branch=master)
[![RStudio CRAN mirror downloads](https://cranlogs.r-pkg.org/badges/getopt)](https://cran.r-project.org/package=getopt)
[![Project Status: Active -- The project has reached a stable, usable state but is no longer being actively developed; support/maintenance will be provided as time allows.](http://www.repostatus.org/badges/latest/inactive.svg)](http://www.repostatus.org/#inactive)
`getopt` is an R package designed to be used with `Rscript` to write
\"\#!\"-shebang scripts that accept short and long flags/options. Many
users will prefer using instead the package
[optparse](https://github.com/trevorld/r-optparse) which adds extra
features (automatically generated help option and usage, support for
default values, basic positional argument support).
To install the last version released on CRAN use the following command:
``` {.sourceCode .r}
install.packages("getopt")
```
To install the development version use the following command:
``` {.sourceCode .r}
install.packages("remotes")
remotes:install_github("trevorld/r-getopt")
```
example
-------
An example Rscript using `getopt`:
``` {.sourceCode .r}
#!/path/to/Rscript
library('getopt')
# 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)
```
#!/path/to/Rscript
library('getopt');
library('stats');
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(
......@@ -9,14 +9,14 @@ spec = matrix(c(
'count' , 'c', 1, "integer",
'mean' , 'm', 1, "double",
'sd' , 's', 1, "double"
), byrow=TRUE, ncol=4);
opt = getopt(spec);
), 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);
cat(getopt(spec, usage=TRUE))
q(status=1)
}
#set some reasonable defaults for the options that are needed,
......@@ -27,11 +27,11 @@ 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()); }
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");
cat(paste(rnorm(opt$count,mean=opt$mean,sd=opt$sd),collapse="\n"))
cat("\n")
#signal success and exit.
q(status=0);
q(status=0)
......@@ -5,8 +5,8 @@
\alias{getopt-package}
\title{C-like getopt behavior}
\usage{
getopt(spec = NULL, opt = commandArgs(TRUE),
command = get_Rscript_filename(), usage = FALSE, debug = FALSE)
getopt(spec = NULL, opt = NULL, command = get_Rscript_filename(),
usage = FALSE, debug = FALSE)
}
\arguments{
\item{spec}{The getopt specification, or spec of what options are considered
......@@ -35,7 +35,9 @@ The terms \emph{option}, \emph{flag}, \emph{long flag}, \emph{short flag},
and \emph{argument} have very specific meanings in the context of this
document. Read the ``Description'' section for definitions.}
\item{opt}{This defaults to the return value of \link{commandArgs}(TRUE).
\item{opt}{This defaults to the return value of \link{commandArgs}(TRUE) unless
\code{argv} is in the global environment in which case it uses that instead
(this is for compatibility with littler).
If R was invoked directly via the ``R'' command, this corresponds to all
arguments passed to R after the ``--args'' flag.
......@@ -140,8 +142,8 @@ baz" and recover "foo", "bar", "baz" as a list. Likewise for "my.R foo
#!/path/to/Rscript
library('getopt')
#get options, using the spec as defined by the enclosed list.
#we read the options from the default: commandArgs(TRUE).
# 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",
......@@ -158,22 +160,22 @@ if ( !is.null(opt$help) ) {
q(status=1)
}
#set some reasonable defaults for the options that are needed,
#but were not specified.
# 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.
# print some progress messages to stderr, if requested.
if ( opt$verbose ) { write("writing...",stderr()) }
#do some operation based on user input.
# 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)
# signal success and exit.
# q(status=0)
}
\seealso{
......
......@@ -9,37 +9,37 @@ test_that("getopt works as expected", {
'mean' , 'm', 1, "double",
'sd' , 's', 1, "double",
'output' , 'O', 1, "character"
), ncol=4, byrow=TRUE);
), ncol=4, byrow=TRUE)
expect_equal(sort_list(getopt(spec, c('-c', '-1', '-m', '-1.2'))),
sort_list(list(ARGS=character(0), count=-1, mean=-1.2)));
sort_list(list(ARGS=character(0), count=-1, mean=-1.2)))
expect_equal(sort_list(getopt(spec, c('-v', '-m', '3'))),
sort_list(list(ARGS=character(0), verbose=1, mean=3)));
sort_list(list(ARGS=character(0), verbose=1, mean=3)))
expect_equal(sort_list(getopt(spec, c('-m', '3', '-v'))),
sort_list(list(ARGS=character(0), mean=3, verbose=1)));
sort_list(list(ARGS=character(0), mean=3, verbose=1)))
expect_equal(sort_list(getopt(spec, c('-m', '3', '-v', '2', '-v'))),
sort_list(list(ARGS=character(0), mean=3, verbose=1)));
sort_list(list(ARGS=character(0), mean=3, verbose=1)))
expect_equal(sort_list(getopt(spec, c('-O', '-', '-m', '3'))),
sort_list(list(ARGS=character(0), output="-", mean=3)));
sort_list(list(ARGS=character(0), output="-", mean=3)))
expect_equal(sort_list(getopt(spec, c('-O', '-', '-m', '3'))),
sort_list(getopt(spec, c('-m', '3', '-O', '-'))));
sort_list(getopt(spec, c('-m', '3', '-O', '-'))))
expect_equal(sort_list(getopt(spec, c('-de'))),
sort_list(getopt(spec, c('-ed'))));
sort_list(getopt(spec, c('-ed'))))
expect_equal(sort_list(getopt(spec, c('-de'))),
sort_list(list(ARGS=character(0), dummy1=TRUE, dummy2=TRUE)));
sort_list(list(ARGS=character(0), dummy1=TRUE, dummy2=TRUE)))
expect_equal(sort_list(getopt(spec, c('-de', '1'))),
sort_list(list(ARGS=character(0), dummy1=TRUE, dummy2=NA)));
sort_list(list(ARGS=character(0), dummy1=TRUE, dummy2=NA)))
expect_equal(sort_list(getopt(spec, c('--verbose'))),
sort_list(list(ARGS=character(0), verbose=1)));
sort_list(list(ARGS=character(0), verbose=1)))
expect_equal(sort_list(getopt(spec, c('--verbose', '--help'))),
sort_list(list(ARGS=character(0), verbose=1, help=TRUE)));
sort_list(list(ARGS=character(0), verbose=1, help=TRUE)))
expect_equal(sort_list(getopt(spec, c('--verbose', '--mean', '5'))),
sort_list(list(ARGS=character(0), verbose=1, mean=5)));
sort_list(list(ARGS=character(0), verbose=1, mean=5)))
expect_equal(sort_list(getopt(spec, c('--mean=5'))),
sort_list(list(ARGS=character(0), mean=5)));
sort_list(list(ARGS=character(0), mean=5)))
expect_equal(sort_list(getopt(spec, c('--verbose', '--mean=5', '--sd', '5'))),
sort_list(list(ARGS=character(0), verbose=1, mean=5, sd=5)));
sort_list(list(ARGS=character(0), verbose=1, mean=5, sd=5)))
expect_equal(sort_list(getopt(spec, c('--verbose', '--mean=5', '--sd', '5'))),
sort_list(getopt(spec, c('--mean=5', '--sd', '5', '--verbose'))));
sort_list(getopt(spec, c('--mean=5', '--sd', '5', '--verbose'))))
spec = c(
'date' , 'd', 1, "character",
......@@ -47,25 +47,29 @@ test_that("getopt works as expected", {
'getdata' , 'g', 0, "logical",
'market' , 'm', 1, "character",
'threshold', 't', 1, "double"
);
)
spec2 <- matrix(spec, ncol=4, byrow=TRUE)
# should give warning is spec is not matrix
expect_that(getopt(spec, c('--date','20080421','--market','YM','--getdata')), gives_warning());
expect_that(getopt(spec, c('--date','20080421','--market','YM','--getdata')), gives_warning())
expect_equal(sort_list(getopt(spec2, c('--date','20080421','--market','YM','--getdata'))),
sort_list(list(ARGS=character(0), date='20080421', market='YM', getdata=TRUE)))
expect_equal(sort_list(getopt(spec2, c('--date','20080421','--market','YM','--getdata'))),
sort_list(getopt(spec2, c('--date','20080421','--getdata','--market','YM'))));
sort_list(getopt(spec2, c('--date','20080421','--getdata','--market','YM'))))
expect_that(getopt(spec2, c('--date','20080421','--getdata','--market','YM'),debug=TRUE),
prints_text("processing "));
prints_text("processing "))
expect_that(print(getopt(spec2, c('--date','20080421','--getdata','--market','YM'),usage=TRUE)),
prints_text("Usage: "));
prints_text("Usage: "))
})
test_that("numeric is cast to double", {
# Feature reported upstream (optparse) by Miroslav Posta
spec = matrix(c("count", "c", 1, "integer"), ncol=4, byrow=TRUE)
getopt(spec, c("-c", "-55"))
opt <- getopt(spec, c("-c", "-55"))
expect_equal(typeof(opt$count), "integer")
spec = matrix(c("count", "c", 1, "numeric"), ncol=4, byrow=TRUE)
getopt(spec, c("-c", "-55.0"))
opt <- getopt(spec, c("-c", "-55.0"))
expect_equal(typeof(opt$count), "double")
})
test_that("empty strings are handled correctly for mandatory character arguments", {
spec = matrix(c("string", "s", 1, "character",
......@@ -185,6 +189,15 @@ test_that("tests to get coverage up", {
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')
expect_error(getopt(spec, c("--number", 3, 4)), '"4" is not a valid option, or does not support an argument')
expect_error(getopt(spec, c("-b")), '"b" requires an argument')
expect_error(getopt(spec, c("--foobar")), 'flag "foobar" requires an argument')
expect_error(getopt(spec, c("--foobar", "--help")), 'flag "foobar" requires an argument')
expect_output(getopt(spec, c("-n", "2"), debug=TRUE), "short option: -n")
expect_output(getopt(spec, c("-b", "-"), debug=TRUE), 'lone dash')
# causes covr::report() to crash even though test() runs fine
# expect_warning(getopt(spec, c("-n", "-")), 'double expected, got "-"')
})
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