Skip to content

Commit 5f2e089

Browse files
authored
Merge pull request #6 from r-lib/feature/short-opts
Show short opt alias in `--help`
2 parents 2476701 + 9301461 commit 5f2e089

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

R/args.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,14 @@ print_app_help <- function(app, yaml = TRUE) {
220220
if (opt$arg_type == "option") {
221221
default <- opt$default
222222
type <- opt$val_type
223+
short_name <- opt$short
223224
if (type == "string")
224225
default <- deparse1(default)
225-
header <- sprintf(" --%s <value> (Default: %s, Type: %s)", name, default, type)
226+
header <- if (is.null(short_name)) {
227+
sprintf(" --%s <value> (Default: %s, Type: %s)", name, default, type)
228+
} else {
229+
sprintf(" -%s, --%s <value> (Default: %s, Type: %s)", short_name, name, default, type)
230+
}
226231
description <- if (length(opt$description))
227232
strwrap(opt$description, 70, indent = 6, exdent = 6)
228233
out <- paste0(c(header, description), collapse = "\n")

tests/testthat/_snaps/basics.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# examples work
2+
3+
Code
4+
writeLines(run_app("flip-coin.R --help"))
5+
Output
6+
Flip a coin.
7+
8+
Usage: flip-coin [options]
9+
10+
Options:
11+
-n, --flips <value> (Default: 1, Type: integer)
12+
Number of coin flips
13+
--sep <value> (Default: " ", Type: string)
14+
--wrap | --no-wrap (Default: --wrap)
15+
--seed <value> (Default: NA, Type: integer)
16+

tests/testthat/test-basics.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ test_that("examples work", {
4343
)
4444

4545
expect_all_equal <- function(...) {
46-
if(...length()<2) stop("not enough args")
46+
if(...length() < 2) stop("not enough args")
4747
for(i in 2:...length()) {
4848
expect_equal(..1, ...elt(i))
4949
}
@@ -57,4 +57,8 @@ test_that("examples work", {
5757
run_app("flip-coin.R -n 3 --seed=1234")
5858
)
5959

60+
expect_snapshot(
61+
writeLines(run_app("flip-coin.R --help"))
62+
)
63+
6064
})

0 commit comments

Comments
 (0)