-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
146 lines (112 loc) · 4.1 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup}
#| include: false
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# scribe
<!-- badges: start -->
[![R-CMD-check](https://github.com/jmbarbone/scribe/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/jmbarbone/scribe/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/jmbarbone/scribe/branch/main/graph/badge.svg)](https://app.codecov.io/gh/jmbarbone/scribe?branch=main)
<!-- badges: end -->
The goal of scribe is to provide a detailed argument parser for `Rscript`.
This package contains no dependencies outside of `base` and `methods`.
The core functions utilize `ReferenceClasses` under the hood.
```{r library}
library(scribe)
```
Install `{scribe}` from CRAN with:
``` r
install.packages("scribe")
```
Alternatively, you can install the development version of `{scribe}` [GitHub](https://github.com/) with:
```
# install.packages("devtools")
devtools::install_github("jmbarbone/scribe")
```
You can enter command arguments as a vector to test out the behavior.
Arguments can be added to the ``r class(command_args())`` class (here as `ca`).
Default behavior tries to parse objects but additional control can be taken.
```{r example-simple}
ca <- command_args(c("-a", "1", "-b", "2"))
ca$add_argument("-a")
ca$add_argument("-b")
args <- ca$parse()
str(args$a + args$b)
```
Control
```{r example-controls}
# don't convert numbers
ca <- command_args(c("-a", "1", "-b", "1.0"))
ca$add_argument("-a", convert = as.character)
ca$add_argument("-b", convert = as.character)
ca$parse()
# convert numbers to integers
ca <- command_args(c("verbose", "1", "1.5", "1.9"))
ca$add_argument("verbose", action = "flag")
ca$add_argument("...", convert = as.integer)
ca$parse()
# use functions for more control
ca <- command_args(c("verbose", "12-9-2022", "12-10-2022"))
ca$add_argument("verbose", action = "flag")
ca$add_argument("...", convert = function(i) as.Date(i, "%m-%d-%Y"))
ca$parse()
```
You'll probably use `{scribe}` within small scripts that can be called from your favorite terminal.
The example below uses a function to call this file, but if it is added to your `PATH` you'd be able to call it directly:
``` bash
r-file -a 1 -b 9
```
```{r example-script}
lines <- "
#! /usr/bin/env -S Rscript --vanilla
library(scribe)
ca <- scribe::command_args()
ca$add_argument('-a', default = 1)
ca$add_argument('-b', default = 2)
args <- ca$parse()
foo <- function(a, b, ...) {
a + b
}
do.call(foo, args)
"
file <- tempfile()
writeLines(lines, file)
rscript <- function(x, args = character()) {
args <- c("--vanilla", x, args)
res <- system2("Rscript", args, stdout = TRUE)
writeLines(res)
}
rscript(file)
rscript(file, "-a 0")
rscript(file, "-a 0 -b 10")
```
```{r remove}
#| include: false
file.remove(file)
```
## Examples
I've been using `{scribe}` for some personal scripts.
Below is a short list of some examples (mostly in my [`jmb`](https://github.com/jmbarbone/jmb) repo):
- [pak](https://github.com/jmbarbone/jmb/blob/main/bin/pak): call [`{pak}`](https://pak.r-lib.org/) from your terminal
- [update-r-pkgs](https://github.com/jmbarbone/jmb/blob/main/bin/update-r-pkgs): update old R packages
- [todos](https://github.com/jmbarbone/jmb/blob/main/bin/todos): calls [`mark::todos()`](https://jmbarbone.github.io/mark/reference/todos.html)
- [fixmes](https://github.com/jmbarbone/jmb/blob/main/bin/fixmes): calls [`mark::fixmes()`](https://jmbarbone.github.io/mark/reference/todos.html)
## Other packages
This isn't the first package.
Most contain other dependencies, some even in different languages (e.g., `python`).
- [`{argparse}`](https://github.com/trevorld/r-argparse)
- [`{optparse}`](https://github.com/trevorld/r-optparse)
- [`{getopt}`](https://github.com/trevorld/r-getopt)
- [`{minimist}`](https://github.com/jeroen/minimist) (CRAN archived)
- [`{optigrab}`](https://github.com/decisionpatterns/optigrab)
- [`{docopt}`](https://github.com/docopt/docopt.R)