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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
# argparse: Command line optional and positional argument parser
[](https://cran.r-project.org/package=argparse)
[](https://github.com/trevorld/r-argparse/actions)
[](https://codecov.io/github/trevorld/r-argparse?branch=master)
[](https://cran.r-project.org/package=argparse)
<img src="man/figures/logo.png" align="right" width="200px" alt="argparse hex sticker">
`argparse` is an R package which provides a command line parser to be
used with Rscript to write "#!" shebang scripts that gracefully accept
positional and optional arguments and automatically generate usage.
To install the latest version released on CRAN use the following
command:
> install.packages("argparse")
To install the development version use the following command:
> remotes::install_github("trevorld/r-argparse")
## dependencies
The package has a Python dependency. It is easily satisfied if you have
Python (version 3.2 or higher) on your PATH. Read the INSTALL file for
more information if this doesn't describe you.
Additionally this package depends on the R packages `R6`, `findpython`,
and `jsonlite`.
To run the unit tests you will need the suggested R package `testthat`
and in order to build the vignette you will need the suggested R package
`knitr` which in turn probably requires the system tool `pandoc`:
sudo apt install pandoc
## examples
> library("argparse")
> parser <- ArgumentParser(description='Process some integers')
> parser$add_argument('integers', metavar='N', type="integer", nargs='+',
+ help='an integer for the accumulator')
> parser$add_argument('--sum', dest='accumulate', action='store_const',
+ const='sum', default='max',
+ help='sum the integers (default: find the max)')
> parser$print_help()
usage: PROGRAM [-h] [--sum] N [N ...]
Process some integers
positional arguments:
N an integer for the accumulator
optional arguments:
-h, --help show this help message and exit
--sum sum the integers (default: find the max)
Default args for `ArgumentParser()$parse_args` are `commandArgs(TRUE)`
which is what you'd want for an Rscript but not for interactive use:
> args <- parser$parse_args(c("--sum", "1", "2", "3"))
> accumulate_fn <- get(args$accumulate)
> print(accumulate_fn(args$integers))
[1] 6
Beginning with version 2.0 `argparse` also supports argument groups:
> parser = ArgumentParser(prog='PROG', add_help=FALSE)
> group1 = parser$add_argument_group('group1', 'group1 description')
> group1$add_argument('foo', help='foo help')
> group2 = parser$add_argument_group('group2', 'group2 description')
> group2$add_argument('--bar', help='bar help')
> parser$print_help()
usage: PROG [-h] [--bar BAR] foo
optional arguments:
-h, --help show this help message and exit
group1:
group1 description
foo foo help
group2:
group2 description
--bar BAR bar help
as well as mutually exclusive groups:
> parser = ArgumentParser(prog='PROG')
> group = parser$add_mutually_exclusive_group()
> group$add_argument('--foo', action='store_true')
> group$add_argument('--bar', action='store_false')
> parser$parse_args('--foo')
$bar
[1] TRUE
$foo
[1] TRUE
> parser$parse_args('--bar')
$bar
[1] FALSE
$foo
[1] FALSE
> parser$parse_args(c('--foo', '--bar'))
Error in "argparse::parse_args_output(output)" : parse error:
usage: PROG [-h] [--foo | --bar]
PROG: error: argument --bar: not allowed with argument --foo
and even basic support for sub-commands!:
> # create the top-level parser
> parser = ArgumentParser(prog='PROG')
> parser$add_argument('--foo', action='store_true', help='foo help')
> subparsers = parser$add_subparsers(help='sub-command help')
> # create the parser for the "a" command
> parser_a = subparsers$add_parser('a', help='a help')
> parser_a$add_argument('bar', type='integer', help='bar help')
> # create the parser for the "b" command
> parser_b = subparsers$add_parser('b', help='b help')
> parser_b$add_argument('--baz', choices='XYZ', help='baz help')
> # parse some argument lists
> parser$parse_args(c('a', '12'))
$bar
[1] 12
$foo
[1] FALSE
> parser$parse_args(c('--foo', 'b', '--baz', 'Z'))
$baz
[1] "Z"
$foo
[1] TRUE
> parser$print_help()
usage: PROG [-h] [--foo] {a,b} ...
positional arguments:
{a,b} sub-command help
a a help
b b help
optional arguments:
-h, --help show this help message and exit
--foo foo help
> parser_a$print_help()
usage: PROG a [-h] bar
positional arguments:
bar bar help
optional arguments:
-h, --help show this help message and exit
> parser_b$print_help()
usage: PROG b [-h] [--baz {X,Y,Z}]
optional arguments:
-h, --help show this help message and exit
--baz {X,Y,Z} baz help
|