| 1 |
message("*** CLI ...") |
| 2 |
|
| 3 |
cmdargs <- character(0L) |
| 4 |
print(cmdargs) |
| 5 |
args <- parallelly:::parse_cmd_args(cmdargs = cmdargs) |
| 6 |
utils::str(args) |
| 7 |
|
| 8 |
cmdargs <- c("--int=0", "--int=42", "--num=3.14", "--logical=TRUE", "--name=abc", "--expr=1+2") |
| 9 |
print(cmdargs) |
| 10 |
patterns <- list("--(int)=([[:digit:]]+)", "--(num)=([[:digit:].]+)", "--(logical)=(TRUE|FALSE)", "--(name)=(.*)", "--(expr)=(.*)") |
| 11 |
str(patterns) |
| 12 |
args <- tryCatch(parallelly:::parse_cmd_args(patterns = patterns, cmdargs = cmdargs), error = identity) |
| 13 |
utils::str(args) |
| 14 |
stopifnot( |
| 15 |
args[["int"]] == "42", |
| 16 |
args[["num"]] == "3.14", |
| 17 |
args[["logical"]] == "TRUE", |
| 18 |
args[["name"]] == "abc", |
| 19 |
args[["expr"]] == "1+2" |
| 20 |
) |
| 21 |
|
| 22 |
cmdargs <- c("--int=0", "--int=42", "--num=3.14", "--logical=TRUE", "--name=abc", "--expr=1+2") |
| 23 |
print(cmdargs) |
| 24 |
patterns <- list(parallelly:::cli_arg_integer("int"), parallelly:::cli_arg_numeric("num"), parallelly:::cli_arg_logical("logical"), parallelly:::cli_arg_character("name"), parallelly:::cli_arg_expr("expr")) |
| 25 |
str(patterns) |
| 26 |
args <- tryCatch(parallelly:::parse_cmd_args(patterns = patterns, cmdargs = cmdargs), error = identity) |
| 27 |
utils::str(args) |
| 28 |
stopifnot( |
| 29 |
is.integer(args[["int"]]), args[["int"]] == 42L, |
| 30 |
is.numeric(args[["num"]]), args[["num"]] == 3.14, |
| 31 |
is.logical(args[["logical"]]), args[["logical"]] == TRUE, |
| 32 |
is.character(args[["name"]]), args[["name"]] == "abc", |
| 33 |
is.numeric(args[["expr"]]), args[["expr"]] == 3 |
| 34 |
) |
| 35 |
|
| 36 |
cmdargs <- c("--int=42", "--num=3.14", "--logical=TRUE", "--name=abc") |
| 37 |
print(cmdargs) |
| 38 |
args <- tryCatch(parallelly:::parse_cmd_args(cmdargs = cmdargs), error = identity) |
| 39 |
stopifnot(inherits(args, "error")) |
| 40 |
|
| 41 |
print(parallelly::availableCores, call = FALSE) |
| 42 |
|
| 43 |
options(parallelly.tests.cmdargs = character(0L)) |
| 44 |
print(parallelly::availableCores, call = TRUE) |
| 45 |
|
| 46 |
options(parallelly.tests.cmdargs = c("--max=4")) |
| 47 |
print(parallelly::availableCores, call = TRUE) |
| 48 |
|
| 49 |
|
| 50 |
fcn <- function(abc = 0L, def = 0.0) { |
| 51 |
abc <- as.integer(abc) |
| 52 |
def <- as.numeric(def) |
| 53 |
if (abc == 0) { |
| 54 |
data.frame(abc = abc, def = def) |
| 55 |
} else if (abc < 0) { |
| 56 |
list(abc = abc, def = def) |
| 57 |
} else if (abc > 0) { |
| 58 |
list(abc, def) |
| 59 |
} |
| 60 |
} |
| 61 |
parallelly:::cli_fcn(fcn) <- list(parallelly:::cli_arg_integer("abc"), parallelly:::cli_arg_numeric("def")) |
| 62 |
output <- parallelly:::cli_fcn_output(fcn) |
| 63 |
print(output) |
| 64 |
|
| 65 |
output(42) |
| 66 |
output(list(42)) |
| 67 |
output(list(abc = 42)) |
| 68 |
output(c(abc = 42)) |
| 69 |
|
| 70 |
|
| 71 |
options(parallelly.tests.cmdargs = character(0L)) |
| 72 |
void <- print(fcn) |
| 73 |
|
| 74 |
options(parallelly.tests.cmdargs = c("--abc=0", "--def=3.14")) |
| 75 |
void <- print(fcn) |
| 76 |
|
| 77 |
options(parallelly.tests.cmdargs = c("--abc=-1", "--def=3.14")) |
| 78 |
void <- print(fcn) |
| 79 |
|
| 80 |
options(parallelly.tests.cmdargs = c("--abc=+1", "--def=3.14")) |
| 81 |
void <- print(fcn) |
| 82 |
|
| 83 |
parallelly:::cli_fcn_output(fcn) <- utils::str |
| 84 |
options(parallelly.tests.cmdargs = c("--abc=+1", "--def=3.14")) |
| 85 |
void <- print(fcn) |
| 86 |
|
| 87 |
parallelly:::cli_fcn(fcn) <- list(structure(parallelly:::cli_arg_integer("abc"), type = "unknown")) |
| 88 |
void <- tryCatch(print(fcn), error = identity) |
| 89 |
print(void) |
| 90 |
stopifnot(inherits(void, "error")) |
| 91 |
|
| 92 |
options(parallelly.tests.cmdargs = NULL) |
| 93 |
|
| 94 |
|
| 95 |
res <- tryCatch(parallelly:::cli_prune(), error = identity) |
| 96 |
print(res) |
| 97 |
stopifnot(inherits(res, "error")) |
| 98 |
|
| 99 |
|
| 100 |
message("*** CLI ... DONE") |