Wiki
Clone wikiargparser / Home
Alternative Usage
As shown in the round-magrittr.R
example script, we can use magrittr
to simplify the code. (The code is little exotic, and doing so does make the error message a little more verbose.)
#!/usr/bin/env Rscript
library(magrittr, quietly=TRUE);
library(argparser, quietly=TRUE);
# Create a parser, add arguments, and parse the command line arguments
# All steps are chained together, courtesy of magrittr
argv <- arg_parser("Round a floating point number") %>%
add_argument("number", help="number to round", type="numeric") %>%
add_argument("--digits", help="number of decimal places", default=0) %>%
parse_args();
# Do work based on the passed arguments
cat( round(argv$number, argv$digits), "\n");
The script is run in the same way as before.
$ Rscript round.R 3.141
# 3
$ Rscript round.R 3.141 -d 2
# 3.14
See the magrittr
vignette for more information.
Updated