Skip to content

Commit 2476701

Browse files
authored
Merge pull request #5 from r-lib/feature/short-opts
Implement short opts
2 parents f803125 + 7cc9e99 commit 2476701

5 files changed

Lines changed: 47 additions & 12 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Rapp (development version)
22

3+
* Added support for short opts (#4, #5).
4+
35
# Rapp 0.2.0
46

57
* Updated default `--help` output.

R/app.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ get_app_inputs <- function(app) {
139139
while (is_hashpipe[anno_start - 1L])
140140
subtract(anno_start) <- 1L
141141

142-
anno <- parse_hashpipe_yaml(lines[anno_start:anno_end])
142+
anno <- parse_hashpipe_yaml(lines[anno_start:anno_end],
143+
handlers = list("bool#yes"= identity,
144+
"bool#no" = identity))
145+
143146
arg <- utils::modifyList(arg, anno)
144147
}
145148

R/args.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ process_args <- function(args, app) {
1010
on.exit(close(args))
1111
}
1212

13+
short_opt_to_long_opt <- local({
14+
table <- unlist(lapply(app$opts, function(opt) opt$short))
15+
table <- setNames(as.list(sprintf("--%s", names(table))),
16+
sprintf("-%s", table))
17+
function(short_opt) table[[short_opt]]
18+
})
1319
positional_args <- character()
1420
while(length(a <- readLines(args, 1L))) {
1521

@@ -42,9 +48,12 @@ process_args <- function(args, app) {
4248
next
4349
}
4450

45-
if(arg_type == "short-opt") {
46-
# convert to a long opt, possibly pushBack()ing val to args
47-
.NotYetImplemented()
51+
if (arg_type == "short-opt") {
52+
long_name <- short_opt_to_long_opt(a)
53+
if (!is.null(long_name)) {
54+
pushBack(long_name, args)
55+
next
56+
}
4857
}
4958

5059
# resolve these values in this block

inst/examples/flip-coin.R

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@
55

66

77
#| description: Number of coin flips
8-
n <- 1L
8+
#| short: n
9+
flips <- 1L
910

1011
sep <- " "
1112
wrap <- TRUE
1213

13-
cat(sample(c("heads", "tails"), n, TRUE),
14+
seed <- NA_integer_
15+
if (!is.na(seed))
16+
set.seed(seed)
17+
18+
cat(sample(c("heads", "tails"), flips, TRUE),
1419
sep = sep, fill = wrap)
1520

1621

17-
# Rapp flip-coin.R
18-
# Rapp flip-coin.R --n 3
19-
# Rapp flip-coin.R --n=30
20-
# Rapp flip-coin.R --n=30 --wrap
21-
# Rapp flip-coin.R --n 30 --no-wrap --sep __
22-
# Rapp flip-coin.R --n 30 --no-wrap
22+
# flip-coin.R
23+
# flip-coin.R --flips 3
24+
# flip-coin.R -n 3
25+
# flip-coin.R --flips=30
26+
# flip-coin.R --flips=30 --wrap
27+
# flip-coin.R -n 30 --no-wrap --sep __
28+
# flip-coin.R -n 30 --no-wrap

tests/testthat/test-basics.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,19 @@ test_that("examples work", {
4242
c("c", "b", "a")
4343
)
4444

45+
expect_all_equal <- function(...) {
46+
if(...length()<2) stop("not enough args")
47+
for(i in 2:...length()) {
48+
expect_equal(..1, ...elt(i))
49+
}
50+
}
51+
52+
expect_all_equal(
53+
"tails tails tails",
54+
run_app("flip-coin.R --flips 3 --seed 1234"),
55+
run_app("flip-coin.R --flips=3 --seed 1234"),
56+
run_app("flip-coin.R -n 3 --seed 1234"),
57+
run_app("flip-coin.R -n 3 --seed=1234")
58+
)
59+
4560
})

0 commit comments

Comments
 (0)