Skip to content

Commit

Permalink
Allow callback_args to be NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorld committed Jun 17, 2018
1 parent f3574a8 commit affdf3d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Encoding: UTF-8
Package: optparse
Type: Package
Title: Command Line Option Parser
Version: 1.6.0-0
Version: 1.6.0-1
Authors@R: c(person("Trevor L", "Davis", role = c("aut", "cre"), email="[email protected]"),
person("Allen", "Day", role="ctb", comment="Some documentation and examples ported from the getopt package."),
person("Python Software Foundation", role="ctb", comment="Some documentation from the optparse Python module."),
Expand Down
2 changes: 2 additions & 0 deletions R/optparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ make_option <- function(opt_str, action="store", type=NULL, dest=NULL, default=N
if(!is.null(callback_args))
warning(sprintf("callback_args argument is supplied for non-callback action"))
}
if (is.null(callback_args))
callback_args <- list()

return(new("OptionParserOption", short_flag=short_flag, long_flag=long_flag,
action=action, type=type, dest=dest, default=default,
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-optparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ test_that("callback works as expected", {
opts <- parse_args(parser2, args = c("--value=2"))
expect_equal(opts$value, 8)

parser3 <- add_option(parser0, c("-v", "--value"), type="integer",
action="callback", callback=callback_fn)
opts <- parse_args(parser3, args = c("--value=2"))
expect_equal(opts$value, 4)

expect_warning(add_option(parser0, "--warning", callback=as.list), "callback argument is supplied for non-callback action")
expect_warning(add_option(parser0, "--warning", callback_args=list(3,b=4)), "callback_args argument is supplied for non-callback action")
expect_error(add_option(parser0, "--warning", action="callback", callback="hello"), "Option type cannot be inferred")
Expand Down

0 comments on commit affdf3d

Please sign in to comment.