Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
^bin$
^CRAN-SUBMISSION$
^CRAN-PREPARATION\.md$
^CRAN-FEEDBACK\.md$
cran-fixes.patch
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ symbols.rds
*.dll
.Rhistory
.Rproj.user
cran-fixes.patch
*.tar.gz
ribiosArg.Rcheck
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ Authors@R:
role = c("aut", "cre", "ctb"),
email = "jitao_david.zhang@roche.com",
comment = c(ORCID="0000-0002-3085-0909")),
person("F.Hoffmann-La Roche AG", role="cph"))
person(given = "Balazs",
family = "Banfai",
role = "ctb"))
Description: Provides functions to handle command-line arguments for R
scripting. It enables building stand-alone R programs that accept and
parse command-line options in BIOS style.
parse command-line options in 'BIOS' style.
Zhang (2025) <https://github.com/bedapub/ribiosArg>.
Depends:
R (>= 3.4.0),
ribiosUtils
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(argGet)
export(argGetPos)
export(argIsInit)
export(argParse)
export(argPresent)
export(existArg)
Expand Down
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2026-02-12: v1.5-1
+ Add R-level guards in argPresent, argGet, and argGetPos to return safe defaults when the parser is uninitialized
+ Export argIsInit() for users to check parser initialization status
+ argParse returns silently instead of stopping when no script context is detected (fixes --run-donttest examples)

2015-09-04: v1.1-18
+ rarg_parse: fixed a bug caused by passing SEXP as a size_t object

Expand Down
101 changes: 0 additions & 101 deletions NEWS.md

This file was deleted.

20 changes: 13 additions & 7 deletions R/argparse.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#' Parser of command-line parameters in BIOS style
#' @aliases argIsInit
#' @aliases argPresent
#'
#' @param optargs String describing optional arguments. Syntax: \code{<optname1>[,paramcnt1] <optname2>[,paramcnt2]\dots}. Example: \dQuote{verbose outfile,1} means the command line has the syntax \code{prog [-verbose] [outfile name]}. It can be an empty string to express \dQuote{no options}. The value for \code{paramcnt} is 0.
Expand Down Expand Up @@ -37,7 +36,7 @@
#' @useDynLib ribiosArg, .registration=TRUE, .fixes="C_"
#'
#' @examples
#' \dontrun{
#' \donttest{
#' argParse("verbose threshold,2", "infile outfile",
#' usage="prog [-infile ]infile [-outfile ]outfile [-verbose] [-threshold MIN MAX]")
#' argIsInit()
Expand Down Expand Up @@ -70,8 +69,8 @@ argParse <- function(optargs, reqargs, usage=paste(scriptName(), "-h"), strict=T
efind <- which(isE)
allComm <- allComm[-(1:(efind+1))]
} else {
stop("This should not happen: no parameters in the form of '-f' or '--f' is detected. Please contact the developer")
}
return(invisible(NULL))
}
comm <- allComm[!grepl("^--", allComm)]
## the following code was valid till R-3.0.x.
## if("--args" %in% allComm) {
Expand Down Expand Up @@ -114,6 +113,10 @@ argParse <- function(optargs, reqargs, usage=paste(scriptName(), "-h"), strict=T
}
}

#' Check whether the argument parser has been initialized
#'
#' @return Logical, \code{TRUE} if \code{argParse} has been called, \code{FALSE} otherwise.
#' @export
argIsInit <- function() .Call(C_rarg_isInit)

#' Test whether the given option is present in the command line or not
Expand All @@ -127,6 +130,7 @@ argPresent <- function(opt) {
message("[DEBUGGIING] The script is running in an interactive session, e.g. debugging mode. FALSE is returned")
return(FALSE)
}
if(!isTRUE(argIsInit())) return(FALSE)
.Call(C_rarg_present, opt)
}

