Getopt::Long
in RWrapper of the Perl module Getopt::Long
in R
GetoptLong(..., help_head = NULL, help_foot = NULL, envir = parent.frame(),
argv_str = NULL, template_control = list(),
help_style = GetoptLong.options$help_style)
Specification of options. The value can be a two-column matrix, a vector with even number of elements or a text template. See the vignette for detailed explanation.
Head of the help message when invoking Rscript foo.R --help
.
Foot of the help message when invoking Rscript foo.R --help
.
User's enrivonment where GetoptLong
looks for default values and exports variables.
A string that contains command-line arguments. It is only for testing purpose.
A list of parameters for controlling when the specification is a template.
The style of the help messages. Value should be either "one-column" or "two-column".
Following shows a simple example. Put following code at the beginning of your script (e.g. foo.R
):
library(GetoptLong)cutoff = 0.05 GetoptLong( "number=i", "Number of items.", "cutoff=f", "Cutoff for filtering results.", "verbose", "Print message." )
Then you can call the script from command line either by:
Rscript foo.R --number 4 --cutoff 0.01 --verbose Rscript foo.R --number 4 --cutoff=0.01 --verbose Rscript foo.R -n 4 -c 0.01 -v Rscript foo.R -n 4 --verbose
In this example, number
is a mandatory option and it should only be in
integer mode. cutoff
is optional and it already has a default value 0.05.
verbose
is a logical option. If parsing is successful, two variables number
and verbose
will be imported into the working environment with the specified
values. Value for cutoff
will be updated if it is specified in command-line.
For advanced use of this function, please go to the vignette.
# NOT RUN {
# There is no example
NULL
# }
Run the code above in your browser using DataLab