Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

78 scribe execute #79

Merged
merged 30 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b3666a5
bump version
jmbarbone Apr 13, 2024
1f2c3a6
check type for evaluate
jmbarbone Apr 13, 2024
9099866
add tests for convert
jmbarbone Apr 13, 2024
8a40535
Merge branch 'main' into dev
jmbarbone May 20, 2024
59fba6d
bump version
jmbarbone May 25, 2024
f1edef5
check type for evaluate
jmbarbone May 25, 2024
dfcbb40
add tests for convert
jmbarbone Apr 13, 2024
ed4a64f
align command arg method args (#78)
jmbarbone May 25, 2024
b84271a
(#78)
jmbarbone May 25, 2024
173b141
add method checking function for testing (#78)
jmbarbone May 25, 2024
90c99cd
add news point (#78)
jmbarbone May 25, 2024
2a413d4
merge
jmbarbone May 25, 2024
543292f
ignore .git (#78)
jmbarbone May 25, 2024
d3bbe63
update wordlist (#78)
jmbarbone May 25, 2024
068a7f2
(#78)
jmbarbone May 25, 2024
7511ea6
limit installations (#78)
jmbarbone May 25, 2024
94323ec
bump version (#78)
jmbarbone May 25, 2024
83583c0
(#78)
jmbarbone May 27, 2024
da16c90
Merge branch 'scribe-execute' of https://github.com/jmbarbone/scribe …
jmbarbone Jun 2, 2024
36af550
add installation function
jmbarbone Jun 2, 2024
506baee
remove included args in execute
jmbarbone Jun 2, 2024
faf8a1b
assign values by arg order
jmbarbone Jun 2, 2024
f237abb
include more tests for default values (#78)
jmbarbone Jun 2, 2024
cfd4749
Merge branch 'dev' into 78-scribe-execute
jmbarbone Jun 2, 2024
d92871f
use installed version of scribe for test scripts (#78)
jmbarbone Jun 2, 2024
73eebef
simplify value storage (#78)
jmbarbone Jun 2, 2024
c61cbc9
delint (#78)
jmbarbone Jun 2, 2024
110d60b
add news item (#78)
jmbarbone Jun 3, 2024
3eaa494
remove ca_remove_arg() (#78)
jmbarbone Jun 3, 2024
1a79afe
test coverage (#78)
jmbarbone Jun 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
^cran-comments\.md$
^CRAN-SUBMISSION$
^revdep$
^\.git$
9 changes: 9 additions & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

.InstallScribe <- function() {
devtools::document()
pak::pkg_install("local::.", Sys.getenv("R_LIBS_SCRIBE"))
}

if (file.exists("~/.Rprofile")) {
source("~/.Rprofile")
}
3 changes: 2 additions & 1 deletion .github/workflows/r-check-verison.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ jobs:

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::gh, any::fuj
packages: any::gh, any::fuj
pandoc-install: false

- uses: jmbarbone/actions/r-check-version@main
with:
Expand Down
25 changes: 0 additions & 25 deletions .github/workflows/r-check-version.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: scribe
Title: Command Argument Parsing
Version: 0.3.0.9001
Version: 0.3.0.9002
Authors@R:
person(
given = "Jordan Mark",
Expand All @@ -16,7 +16,7 @@ License: MIT + file LICENSE
Encoding: UTF-8
Language: en-US
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
Depends:
R (>= 3.6)
Imports:
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ These are not meant to be user accessible.
- `new_arg()` now throws a more helpful error when _value doesn't convert to itself_
- `arg$show()` now denotes if the argument is resolves by display an `"R"` before the value
- `arg$show()` now prints values of class `"scribe_empty_value"` as `<empty>`
- `arg$add_argument()` correctly passes all method arguments [#78](https://github.com/jmbarbone/scribe/issues/78)
- `ca$parse()` correctly deals with new arguments that nave the same _name_ as ones in `included` [#80](https://github.com/jmbarbone/scribe/issues/80)

# scribe 0.3.0

Expand Down
22 changes: 13 additions & 9 deletions R/class-command-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,13 @@ scribeCommandArgs$methods(

add_argument = function(
...,
action = arg_actions(),
options = NULL,
convert = scribe_convert(),
action = arg_actions(),
default = NULL,
n = NA_integer_,
info = NULL,
convert = scribe_convert(),
n = NA_integer_,
info = NULL,
options = list(),
stop = c("none", "hard", "soft"),
execute = invisible
) {
"Add a \\link{scribeArg} to \\code{args}
Expand All @@ -220,17 +221,20 @@ scribeCommandArgs$methods(
all other arguments are ignored. Note that only the first value
(\\link{..1}) is used.}
\\item{\\code{action}, \\code{options}, \\code{convet}, \\code{default},
\\code{n}, \\code{info}}{See \\code{\\link[=new_arg]{new_arg()}}}
\\code{n}, \\code{info}, \\code{stop}, \\code{execute}}{See
\\code{\\link[=new_arg]{new_arg()}}}
}"
ca_add_argument(
self = .self,
...,
action = action,
options = options,
convert = convert,
default = default,
convert = convert,
n = n,
info = info
info = info,
options = options,
stop = stop,
execute = execute
)
},

Expand Down
60 changes: 33 additions & 27 deletions R/command-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ command_args <- function(

ca_initialize <- function(
self,
input = NULL,
input = "",
include = c("help", "version", NA_character_),
supers = include
) {
Expand Down Expand Up @@ -97,7 +97,7 @@ ca_initialize <- function(
invisible(self)
}

ca_show <- function(self, all_values = FALSE, ...) {
ca_show <- function(self, ...) {
print_line("Initial call: ", to_string(self$get_input()))

if (!self$resolved) {
Expand Down Expand Up @@ -203,28 +203,28 @@ ca_resolve <- function(self) {
# dots must always be parsed last
wapply(args, function(i) i$positional),
wapply(args, function(i) i$action == "dots"),
wapply(args, function(i) inherits(i, "scribeSuperArg"))
NULL
),
fromLast = TRUE
)

# move stops earlier
arg_order <- unique(c(
wapply(args, function(i) inherits(i, "scribeSuperArg")),
wapply(args, function(i) i$stop == "hard"),
wapply(args, function(i) i$stop == "soft"),
arg_order
))

arg_names <- vapply(args, function(arg) arg$get_name(), NA_character_)
self$field("values", structure(
vector("list", length(arg_order)),
names = arg_names[arg_order]
))

for (arg in args[arg_order]) {
self$set_values(arg$get_name(), arg_parse_value(arg, self))
arg_parse_value(arg, self)
}

self$field("values", structure(
lapply(args, function(arg) arg$get_value()),
names = vapply(args, function(arg) arg$get_name(), NA_character_)
))

if (length(ca_get_working(self)) && self$stop == "none") {
warning(
"Not all values parsed:\n",
Expand All @@ -233,7 +233,6 @@ ca_resolve <- function(self) {
)
}

self$field("values", self$values[order(arg_order)])
self$field("resolved", TRUE)
invisible(self)
}
Expand Down Expand Up @@ -272,7 +271,14 @@ ca_get_values <- function(
values <- self$values

if (!included) {
values <- values[setdiff(names(values), self$included)]
# in the event that an arg uses a name like 'version', this will only remove
# the first instance -- which should be the 'included' args. Should
# probably be searching by class instead.
m <- match(self$included, names(values), 0L)
ok <- which(m > 0L)
if (length(ok)) {
values <- values[-m[ok]]
}
}

if (!super && !is.null(names(values))) {
Expand All @@ -286,11 +292,11 @@ ca_get_values <- function(
values
}

ca_set_values <- function(self, i = NULL, value) {
ca_set_values <- function(self, i = TRUE, value) {
stopifnot(length(i) == 1)

if (is.null(value)) {
return(NULL)
return(invisible(self))
}

self$field("values", replace2(self$values, i, value))
Expand All @@ -316,13 +322,13 @@ ca_get_args <- function(self, included = TRUE, super = FALSE) {
ca_add_argument <- function(
self,
...,
n = NA_integer_,
action = NULL,
convert = scribe_convert(),
options = NULL,
action = arg_actions(),
default = NULL,
info = NULL,
stop = "none",
convert = scribe_convert(),
n = NA_integer_,
info = NULL,
options = list(),
stop = c("none", "hard", "soft"),
execute = invisible
) {
if (is_arg(..1)) {
Expand All @@ -343,13 +349,13 @@ ca_add_argument <- function(

arg <- new_arg(
aliases = aliases,
action = action,
options = options,
convert = convert,
action = action,
default = default,
info = info,
stop = stop,
n = as.integer(n),
convert = convert,
n = as.integer(n),
info = info,
options = options,
stop = stop,
execute = execute
)
}
Expand Down Expand Up @@ -391,7 +397,7 @@ ca_set_example <- function(self, x = character(), comment = "", prefix = "$ ") {
invisible(self)
}

ca_add_example <- function(self, x = NULL, comment = "", prefix = "$ ") {
ca_add_example <- function(self, x, comment = "", prefix = "$ ") {
if (is.null(x)) {
return(invisible(self))
}
Expand Down
7 changes: 6 additions & 1 deletion R/convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ scribe_convert <- function(method = c("default", "evaluate", "none")) {
method,
none = identity,
default = value_convert,
evaluate = function(x, ...) eval(str2expression(as.character(x)), baseenv())
evaluate = function(x, ...) {
if (!is.character(x)) {
return(x)
}
eval(str2expression(as.character(x)), baseenv())
}
)
}

Expand Down
44 changes: 44 additions & 0 deletions R/utils-testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' Check method arguments
#'
#' @param object Object to check
#' @param prefix Prefix to use for method names
#' @param verbose Print out additional information
#' @keywords internal
#' @noRd
check_methods <- function(object, prefix, verbose = getOption("verbose")) {
requireNamespace("testthat")

for (method in object$methods()) {
obj <- get0(method, object$def@refMethods, mode = "function")

if (is.null(obj)) {
next
}

exp <- get0(paste0(prefix, method), mode = "function")

if (is.null(exp)) {
if (verbose) {
cat("Skipping method: ", method, "()\n", sep = "") # nocov
}

next
}

obj <- as.list(formals(obj))

if (identical(obj, list())) {
# exp will be a named list, but will be empty
obj <- structure(list(), names = character())
}

exp <- as.list(formals(exp))[-1L]

testthat::expect_identical(
object = obj,
expected = exp,
info = paste0("method: ", method, "()"),
ignore_srcref = TRUE
)
}
}
11 changes: 5 additions & 6 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
CMD
Codecov
fixmes
ORCID
ReferenceClass
Rscript
arg
args
fixmes
pak
pkgs
ReferenceClass
repo
Rscript
scribeArg
scribeArgs
scribeCommandArgs
todos
8 changes: 5 additions & 3 deletions man/scribeCommandArgs-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/class-command-args.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
Code
command_args("---help")$parse()
Output
{scribe} v0.3.0.9001
{scribe} v0.3.0.9002
For more information, see https://jmbarbone.github.io/scribe/
named list()

Expand All @@ -203,6 +203,6 @@
Code
command_args("---version")$parse()
Output
0.3.0.9001
0.3.0.9002
named list()

9 changes: 8 additions & 1 deletion tests/testthat/scripts/help.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#!/usr/bin/Rscript --vanilla

library(scribe)
if (!require(
scribe,
lib.loc = Sys.getenv("R_LIBS_SCRIBE", "~/R/scribe-library")
)) {
warning("expected {scribe} to be installed at R_LIBS_SCRIBE")
library(scribe)
}

ca <- command_args()
ca$add_argument("-f", "--foo")
ca$add_argument("-b", "--bar")
Expand Down
Loading
Loading