Title: | Parsing Command-Line Arguments and Simple Variable Interpolation |
---|---|
Description: | This is a command-line argument parser which wraps the powerful Perl module Getopt::Long and with some adaptations for easier use in R. It also provides a simple way for variable interpolation in R. |
Authors: | Zuguang Gu |
Maintainer: | Zuguang Gu <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1.0 |
Built: | 2024-10-24 02:58:00 UTC |
Source: | https://github.com/jokergoo/getoptlong |
Directory of current script
get_scriptdir()
get_scriptdir()
If the R script is not run from the command-line, it returns NULL
.
Zuguang Gu <[email protected]>
# There is no example NULL
# There is no example NULL
File name of current script
get_scriptname()
get_scriptname()
If the R script is not run from the command-line, it returns NULL
.
Zuguang Gu <[email protected]>
# There is no example NULL
# There is no example NULL
Getopt::Long
in R
Wrapper of the Perl module Getopt::Long
in R
GetOptions(..., envir = parent.frame())
GetOptions(..., envir = parent.frame())
... |
Pass to |
envir |
User's enrivonment where |
This function is the same as GetoptLong
. It is just to make it consistent as the GetOptions()
subroutine in Getopt::Long
module in Perl.
Zuguang Gu <[email protected]>
# There is no example NULL
# There is no example NULL
Getopt::Long
in R
Wrapper 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)
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. |
help_head |
Head of the help message when invoking |
help_foot |
Foot of the help message when invoking |
envir |
User's enrivonment where |
argv_str |
A string that contains command-line arguments. It is only for testing purpose. |
template_control |
A list of parameters for controlling when the specification is a template. |
help_style |
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.
Zuguang Gu <[email protected]>
# There is no example NULL
# There is no example NULL
Global options for GetoptLong()
GetoptLong.options(..., RESET = FALSE, READ.ONLY = NULL, LOCAL = FALSE, ADD = FALSE)
GetoptLong.options(..., RESET = FALSE, READ.ONLY = NULL, LOCAL = FALSE, ADD = FALSE)
... |
Options, see 'Details' section. |
RESET |
Whether to reset options to their default values. |
READ.ONLY |
Whether to only return read-only options. |
LOCAL |
Whether to switch local mode. |
ADD |
Whether to add new options. |
Supported global options are following:
config
Configuration of Getopt::Long
, check https://perldoc.pl/Getopt::Long#Configuring-Getopt::Long .
template_tag
The tag for identifying specifications in the template. The format should be in left_tag CODE right_tag
.
help_style
The style of the help message.
GetoptLong.options(...)
should be put before calling GetoptLong
function.
Zuguang Gu <[email protected]>
# There is no example NULL
# There is no example NULL
Simple variable interpolation in texts
qq(..., envir = parent.frame(), code.pattern = NULL, collapse = TRUE, sep = " ")
qq(..., envir = parent.frame(), code.pattern = NULL, collapse = TRUE, sep = " ")
... |
Text string in which variables are marked with certain rules |
envir |
Environment where to look for variables. By default it is the environment where |
code.pattern |
Pattern of marks for the variables. By default it is |
collapse |
If variables return vector of length larger than one, whether collapse into one string or return a vector |
sep |
Separator character when there are multiple templates. |
I like variable interpolation in Perl. But in R, if you want to concatenate plain text and variables,
you need to use functions such as paste
. However, if there are so many variables, quotes, braces
in the string you want to construct, it would be painful.
This function allows you to construct strings as in Perl style. Variables are
marked in the text with certain rule. qq
will look up these variables in user's
environment and replace the variable marks with their real values.
For more explaination of this function, please refer to vignette.
Zuguang Gu <[email protected]>
a = 1 b = "text" qq("a = @{a}, b = '@{b}'") qq("a = @{a}", "b = '@{b}'", sep = ", ") a = 1:2 qq("a = @{a}, b = '@{b}'") qq("a = @{a}, b = '@{b}'", collapse = FALSE) a = 1 qq("a = `a`, b = '`b`'", code.pattern = "`CODE`")
a = 1 b = "text" qq("a = @{a}, b = '@{b}'") qq("a = @{a}", "b = '@{b}'", sep = ", ") a = 1:2 qq("a = @{a}, b = '@{b}'") qq("a = @{a}, b = '@{b}'", collapse = FALSE) a = 1 qq("a = `a`, b = '`b`'", code.pattern = "`CODE`")
Global options for qq() related functions
qq.options(..., RESET = FALSE, READ.ONLY = NULL, LOCAL = FALSE, ADD = FALSE)
qq.options(..., RESET = FALSE, READ.ONLY = NULL, LOCAL = FALSE, ADD = FALSE)
... |
Options, see 'Details' section. |
RESET |
Whether to reset options to their default values. |
READ.ONLY |
Whether to only return read-only options. |
LOCAL |
Whether to switch local mode. |
ADD |
Whether to add new options. |
Supported options are following:
cat_prefix
prefix of the string which is printed by qqcat
cat_verbose
whether to print text by qqcat
cat_strwrap
whether call strwrap
to wrap the string
code.pattern
code pattern for variable interpolation
Zuguang Gu <[email protected]>
a = 1 qq.options(cat_prefix = "[INFO] ") qqcat("a = @{a}\n") qq.options(cat_verbose = FALSE) qqcat("a = @{a}\n") qq.options(RESET = TRUE) qq.options(code.pattern = "`CODE`") qqcat("a = `a`\n") qq.options(RESET = TRUE)
a = 1 qq.options(cat_prefix = "[INFO] ") qqcat("a = @{a}\n") qq.options(cat_verbose = FALSE) qqcat("a = @{a}\n") qq.options(RESET = TRUE) qq.options(code.pattern = "`CODE`") qqcat("a = `a`\n") qq.options(RESET = TRUE)
Print a string which has been intepolated with variables
qqcat(..., envir = parent.frame(), code.pattern = NULL, file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE, cat_prefix = NULL, strwrap = qq.options("cat_strwrap"), strwrap_param = list(), sep2 = "")
qqcat(..., envir = parent.frame(), code.pattern = NULL, file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE, cat_prefix = NULL, strwrap = qq.options("cat_strwrap"), strwrap_param = list(), sep2 = "")
... |
text string in which variables are marked with certain rules |
envir |
environment where to look for those variables |
code.pattern |
pattern of marks for the variables |
file |
pass to |
sep |
pass to |
fill |
pass to |
labels |
pass to |
append |
pass to |
cat_prefix |
prefix string. It is prior than |
strwrap |
whether call |
strwrap_param |
parameters sent to |
sep2 |
Separation character when there are multiple templates. |
This function is a shortcut of
cat(qq(text, envir, code.pattern), ...)
Additionally, you can add global prefix:
qq.options("cat_prefix" = "[INFO] ") qq.options("cat_prefix" = function(x) format(Sys.time(), "[%Y-%m-%d %H:%M:%S] ")) qq.options("cat_prefix" = NULL)
You can also add local prefix by specifying cat_prefix
in qqcat
.
qqcat(text, cat_prefix = "[INFO] ")
Please refer to qq
to find more details.
Zuguang Gu <[email protected]>
a = 1 b = "text" qqcat("a = @{a}, b = '@{b}'\n") qqcat("a = `a`, b = '`b`'\n", code.pattern = "`CODE`") qq.options("cat_prefix" = function(x) format(Sys.time(), "[%Y-%m-%d %H:%M:%S] ")) qqcat("a = @{a}, b = '@{b}'\n") Sys.sleep(2) qqcat("a = @{a}, b = '@{b}'\n") qq.options(RESET = TRUE)
a = 1 b = "text" qqcat("a = @{a}, b = '@{b}'\n") qqcat("a = `a`, b = '`b`'\n", code.pattern = "`CODE`") qq.options("cat_prefix" = function(x) format(Sys.time(), "[%Y-%m-%d %H:%M:%S] ")) qqcat("a = @{a}, b = '@{b}'\n") Sys.sleep(2) qqcat("a = @{a}, b = '@{b}'\n") qq.options(RESET = TRUE)
Source the R script with command-line arguments
source_script(file, ..., argv_str = NULL)
source_script(file, ..., argv_str = NULL)
file |
The R script |
... |
Pass to |
argv_str |
The command-line arguments. |
# There is no example NULL
# There is no example NULL
Setting sub commands
subCommands(..., help_head = NULL, help_foot = NULL, argv_str = NULL)
subCommands(..., help_head = NULL, help_foot = NULL, argv_str = NULL)
... |
Specification of commands. See section Details. |
help_head |
Head of the help message when invoking |
help_foot |
Foot of the help message when invoking |
argv_str |
A string that contains command-line arguments. It is only for testing purpose. |
The format of input can be one of the following:
1. A matrix with two columns. Then the first column contains paths of the scripts and the second column contains the description of the subcommand. The basenames of path in the first column by removing the suffix are taken as the sub commands. 2. A matrix with three columns. The the first column contains the sub commands, the second column contains corresponding script paths and the third column contains descriptions of the sub commands. 3. A vector with length as multiples of 2. In this case, every two elements are grouped and concatenated into a matrix by rows. Then it follows the rule 1. 4. A vector with length as multiples of 3. In this case, every three elements are grouped and concatenated into a matrix by rows. Then it follows the rule 2.
# There is no example NULL
# There is no example NULL