| Title: | C-Like 'getopt' Behavior |
|---|---|
| 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. |
| Authors: | Trevor L. Davis [aut, cre] (ORCID: <https://orcid.org/0000-0001-6341-4639>), Allen Day [aut] (Original package author), Roman Zenka [ctb] |
| Maintainer: | Trevor L. Davis <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 1.21.1 |
| Built: | 2026-05-09 07:54:34 UTC |
| Source: | https://github.com/trevorld/r-getopt |
getfile() returns the file name that Rscript is interpreting.
Also supports littler via the LITTLER_SCRIPT_PATH environment variable.
get_Rscript_filename() is an alias.
getfile() get_Rscript_filename()getfile() get_Rscript_filename()
A string with the filename of the calling script.
If not found (i.e. you are in a interactive session) returns NA_character_.
Extracts the positional arguments stored in the "operands" attribute of the
object returned by getopt().
getoperand(x)getoperand(x)
x |
An object of class |
A character vector of positional arguments.
spec <- matrix(c("verbose", "v", 0, "logical"), ncol = 4, byrow = TRUE) opt <- getopt(spec, c("--verbose", "--", "file1.txt", "file2.txt")) getoperand(opt)spec <- matrix(c("verbose", "v", 0, "logical"), ncol = 4, byrow = TRUE) opt <- getopt(spec, c("--verbose", "--", "file1.txt", "file2.txt")) getoperand(opt)
getopt is primarily intended to be used with Rscript. It
facilitates writing #! shebang scripts that accept short and long
flags/options. It can also be used from R directly, but is probably less
useful in this context.
getopt( spec = NULL, opt = NULL, command = getfile(), usage = FALSE, debug = FALSE, operand = "after--only" )getopt( spec = NULL, opt = NULL, command = getfile(), usage = FALSE, debug = FALSE, operand = "after--only" )
spec |
The getopt specification, or spec of what options are considered
valid. The specification must be either a 4-5 column matrix, a 4-5 column data frame, or a
character vector coercible into a 4 column matrix using
Column 1: the long flag name. A multi-character string. Column 2: short flag alias of Column 1. A single-character string.
May be Column 3: Action of the flag. A string. Possible values:
For backwards compatibility Column 4: Data type to which the flag's argument shall be cast using
Column 5 (optional): A brief description of the purpose of the option. The terms option, flag, long flag, short flag,
and argument have very specific meanings in the context of this
document. Read the “Details” section of |
opt |
This defaults to the return value of If R was invoked directly via the If R was invoked via the Read about |
command |
The string to use in the usage message as the name of the script. See argument usage. |
usage |
If |
debug |
This is used internally to debug the |
operand |
Controls how positional arguments (operands) are handled.
Operands are stored in the |
Notes on naming convention:
An option is one of the shell-split input strings.
A flag is a type of option. a flag can be defined as having no argument (defined below), a required argument, or an optional argument.
An argument is a type of option, and is the value associated with a flag.
A long flag is a type of flag, and begins with the string
--. If the long flag has an associated argument, it may be
delimited from the long flag by either a trailing =, or may be
the subsequent option.
A short flag is a type of flag, and begins with the string
-. If a short flag has an associated argument, it is the
subsequent option. short flags may be bundled together,
sharing a single leading -, but only the final short flag is able
to have a corresponding argument.
Many users wonder whether they should use the getopt package, optparse package,
or argparse package.
Here is some of the major differences:
Features available in getopt unavailable in optparse
getopt allows one to specify options with an optional argument.
Long flags may be abbreviated as long as the abbreviation is unique,
e.g. --verb matches --verbose if no other long flag starts with verb.
Some features implemented in the optparse package unavailable in getopt
Support for capturing positional arguments after the optional arguments
when positional_arguments set to TRUE in optparse::parse_args()
Automatic generation of an help option and printing of help text when encounters an -h
Option to specify default arguments for options as well the variable name to store option values
There is also package argparse supports
all the features of both getopt and optparse (plus more)
Some Features unlikely to be implemented in getopt:
Support for lists, e.g. --define os=linux --define os=redhat would
set result$os$linux=TRUE and result$os$redhat=TRUE.
getopt() returns a list data structure containing names of the
flags that were present in the character vector passed in under
the opt argument. Each value of the list is coerced to the
data type specified according to the value of the spec argument. See
the “Details” section for more information.
Any positional arguments (operands) are stored in its "operand" attribute
and can also be accessed by getoperand().
Allen Day and Trevor L. Davis
#!/path/to/Rscript library('getopt') # get options, using the spec as defined by the matrix 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 = 4L) opt <- getopt(spec) # if help was asked for print a friendly message and exit if (isTRUE(opt$help)) { cat(getusage(spec)) q(status = 0) } # set reasonable defaults for options that 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 <- 10L 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")#!/path/to/Rscript library('getopt') # get options, using the spec as defined by the matrix 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 = 4L) opt <- getopt(spec) # if help was asked for print a friendly message and exit if (isTRUE(opt$help)) { cat(getusage(spec)) q(status = 0) } # set reasonable defaults for options that 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 <- 10L 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")
Generate a usage string from a getopt spec matrix.
getusage(spec, command = getfile(), usage = "Usage: %command %options")getusage(spec, command = getfile(), usage = "Usage: %command %options")
spec |
The getopt specification, or spec of what options are considered
valid. The specification must be either a 4-5 column matrix, a 4-5 column data frame, or a
character vector coercible into a 4 column matrix using
Column 1: the long flag name. A multi-character string. Column 2: short flag alias of Column 1. A single-character string.
May be Column 3: Action of the flag. A string. Possible values:
For backwards compatibility Column 4: Data type to which the flag's argument shall be cast using
Column 5 (optional): A brief description of the purpose of the option. The terms option, flag, long flag, short flag,
and argument have very specific meanings in the context of this
document. Read the “Details” section of |
command |
The string to use in the usage message as the name of the script. See argument usage. |
usage |
A template string for the usage line. |
A character string with the usage message.
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) cat(getusage(spec, command = "myscript")) cat(getusage(spec, command = "myscript", usage = "Usage: %command %options FILE"))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) cat(getusage(spec, command = "myscript")) cat(getusage(spec, command = "myscript", usage = "Usage: %command %options FILE"))
sort_list() returns a recursively sorted list
sort_list(unsorted_list)sort_list(unsorted_list)
unsorted_list |
A list. |
A sorted list.
l <- list(b = 2, a = 1) sort_list(l)l <- list(b = 2, a = 1) sort_list(l)