Expand All @@ -147,14 +151,15 @@ argPresent <- function(opt) {
#' @seealso \code{\link{argParse}}, \code{\link{argGet}}, and \code{\link{argPresent}}
#'
#' @examples
#' \dontrun{argGetPos("thresholds", ind=2)}
#' \donttest{argGetPos("thresholds", ind=2)}
#'
#' @export
argGetPos <- function(opt, ind=1L, default=NULL, choices=NULL) {
if(isDebugging()) {
message("[DEBUGGIING] The script is running in an interactive session, e.g. debugging mode. Default value is returned")
return(default)
}
}
if(!isTRUE(argIsInit())) return(default)
if(argPresent(opt)) {
res <- .Call(C_rarg_getPos, opt,as.integer(ind))
if(!is.null(choices) && !res %in% choices)
Expand Down Expand Up @@ -182,14 +187,15 @@ argGetPos <- function(opt, ind=1L, default=NULL, choices=NULL) {
#' @seealso \code{\link{argParse}}, \code{\link{argGetPos}}, and \code{\link{argPresent}}
#'
#' @examples
#' \dontrun{argGet("infile")}
#' \donttest{argGet("infile")}
#'
#' @export
argGet <- function(opt, default=NULL, choices=NULL) {
if(isDebugging()) {
message("[DEBUGGIING] The script is running in an interactive session, e.g. debugging mode. Default value is returned")
return(default)
}
if(!isTRUE(argIsInit())) return(default)
if(argPresent(opt)) {
res <- .Call(C_rarg_get, opt)
if(!is.null(choices) && !res %in% choices) {
Expand Down
8 changes: 6 additions & 2 deletions R/parseFuncs.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#' @param failVal If the parsing failed (for example length not correct, or non-numeric values were provided, this value will be returned
#' @param sep Separator in the character string, default ","
#'
#' @return A numeric vector of the parsed values, or \code{failVal} if parsing fails.
#' @seealso \code{\link{argGet}}
#'
#' @export
Expand Down Expand Up @@ -127,6 +128,7 @@ parsePairs <- function(str, collapse=",", sep="=",
#' @param make.names Should names be converted to adhere to the rule of variable names in R
#' @param verbose Logical vector
#'
#' @return A factor with the specified levels.
#' @export
#' @examples
#' makeFactor(c("A", "B", "C", "C", "A"), levels=LETTERS[3:1])
Expand Down Expand Up @@ -178,6 +180,7 @@ makeFactor <- function(groups, levels=NULL, make.names=TRUE, verbose=FALSE) {
#' @param make.names Logical, should names be converted to adhere to the rule of variable names in R
#' @param collapse Character used in \code{relevels} to collapse different levels
#'
#' @return A factor parsed from the input string with the specified levels.
#' @export
#' @examples
#' parseFactor("A,B,C,B,A", rlevels="A,B,C")
Expand All @@ -194,14 +197,14 @@ makeFactor <- function(groups, levels=NULL, make.names=TRUE, verbose=FALSE) {
#' groups <- factor(c("B", "C", "A", "D"), levels=c("D","C","A","B"))
#' makeFactor(groups)
#'
#' \dontrun{
#' \donttest{
#' groups <- c("ATest", "Control", "Control", "ATest")
#' levels <- c("Control", "ATest", "Unknown")
#' makeFactor(groups, levels)
#'
#' groups <- c("ATest", "Control", "Control", "ATest", "BTest")
#' levels <- c("Control", "ATest")
#' makeFactor(groups, levels)
#' try(makeFactor(groups, levels))
#' }
#'
parseFactor <- function(str, rlevels=NULL, make.names=TRUE, collapse=",") { ## CL=command line
Expand Down Expand Up @@ -233,6 +236,7 @@ isDir <- function(str) file.info(str)$isdir
#' @param recursive In cse of directory or compressed files, whether files should be found recursively
#' @param ignore.case In case of directory or compressed files, whether case should be ignored
#'
#' @return A character vector of file paths.
#' @importFrom ribiosUtils extname
#' @importFrom utils untar unzip
#' @export
Expand Down
10 changes: 6 additions & 4 deletions R/ribiosArg.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#' Command-line argument handling for R scripting
#' @docType package
#' @description Provides command-line argument handling for R scripting
#' @keywords internal
"_PACKAGE"

#' ribiosIO
#' ribiosIO provides Command-line argument handling for R scripting
#' @author Jitao David Zhang <jitao_david.zhang@roche.com>
#' @useDynLib ribiosArg, .registration=TRUE, .fixes="C_"
#' @name ribiosArg-package
#' @name ribiosArg
NULL
12 changes: 7 additions & 5 deletions R/scriptInit.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#' Prepare the environment for a script
#' @aliases initScript
#'
#'
#' This function is called at the beginning of an Rscript, in order to
#' prepare the R environment to run in a script setting.
#'
#' @return Only side effect is used
#'
#' @return No return value, called for side effects.
#'
#' @aliases initScript
#' @export
#' @examples
#' \dontrun{
#' \donttest{
#' scriptInit()
#' }
scriptInit <- function() {
old <- options()
on.exit(options(old))
if(interactive()) {
setDebug()
} else {
Expand Down
4 changes: 2 additions & 2 deletions R/scriptName.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#'
#' @export
#' @examples
#' \dontrun{scriptName()}
#' \donttest{scriptName()}
#'
scriptName <- function() {
filename <- grep("--file=", commandArgs(), value=TRUE)
Expand Down Expand Up @@ -49,7 +49,7 @@ scriptName <- function() {
#'
#' @export
#' @examples
#' \dontrun{scriptPath()}
#' \donttest{scriptPath()}
#'
scriptPath <- function() {
sname <- scriptName()
Expand Down
4 changes: 3 additions & 1 deletion R/scriptSkeleton.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#'
#' @param file Output file. By default the function writes to standard output.
#'
#' @return Invisibly returns the character vector of skeleton lines.
#' Called for its side effect of writing to \code{file}.
#' @export
#' @examples
#' scriptSkeleton()
#' scriptSkeleton(file = file.path(tempdir(), "myscript.R"))

scriptSkeleton <- function(file=stdout()) {
sentences <- c("#!/usr/bin/env Rscript",
Expand Down
2 changes: 1 addition & 1 deletion man/argGet.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/argGetPos.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/argIsInit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/argParse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading