Skip to content

Commit

Permalink
Fixed arguments which embed a space (issue #11, thanks to @jennybc)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwindj committed Jun 10, 2015
1 parent e574924 commit 9beb739
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: docopt
Type: Package
Title: Command-Line Interface Specification Language
Version: 0.4.3.3
Version: 0.4.4
Date: 2015-05-03
Author: Edwin de Jonge ([email protected])
Maintainer: Edwin de Jonge <[email protected]>
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
version 0.4.4
- Fixed arguments which embed a space (issue #11, thanks to @jennybc)

version 0.4.3.1
- Fixed stringr dependency thanks to (@eddelbuettel)

Expand Down
13 changes: 12 additions & 1 deletion R/docopt.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ docopt <- function( doc, args=commandArgs(TRUE), name=NULL, help=TRUE, version=N
if (exists("argv", where = .GlobalEnv, inherits = FALSE)) {
args = get("argv", envir = .GlobalEnv);
}

# quote arguments containing a space. commandArgs removes quotes!
if (length(args) > 1) {
args <- quote_spaced(args)
}

args <- str_c(args, collapse=" ")
usage <- printable_usage(doc, name)
pot_options <- parse_doc_options(doc)
Expand Down Expand Up @@ -139,6 +143,13 @@ formal_usage <- function(printable_usage){
formal <- str_c(tail(pu, -1), collapse=" ")
formal
}

quote_spaced <- function(x){
ifelse( str_detect(x, "\\s")
, sQuote(x)
, x
)
}
#
# class Dict extends Object
#
Expand Down
23 changes: 23 additions & 0 deletions issues/11.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/Rscript

#suppressPackageStartupMessages(library(docopt))
devtools::load_all("..")

"docopt practice script
Usage: foo.R [-i <integers>]
Options:
-i <integers>, --integers=<integers> Integers [default: 1]
" -> doc

"%||%" <- function(a, b) if (!is.null(a)) a else b

opts <- docopt(doc)
my_ints <- opts$integers %||% opts$i
my_ints <- as.integer(eval(parse(text = my_ints)))
cat(sprintf("integers = %s\n", paste(my_ints, collapse = ", ")))


# fails with:
# Rscript 11.r -i 'c(1, 8)'
12 changes: 12 additions & 0 deletions tests/testthat/test_issues.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ Options:
" -> doc
opt <- docopt(doc, "-r repo1 -r repo2")
expect_equal(opt$r, c("repo1", "repo2"))
})


test_that("strings containing spaces are passed correctly (#11)", {
"
Usage: foo.R [-i <integers>]
Options:
-i <integers>, --integers=<integers> Integers [default: 1]
" -> doc
opt <- docopt(doc, "-i ' c(1, 8)'")

})
31 changes: 31 additions & 0 deletions tests/testthat/test_quotedargs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
context("Issues")

test_that("quoted arguments work (#2)", {
doc <- "
Usage:
exampleScript <arg1>
"
docopt(doc, "\"quoted arg\"")
})


test_that("quoted arguments work (#4)", {
doc <- 'Usage: do.R <dirname> [<other>...]'
opt <- docopt(doc, "some_directory '--quoted test'", quoted_args=T
)

expect_equal(opt$other, "--quoted test")

})

test_that("multivalued options work (#8)",{
"
Usage:
install.r [-r repo]...
Options:
-r=repo Repository
" -> doc
opt <- docopt(doc, "-r repo1 -r repo2")
expect_equal(opt$r, c("repo1", "repo2"))
})

0 comments on commit 9beb739

Please sign in to comment.