Commit c94c1d2b authored by Dirk Eddelbuettel's avatar Dirk Eddelbuettel

Import Upstream version 1.20.0

parent 1502c840
This diff is collapsed.
Package: getopt Package: getopt
Type: Package Type: Package
Title: C-like getopt behavior. Title: C-like getopt behavior.
Version: 1.19.1 Version: 1.20.0
Author: Allen Day Contributions from Trevor L Davis Author: Allen Day.
Contributions from Trevor L Davis.
Maintainer: Trevor L Davis <trevor.l.davis@stanford.edu> Maintainer: Trevor L Davis <trevor.l.davis@stanford.edu>
Description: Package designed to be used with Rscript to write ``#!'' URL: https://github.com/trevorld/getopt
shebang scripts that accept short and long flags/options. Many BugReports: https://github.com/trevorld/getopt/issues
users will prefer using instead the package optparse which adds Description: Package designed to be used with Rscript to write
extra features (automatically generated help option and usage, ``#!'' shebang scripts that accept short and long flags/options.
support for default values, basic positional argument support). 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) License: GPL (>= 2)
Suggests: testthat Suggests: testthat
Collate: 'getopt.R' Collate: 'getopt.R' 'utils.R'
Repository: CRAN Packaged: 2013-08-30 07:02:26 UTC; trevorld
Repository/R-Forge/Project: optparse
Repository/R-Forge/Revision: 116
Repository/R-Forge/DateTimeStamp: 2013-03-01 22:49:51
Date/Publication: 2013-03-04 07:40:03
Packaged: 2013-03-01 23:15:17 UTC; rforge
NeedsCompilation: no NeedsCompilation: no
Repository: CRAN
Date/Publication: 2013-08-30 10:36:29
bf4ef0ccdbd56ea20320566ce81af357 *DESCRIPTION b234ee4d69f5fce4486a80fdaf4a4263 *COPYING
aebacd1e62ffaff4ccc2243969fd06b0 *NAMESPACE 08656e23fb8bd07c69f68e7d5443f05a *DESCRIPTION
96424b4d8b4cc2bdeb2031a954658e77 *NEWS dd396c987d24860d37a42fc7541e77b5 *NAMESPACE
30b673c833af7a333dab2354bba1ca38 *R/getopt.R f651db85028ffd75fd6e59b496a413fe *NEWS
7118628e7508f453e19082f20ed4991e *inst/tests/test-getopt.R 244e1fed9d7aaaffaf66976576c06f9d *R/getopt.R
e374b85f40f7549cbdb66c9065c1f1ec *R/utils.R
252fbb7071cd1cc555ca81c8f537a6b9 *inst/tests/test-getopt.R
f692f19f2e09013c3d075f6d322473c0 *man/get_Rscript_filename.Rd f692f19f2e09013c3d075f6d322473c0 *man/get_Rscript_filename.Rd
7476469866359a385df8551bf79b1cce *man/getopt.Rd 7edc52e347b20c53819d16cef5ff8086 *man/getopt.Rd
21469f658334a9ef307e44fcd49a1710 *man/sort_list.Rd
71912d95683c1891b4523acbc554da5e *tests/run-all.R 71912d95683c1891b4523acbc554da5e *tests/run-all.R
export(get_Rscript_filename) export(get_Rscript_filename)
export(getopt) export(getopt)
export(sort_list)
getopt 1.20.0
=============
* Type of "numeric" in spec automatically cast to "double".
Previously users might have had an error passing negative numbers if they
accidentally specified "numeric" instead of "double".
* Project website moved to https://github.com/trevorld/getopt
* Exports new function ``sort_list``.
getopt 1.19.1 getopt 1.19.1
============= =============
* If a passed in option matches multiple options in the getopt specification but matches one exactly * If a passed in option matches multiple options in the getopt specification but matches one exactly
......
# Copyright (c) 2008-2010 Allen Day
# Copyright (c) 2011-2013 Trevor L. Davis <trevor.l.davis@stanford.edu>
#
# 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
# Free Software Foundation, either version 2 of the License, or (at your
# option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#' C-like getopt behavior #' C-like getopt behavior
#' #'
#' getopt is primarily intended to be used with ``\link{Rscript}''. It #' getopt is primarily intended to be used with ``\link{Rscript}''. It
...@@ -33,13 +49,14 @@ ...@@ -33,13 +49,14 @@
#' sharing a single leading ``-'', but only the final \emph{short flag} is able #' sharing a single leading ``-'', but only the final \emph{short flag} is able
#' to have a corresponding \emph{argument}. #' to have a corresponding \emph{argument}.
#' #'
#' Many users wonder whether they should use the getopt package or optparse package #' Many users wonder whether they should use the getopt package, optparse package,
#' or argparse package.
#' Here is some of the major differences: #' Here is some of the major differences:
#' #'
#' Features available in \code{getopt} unavailable in \code{optparse} #' Features available in \code{getopt} unavailable in \code{optparse}
#' #'
#' 1. As well as allowing one to specify options that take either #' 1. As well as allowing one to specify options that take either
#' no argument or a required argument, #' no argument or a required argument like \code{optparse},
#' \code{getopt} also allows one to specify option with an optional argument. #' \code{getopt} also allows one to specify option with an optional argument.
#' #'
#' Some features implemented in \code{optparse} package unavailable in \code{getopt} #' Some features implemented in \code{optparse} package unavailable in \code{getopt}
...@@ -52,9 +69,9 @@ ...@@ -52,9 +69,9 @@
#' 3. Option to specify default arguments for options as well the #' 3. Option to specify default arguments for options as well the
#' variable name to store option values #' variable name to store option values
#' #'
#' There is also new package \code{argparse} which contains all the features #' There is also new package \code{argparse} introduced in 2012 which contains
#' of both getopt and optparse but which has a dependency on Python (>= 2.7) #' all the features of both getopt and optparse but which has a dependency on
#' and has not been used in production for a few years #' Python 2.7 or 3.2+ and has not been used in production since 2008 or 2009
#' like the getopt and optparse packages. #' like the getopt and optparse packages.
#' #'
#' Some Features unlikely to be implemented in \code{getopt}: #' Some Features unlikely to be implemented in \code{getopt}:
...@@ -99,6 +116,7 @@ ...@@ -99,6 +116,7 @@
#' \link{storage.mode}. A multi-\link{character} string. This only considered #' \link{storage.mode}. A multi-\link{character} string. This only considered
#' for same-row Column 3 values of 1,2. Possible values: \link{logical}, #' for same-row Column 3 values of 1,2. Possible values: \link{logical},
#' \link{integer}, \link{double}, \link{complex}, \link{character}. #' \link{integer}, \link{double}, \link{complex}, \link{character}.
#' If \link{numeric} is encountered then it will be converted to double.
#' #'
#' Column 5 (optional): A brief description of the purpose of the option. #' Column 5 (optional): A brief description of the purpose of the option.
#' #'
...@@ -216,6 +234,8 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=get_Rscript_filename( ...@@ -216,6 +234,8 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=get_Rscript_filename(
if ( length(na.omit(unique(spec[,col.short.name]))) != length(na.omit(spec[,col.short.name])) ) { if ( length(na.omit(unique(spec[,col.short.name]))) != length(na.omit(spec[,col.short.name])) ) {
stop(paste('redundant short names for flags (column ',col.short.name,').',sep='')); stop(paste('redundant short names for flags (column ',col.short.name,').',sep=''));
} }
# convert numeric type to double type
spec[,4] <- gsub("numeric", "double", spec[,4])
# if usage=TRUE, don't process opt, but generate a usage string from the data in spec # if usage=TRUE, don't process opt, but generate a usage string from the data in spec
if ( usage ) { if ( usage ) {
...@@ -446,18 +466,3 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=get_Rscript_filename( ...@@ -446,18 +466,3 @@ getopt = function (spec=NULL,opt=commandArgs(TRUE),command=get_Rscript_filename(
} }
return(result); return(result);
} }
#' Returns file name of calling Rscript
#'
#' \code{get_Rscript_filename} returns the file name of calling Rscript
#' @return A string with the filename of the calling script.
#' If not found (i.e. you are in a interactive session) returns NA.
#'
#' @export
get_Rscript_filename <- function() {
prog <- sub("--file=", "", grep("--file=", commandArgs(), value=TRUE)[1])
if( .Platform$OS.type == "windows") {
prog <- gsub("\\\\", "\\\\\\\\", prog)
}
prog
}
# Copyright (c) 2011-2013 Trevor L. Davis <trevor.l.davis@stanford.edu>
#
# 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
# Free Software Foundation, either version 2 of the License, or (at your
# option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#' Returns file name of calling Rscript
#'
#' \code{get_Rscript_filename} returns the file name of calling Rscript
#' @return A string with the filename of the calling script.
#' If not found (i.e. you are in a interactive session) returns NA.
#'
#' @export
get_Rscript_filename <- function() {
prog <- sub("--file=", "", grep("--file=", commandArgs(), value=TRUE)[1])
if( .Platform$OS.type == "windows") {
prog <- gsub("\\\\", "\\\\\\\\", prog)
}
prog
}
#' Recursively sorts a list
#'
#' \code{sort_list} returns a sorted list
#' @param unsorted_list A list.
#' @return A sorted list.
#' @export
sort_list <- function(unsorted_list) {
for(ii in seq(along=unsorted_list)) {
if(is.list(unsorted_list[[ii]])) {
unsorted_list[[ii]] <- sort_list(unsorted_list[[ii]])
}
}
unsorted_list[sort(names(unsorted_list))]
}
library("testthat")
library("getopt")
sort_list <- function(unsorted_list) {
for(ii in seq(along=unsorted_list)) {
if(is.list(unsorted_list[[ii]])) {
unsorted_list[[ii]] <- sort_list(unsorted_list[[ii]])
}
}
unsorted_list[sort(names(unsorted_list))]
}
context("Testing getopt") context("Testing getopt")
test_that("getopt works as expected", { test_that("getopt works as expected", {
spec = matrix(c( spec = matrix(c(
...@@ -51,6 +40,7 @@ test_that("getopt works as expected", { ...@@ -51,6 +40,7 @@ test_that("getopt works as expected", {
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'))), 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( spec = c(
'date' , 'd', 1, "character", 'date' , 'd', 1, "character",
'help' , 'h', 0, "logical", 'help' , 'h', 0, "logical",
...@@ -58,27 +48,41 @@ test_that("getopt works as expected", { ...@@ -58,27 +48,41 @@ test_that("getopt works as expected", {
'market' , 'm', 1, "character", 'market' , 'm', 1, "character",
'threshold', 't', 1, "double" 'threshold', 't', 1, "double"
); );
spec2 <- matrix(spec, ncol=4, byrow=TRUE)
# should give warning is spec is not matrix # 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(spec, c('--date','20080421','--market','YM','--getdata'))), expect_equal(sort_list(getopt(spec2, c('--date','20080421','--market','YM','--getdata'))),
sort_list(list(ARGS=character(0), date='20080421', market='YM', getdata=TRUE))) sort_list(list(ARGS=character(0), date='20080421', market='YM', getdata=TRUE)))
expect_equal(sort_list(getopt(spec, c('--date','20080421','--market','YM','--getdata'))), expect_equal(sort_list(getopt(spec2, c('--date','20080421','--market','YM','--getdata'))),
sort_list(getopt(spec, c('--date','20080421','--getdata','--market','YM')))); sort_list(getopt(spec2, c('--date','20080421','--getdata','--market','YM'))));
expect_that(getopt(spec, c('--date','20080421','--getdata','--market','YM'),debug=TRUE), expect_that(getopt(spec2, c('--date','20080421','--getdata','--market','YM'),debug=TRUE),
prints_text("processing ")); prints_text("processing "));
expect_that(print(getopt(spec, c('--date','20080421','--getdata','--market','YM'),usage=TRUE)), 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"))
spec = matrix(c("count", "c", 1, "numeric"), ncol=4, byrow=TRUE)
getopt(spec, c("-c", "-55.0"))
})
test_that("don't throw error if multiple matches match one argument fully", { test_that("don't throw error if multiple matches match one argument fully", {
# test if partial name matches fully, # test if partial name matches fully,
# still throw error if multiple matches and doesn't match both fully # still throw error if multiple matches and doesn't match both fully
# feature request from Jonas Zimmermann # feature request from Jonas Zimmermann
spec = c( spec = matrix(c(
'foo' , 'f', 0, "logical", 'foo' , 'f', 0, "logical",
'foobar' , 'b', 0, "logical", 'foobar' , 'b', 0, "logical",
'biz' , 'z', 0, "logical" 'biz' , 'z', 0, "logical"
) ), ncol=4, byrow=TRUE)
expect_that(getopt(spec, c('--fo')), throws_error()) expect_that(getopt(spec, c('--fo')), throws_error())
expect_equal(getopt(spec, c('--foo')), sort_list(list(ARGS=character(0), foo=TRUE))) expect_equal(getopt(spec, c('--foo')), sort_list(list(ARGS=character(0), foo=TRUE)))
}) })
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)))
})
...@@ -30,7 +30,8 @@ ...@@ -30,7 +30,8 @@
multi-\link{character} string. This only considered for multi-\link{character} string. This only considered for
same-row Column 3 values of 1,2. Possible values: same-row Column 3 values of 1,2. Possible values:
\link{logical}, \link{integer}, \link{double}, \link{logical}, \link{integer}, \link{double},
\link{complex}, \link{character}. \link{complex}, \link{character}. If \link{numeric} is
encountered then it will be converted to double.
Column 5 (optional): A brief description of the purpose Column 5 (optional): A brief description of the purpose
of the option. of the option.
...@@ -107,16 +108,16 @@ ...@@ -107,16 +108,16 @@
\emph{argument}. \emph{argument}.
Many users wonder whether they should use the getopt Many users wonder whether they should use the getopt
package or optparse package Here is some of the major package, optparse package, or argparse package. Here is
differences: some of the major differences:
Features available in \code{getopt} unavailable in Features available in \code{getopt} unavailable in
\code{optparse} \code{optparse}
1. As well as allowing one to specify options that take 1. As well as allowing one to specify options that take
either no argument or a required argument, \code{getopt} either no argument or a required argument like
also allows one to specify option with an optional \code{optparse}, \code{getopt} also allows one to specify
argument. option with an optional argument.
Some features implemented in \code{optparse} package Some features implemented in \code{optparse} package
unavailable in \code{getopt} unavailable in \code{getopt}
...@@ -132,11 +133,11 @@ ...@@ -132,11 +133,11 @@
3. Option to specify default arguments for options as 3. Option to specify default arguments for options as
well the variable name to store option values well the variable name to store option values
There is also new package \code{argparse} which contains There is also new package \code{argparse} introduced in
all the features of both getopt and optparse but which 2012 which contains all the features of both getopt and
has a dependency on Python (>= 2.7) and has not been used optparse but which has a dependency on Python 2.7 or 3.2+
in production for a few years like the getopt and and has not been used in production since 2008 or 2009
optparse packages. like the getopt and optparse packages.
Some Features unlikely to be implemented in Some Features unlikely to be implemented in
\code{getopt}: \code{getopt}:
......
\name{sort_list}
\alias{sort_list}
\title{Recursively sorts a list}
\usage{
sort_list(unsorted_list)
}
\arguments{
\item{unsorted_list}{A list.}
}
\value{
A sorted list.
}
\description{
\code{sort_list} returns a sorted list
}
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