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
|
---
title: "argparse Command Line Argument Parsing"
output: rmarkdown::html_vignette
vignette: >
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{argparse Command Line Argument Parsing}
%\VignetteEncoding{UTF-8}
---
argparse is a command line argument parser inspired by Python's "argparse" library. Use this with Rscript to write "#!"-shebang scripts that accept short and long flags/options and positional arguments, generate a usage statement, and set default values for options that are not specified on the command line.
```{r echo=FALSE}
library("knitr")
rscript_executable <- paste(file.path(R.home(), "bin", "Rscript"), "--vanilla")
opts_knit$set(root.dir = system.file("exec", package = "argparse")) # to access the "Rscript files"
opts_chunk$set(comment = NA, echo = FALSE)
list_file_command <- "ls"
chmod_command <- "chmod ug+x display_file.R example.R"
path_command <- "export PATH=$PATH:`pwd`"
run_command <- function(string) suppressWarnings(cat(system(string, intern = TRUE), sep = "\n"))
```
In our working directory we have two example R scripts, named "example.R" and "display_file.R" illustrating the use of the argparse package.
**`r paste("bash$", list_file_command)`**
```{r}
run_command(sprintf("%s", list_file_command))
command <- "display_file.R example.R" # to show file
```
In order for a \*nix system to recognize a "\#!"-shebang line you need to mark the file executable with the ``chmod`` command, it also helps to add the directory containing your Rscripts to your path:
**`r paste("bash$", chmod_command)`**
**`r paste("bash$", command)`**
Here is what "example.R" contains:
**`r paste("bash$", command)`**
```{r}
run_command(sprintf("%s %s 2>&1", rscript_executable, command))
command <- "example.R --help" # same as system("Rscript example.R -h")
```
By default ``argparse`` will generate a help message if it encounters ``--help`` or ``-h`` on the command line. Note how ``%(default)s`` in the example program was replaced by the actual default values in the help statement that ``argparse`` generated.
**`r paste("bash$", command)`**
```{r}
run_command(sprintf("%s %s 2>&1", rscript_executable, command))
command <- "example.R" # rely only on defaults
```
If you specify default values when creating your ``ArgumentParser`` then ``argparse`` will use them as expected.
**`r paste("bash$", command)`**
```{r}
run_command(sprintf("%s %s 2>&1", rscript_executable, command))
command <- "example.R --mean=10 --sd=10 --count=3"
```
Or you can specify your own values.
**`r paste("bash$", command)`**
```{r}
run_command(sprintf("%s %s 2>&1", rscript_executable, command))
command <- "example.R --quiet -c 4 --generator=\"runif\"" # same as above but "quiet"
```
If you remember from the example program that ``--quiet`` had ``action="store_false"`` and
``dest="verbose"``. This means that ``--quiet`` is a switch that turns the ``verbose`` option from its default value of ``TRUE`` to ``FALSE``. Note how the ``verbose`` and ``quiet`` options store their value in the exact same variable.
**`r paste("bash$", command)`**
```{r}
run_command(sprintf("%s %s 2>&1", rscript_executable, command))
command <- "example.R --silent -m 5" # same as above but "quiet"
```
If you specify an illegal flag then \emph{argparse} will print out a usage message and an error message and quit.
**`r paste("bash$", command)`**
```{r}
run_command(sprintf("%s %s 2>&1", rscript_executable, command))
command <- "example.R -c 100 -c 2 -c 1000 -c 7" # same as above but "quiet"
```
If you specify the same option multiple times then \emph{argparse} will use the value of the last option specified.
**`r paste("bash$", command)`**
```{r}
run_command(sprintf("%s %s 2>&1", rscript_executable, command))
command <- "display_file.R --help"
```
``argparse`` can also parse positional arguments. Below we give an example program ``display_file.R``, which is a program that prints out the contents of a single file (the required positional argument, not an optional argument) and which accepts the normal help option as well as an option to add line numbers to the output.
**`r paste("bash$", command)`**
```{r}
run_command(sprintf("%s %s 2>&1", rscript_executable, command))
command <- "display_file.R --add_numbers display_file.R"
```
**`r paste("bash$", command)`**
```{r}
run_command(sprintf("%s %s 2>&1", rscript_executable, command))
command <- "display_file.R non_existent_file.txt"
```
**`r paste("bash$", command)`**
```{r}
run_command(sprintf("%s %s 2>&1", rscript_executable, command))
command <- "display_file.R"
```
**`r paste("bash$", command)`**
```{r}
run_command(sprintf("%s %s 2>&1", rscript_executable, command))
```
|