getopt/ 0000755 0001762 0000144 00000000000 14506210752 011556 5 ustar ligges users getopt/NAMESPACE 0000644 0001762 0000144 00000000172 14130057213 012767 0 ustar ligges users # Generated by roxygen2: do not edit by hand
export(get_Rscript_filename)
export(getopt)
export(sort_list)
import(stats)
getopt/exec/ 0000755 0001762 0000144 00000000000 13400340627 012477 5 ustar ligges users getopt/exec/example.R 0000644 0001762 0000144 00000002141 13400340622 014246 0 ustar ligges users #!/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)
getopt/README.md 0000644 0001762 0000144 00000005075 14505725505 013052 0 ustar ligges users # getopt
[](https://cran.r-project.org/package=getopt)
[](https://github.com/trevorld/r-getopt/actions)
[](https://app.codecov.io/github/trevorld/r-getopt?branch=master)
[](https://cran.r-project.org/package=getopt)
[](https://cran.r-project.org/package=getopt)
`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:
install.packages("getopt")
To install the development version use the following command:
install.packages("remotes")
remotes:install_github("trevorld/r-getopt")
## example
An example Rscript using `getopt`:
#!/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(rnorm(opt$count,mean=opt$mean,sd=opt$sd),sep="\n")
# signal success and exit.
# q(status=0)
getopt/man/ 0000755 0001762 0000144 00000000000 14505724727 012344 5 ustar ligges users getopt/man/sort_list.Rd 0000644 0001762 0000144 00000000571 14505721643 014652 0 ustar ligges users % 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)
}
\arguments{
\item{unsorted_list}{A list.}
}
\value{
A sorted list.
}
\description{
\code{sort_list()} returns a recursively sorted list
}
\examples{
l <- list(b = 2, a = 1)
sort_list(l)
}
getopt/man/getopt.Rd 0000644 0001762 0000144 00000015761 14506176406 014143 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/getopt.R
\name{getopt}
\alias{getopt}
\alias{getopt-package}
\title{C-like getopt behavior}
\usage{
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
valid. The specification must be either a 4-5 column matrix, or a
character vector coercible into a 4 column matrix using
\code{matrix(x,ncol=4,byrow=TRUE)} command. The matrix/vector
contains:
Column 1: the \emph{long flag} name. A multi-character string.
Column 2: \emph{short flag} alias of Column 1. A single-character
string.
Column 3: \emph{Argument} mask of the \emph{flag}. An integer.
Possible values: 0=no argument, 1=required argument, 2=optional argument.
Column 4: Data type to which the \emph{flag}'s argument shall be cast using
\code{\link[=storage.mode]{storage.mode()}}. A multi-character string. This only considered
for same-row Column 3 values of 1,2. Possible values: logical,
integer, double, complex, character.
If numeric is encountered then it will be converted to double.
Column 5 (optional): A brief description of the purpose of the option.
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 \dQuote{Description} section for definitions.}
\item{opt}{This defaults to the return value of \code{commandArgs(TRUE)} unless
\code{argv} is in the global environment in which case it uses that instead
(this is for compatibility with \code{littler}).
If R was invoked directly via the \code{R} command, this corresponds to all
arguments passed to R after the \code{--args} flag.
If R was invoked via the \code{Rscript} command, this corresponds to all
arguments after the name of the R script file.
Read about \code{\link[=commandArgs]{commandArgs()}} and \link{Rscript} to learn more.}
\item{command}{The string to use in the usage message as the name of the
script. See argument \emph{usage}.}
\item{usage}{If \code{TRUE}, argument \code{opt} will be ignored and a usage
statement (character string) will be generated and returned from \code{spec}.}
\item{debug}{This is used internally to debug the \code{getopt()} function itself.}
}
\description{
\code{getopt} is primarily intended to be used with \code{Rscript}. It
facilitates writing \verb{#!} shebang scripts that accept short and long
flags/options. It can also be used from \code{R} directly, but is probably less
useful in this context.
}
\details{
\code{\link[=getopt]{getopt()}} returns a list data structure containing names of the
flags that were present in the character vector passed in under
the \code{opt} argument. Each value of the list is coerced to the
data type specified according to the value of the \code{spec} argument. See
below for details.
Notes on naming convention:
\enumerate{
\item An \emph{option} is one of the shell-split input strings.
\item A \emph{flag} is a type of \emph{option}. a \emph{flag} can be defined as
having no \emph{argument} (defined below), a required \emph{argument}, or an
optional \emph{argument}.
\item An \emph{argument} is a type of \emph{option}, and is the value associated
with a flag.
\item A \emph{long flag} is a type of \emph{flag}, and begins with the string
\verb{--}. If the \emph{long flag} has an associated \emph{argument}, it may be
delimited from the \emph{long flag} by either a trailing \code{=}, or may be
the subsequent \emph{option}.
\item A \emph{short flag} is a type of \emph{flag}, and begins with the string
\code{-}. If a \emph{short flag} has an associated \emph{argument}, it is the
subsequent \emph{option}. \emph{short flags} may be bundled together,
sharing a single leading \code{-}, but only the final \emph{short flag} is able
to have a corresponding \emph{argument}.
}
Many users wonder whether they should use the \code{getopt} package, \code{optparse} package,
or \code{argparse} package.
Here is some of the major differences:
Features available in \code{getopt} unavailable in \code{optparse}
\enumerate{
\item As well as allowing one to specify options that take either
no argument or a required argument like \code{optparse},
\code{getopt} also allows one to specify option with an optional argument.
}
Some features implemented in \code{optparse} package unavailable in \code{getopt}
\enumerate{
\item Limited support for capturing positional arguments after the optional arguments
when \code{positional_arguments} set to \code{TRUE} in \code{optparse::parse_args()}
\item Automatic generation of an help option and printing of help text when encounters an \code{-h}
\item Option to specify default arguments for options as well the
variable name to store option values
}
There is also new package \code{argparse} introduced in 2012 which contains
all the features of both getopt and optparse but which has a dependency on
Python 2.7 or 3.2+.
Some Features unlikely to be implemented in \code{getopt}:
\enumerate{
\item Support for multiple, identical flags, e.g. for \verb{-m 3 -v 5 -v}, the
trailing \code{-v} overrides the preceding \verb{-v 5}, result is \code{v=TRUE} (or equivalent
typecast).
\item Support for multi-valued flags, e.g. \verb{--libpath=/usr/local/lib --libpath=/tmp/foo}.
\item Support for lists, e.g. \verb{--define os=linux --define os=redhat} would
set \code{result$os$linux=TRUE} and \code{result$os$redhat=TRUE}.
\item Support for incremental, argument-less flags, e.g. \verb{/path/to/script -vvv} should set \code{v=3}.
\item Support partial-but-unique string match on options, e.g. \code{--verb} and
\code{--verbose} both match long flag \code{--verbose}.
\item No support for mixing in positional arguments or extra arguments that
don't match any options. For example, you can't do \verb{my.R --arg1 1 foo bar baz} and recover \code{foo}, \code{bar}, \code{baz} as a list. Likewise for \verb{my.R foo --arg1 1 bar baz}.
}
}
\examples{
#!/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(rnorm(opt$count, mean = opt$mean, sd = opt$sd), sep = "\n")
# signal success and exit.
# q(status=0)
}
\author{
Allen Day
}
\keyword{data}
getopt/man/get_Rscript_filename.Rd 0000644 0001762 0000144 00000000737 14505722072 016756 0 ustar ligges users % 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 being interpreted by Rscript}
\usage{
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 \code{NA_character_}.
}
\description{
\code{get_Rscript_filename()} returns the file name that \code{Rscript} is interpreting.
}
getopt/man/figures/ 0000755 0001762 0000144 00000000000 14127752175 014006 5 ustar ligges users getopt/man/figures/logo.png 0000644 0001762 0000144 00000001774 14127752175 015465 0 ustar ligges users PNG
IHDR PLTE 2zdj tRNS DP! pHYs ~ IDATxے0P{gUe]Y
7:EbZ
f?S-e#(=Rά]h=ϿB'AvaҺ4v].RXilʺj,Vf堉Xi#`EMf~]Y
+KScŢtNX,Oڊ"em'=͌eL3uɌYV4m'~@:vbЂNX-Z`
Z:f~BZdaY-rZhV%%`piIX-ZO