MyNixOS website logo
Description

Command Line Option Parser.

A command line parser inspired by Python's 'optparse' library to be used with Rscript to write "#!" shebang scripts that accept short and long flag/options.

optparse: Command line optional argument parser

CRAN Status Badge R-CMD-check Coverage Status RStudio CRAN mirror downloads Dependencies

optparse hex sticker

A pure R language command line parser inspired by Python's optparse library to be used with Rscript to write #! shebang scripts that accept short and long flag/options.

To install the last version released on CRAN use the following command:

install.packages("optparse")

To install the development version use the following command:

install.packages("remotes")
remotes::install_github("trevorld/r-optparse")

examples

A simple example:

library("optparse")
parser <- OptionParser() |>
    add_option(c("-v", "--verbose"), action = "store_true",
               default = TRUE, help = "Print extra output [default]") |>
    add_option(c("-q", "--quietly"), action = "store_false",
               dest = "verbose", help = "Print little output") |>
    add_option(c("-c", "--count"), type = "integer", default = 5,
               help = "Number of random normals to generate [default %default]",
               metavar = "number")
parse_args(parser, args = c("--quietly", "--count=15"))

## $help
## [1] FALSE
## 
## $verbose
## [1] FALSE
## 
## $count
## [1] 15

Note that the args argument of parse_args() default is commandArgs(trailing=TRUE) so it typically doesn't need to be explicitly set if writing an Rscript.

optparse automatically creates a help option:

parse_args(parser, args = c("--help"))

Usage: %prog [options]


Options:
    -h, --help
        Show this help message and exit

    -v, --verbose
        Print extra output [default]

    -q, --quietly
        Print little output

    -c NUMBER, --count=NUMBER
        Number of random normals to generate [default 5]


Error in parse_args(parser, args = c("--help")) : help requested

Note by default when optparse::parse_args() sees a --help flag it will first print out a usage message and then either throw an error in interactive use or call quit() in non-interactive use (i.e. when used within an Rscript called by a shell). To disable the error/quit set print_help_and_exit = FALSE in parse_args() and to simply print out the usage string one can also use the function print_usage().

optparse has limited positional argument support, other command-line parsers for R such as argparse have richer positional argument support:

parse_args(parser, args = c("-vc", "25", "75", "22"), positional_arguments = TRUE)

## $options
## $options$help
## [1] FALSE
## 
## $options$verbose
## [1] TRUE
## 
## $options$count
## [1] 25
## 
## 
## $args
## [1] "75" "22"

The function parse_args2 wraps parse_args while setting positional_arguments=TRUE and convert_hyphens_to_underscores=TRUE:

parse_args2(parser, args = c("-vc", "25", "75", "22"))

## $options
## $options$help
## [1] FALSE
## 
## $options$verbose
## [1] TRUE
## 
## $options$count
## [1] 25
## 
## 
## $args
## [1] "75" "22"

other R packages

  • When optparse was originally written in 2009 the only option parsing package on CRAN was the comparatively low-level getopt package but as of 2026 there are at least 13 R R Argument/Option Parser Packages.
  • Personally I use optparse for most scripts I write:
    • It supports the most important features in an argument/option parsing packages with a reasonably high-level interface.
    • It is well-tested with high code coverage and lots of users with millions of cumulative downloads over the past 15+ years.
    • It has no dependencies.
    • It features a stable API with a semantic version number greater than 1.0.
  • Occasionally I'll also use the argparse package when I need advanced features unsupported by optparse like sub parsers and named positional arguments:
    • In general argparse supports more advanced features than optparse but depends on Python and three R packages whereas optparse is a pure R package with no dependencies.
    • However, unlike argparse, optparse does support a callback action and also allows writing custom help usage formatters.
Metadata

Version

1.8.2

License

Unknown

Platforms (80)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arc-linux
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • sh4-linux
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows