Skip to contents

optparse 1.9.1

CRAN release: 2026-12-01

New features

  • rscript_filename() is a new function that returns the file name of the calling script. Its quote argument controls whether the file name is always quoted with shQuote(), only quoted if needed with shell_quote() (the default), or never quoted (#64).
  • shell_quote() is a new function that quotes strings with shQuote() but only if they contain spaces or other characters that may need quoting when passed to a shell. Its default type is "cmd" on Windows unless run from Git Bash, MSYS2, or Cygwin (detected via the MSYSTEM and SHELL environment variables) and "sh" otherwise.

Bug fixes and minor improvements

  • OptionParser() now quotes the automatically detected script file name if needed (using shell_quote()) before substituting it for %prog in the usage message. A user supplied prog is left as is. Thanks Zivan Karaman (@zivankaraman) for suggestion (#62). OptionParser() also no longer doubles the backslashes in the script file name on Windows.
  • OptionParser() now stores the program name in a new prog slot of the OptionParser class. The error messages parse_args() prints in non-interactive sessions now use it, so they match the usage message (including a user supplied prog) and no longer begin with NA: error: when there is no script file name.
  • parse_args() (and parse_args2()) now consistently return NA for option values that can’t be converted to the option’s type. Previously some invalid values (e.g. --count foo for an integer option) were returned unconverted as character strings, and for action = "append" every value (even the valid ones) was left unconverted. The warning now only lists the values that failed to convert.

optparse 1.8.2

CRAN release: 2026-04-17

New features

  • add_option() (and make_option()) now support the following new actions (#22):

    • action = "append" which appends each occurrence’s value to default.
    • action = "append_const" which appends const to default each time the flag is seen.
    • action = "count", which counts the number of times a flag is seen and adds it to default (treated as 0L if not supplied). Returns NULL if the flag is never seen and no default was supplied.
    • action = "store_const" which stores const when the flag is seen.
  • add_option() (and make_option()) now support a const parameter (intended for new actions "append_const" and "store_const").

  • Several new classed errors are now thrown, mirroring Python’s optparse exception hierarchy:

    • optparse_option_error: invalid option definition in make_option() or add_option().
    • optparse_parse_error: existing base class for all parse-time errors from parse_args().
      • optparse_bad_option_error: unrecognized, misused, or argument-requiring option.
        • optparse_ambiguous_option_error: ambiguous abbreviated long flag.
      • optparse_bad_positional_arguments_error: wrong number of positional arguments supplied.
      • optparse_missing_required_error: a required option was not supplied.
  • add_option() (and make_option()) now allow non-letter characters in short flags (e.g., -1) and at the beginning of long flags (e.g., --1flag), following Python’s optparse convention. However long and short flags may not contain = or whitespace (or begin with hyphens).

  • add_option() (and make_option()) now support a required argument. If TRUE, parse_args() will throw an error if the option is not provided on the command line (#17).

Bug fixes and minor improvements

  • parse_args() (and parse_args2()) now correctly handle a bare -- separator: all arguments after -- are treated as positional arguments rather than options.
  • parse_args() (and parse_args2()) now support abbreviated long flags (e.g. --verb matching --verbose) when positional_arguments is not FALSE. Previously abbreviations were only supported when positional_arguments = FALSE.
  • make_option() now throws a classed optparse_option_error for invalid action or type arguments instead of a confusing error later at parse time.
  • OptionParser() now also checks the LITTLER_SCRIPT_PATH environment variable for the script name when performing %prog on the usage argument so it works when called from littler.

optparse 1.7.5

CRAN release: 2024-04-16

  • We no longer coerce the type of an option default to match that of its type argument when action = "callback". Thanks husheng (@hs3434) for bug report (#47).

optparse 1.7.4

CRAN release: 2024-01-16

  • Revises vignette engine specification in the DESCRIPTION to suppress new CRAN check NOTE (#43).

optparse 1.7.3

CRAN release: 2022-07-20

  • The errors raised by parse_args() (and parse_args2()) are now of class “optparse_parse_error”.

    When interactive() is FALSE we now print out a usage string followed by a (less verbose) error message.

  • Throws a more informative error message for unknown short flags when positional_arguments=TRUE. Thanks Greg Minshall for bug report (#42).

optparse 1.7.1

CRAN release: 2021-10-08

optparse 1.6.6

CRAN release: 2020-04-16

  • Throws an error for unknown short flags and long flags when positional_arguments=TRUE. Thanks Greg Minshall for bug report (#34).
  • If the callback parameter of add_option / make_option is not NULL then the default of action is now "callback". Thanks Greg Minshall for suggestion (#35).
  • Better documentation of action=="callback" in the man page for add_option and make_option. Thanks Greg Minshall for bug report (#35).

optparse 1.6.4

CRAN release: 2019-09-16

  • Fixes bug in printing help for action=="callback" when metavar==NULL (#29). Thanks Ni Huang for bug report.
  • Throws an error when passed short flags more than one letter long (#32). Thanks Gautier Richard for bug report.

optparse 1.6.2

CRAN release: 2019-04-02

  • Fixs a parsing bug when action=="callback" and positional_argument==TRUE (#28). Thanks Ni Huang for bug report.

optparse 1.6.1

CRAN release: 2019-01-15

  • Improves accuracy of README (#27). Thanks Alex Penson for bug report.

optparse 1.6.0

CRAN release: 2018-06-17

  • Supports callback actions (#26). Thanks Gyu Jin Choi for patch.
  • If interactive() == FALSE and print_help_and_exit == TRUE then optparse will now call quit(status=0) instead of quit(status=1) after printing the help message (this matches behaviour of the python package).
  • Better error message when forgetting to set add_help_option=FALSE when defining an -h or --help flag. Thanks Jeff P. Bruce for bug report.
  • Increment getopt requirement so that empty strings are parsed correctly. Thanks Matthew Flickinger for bug report.

optparse 1.4.4

CRAN release: 2017-07-18

  • Minor documentation fixes. Thanks J. J. Ramsey and Daeyoung Kim for bug reports.
  • Fix bug when add_help_option in OptionParser set to FALSE. Thanks to Jeff Bruce for bug report.
  • parse_args now supports convert_hyphens_to_underscores argument which converts any hyphens to underscores when returning the list of options
  • Now includes the convenience function parse_args2 which wraps parse_args with positional_arguments set to TRUE and convert_hyphens_to_underscores set to TRUE.

optparse 1.3.2

CRAN release: 2015-10-03

  • optparse should no longer give any warnings when options(warnPartialMatchArgs=TRUE). Thanks Rich FitzJohn for patch.
  • print_help no longer throws an error if we have a default argument of length zero. Thanks Benjamin Tyner for bug report.

optparse 1.3.0

CRAN release: 2014-12-11

  • OptionParser and OptionParserOption S4 classes are now exported. Thanks Peter Humburg for patch.

optparse 1.2.0

CRAN release: 2014-04-04

  • Parameter positional_arguments of function parse_args now accepts one or two numeric values that denote the minimum and maximum number of supported positional arguments. Thanks Kirill Müller for patch.
  • If interactive() == TRUE then parse_args will no longer quit(status=1) after printing a help message but will instead throw an error. optparse will continue to quit(status=1) after printing a help message for non-interactive Rscripts unless print_help_and_exit == FALSE.
  • In make_option argument type="numeric" automatically cast to double. Previously users might have received an error passing negative numbers if they accidentally specified “numeric” instead of “double”.
  • Bug fixed in printing usage message for options with default value of NA and a help string including “%default”. Thanks Stefan Seemayer for bug report and patch.

optparse 1.0.2

CRAN release: 2013-08-26

  • Project website moved to https://github.com/trevorld/optparse
  • We now replace all occurrences of %prog in usage message (including description and epilogue). Previously we would only replace one occurrence and didn’t make replacements in description and epilogue.
  • Fix bug in parse_args when we have options with no short flag and positional_arguments=TRUE. Thanks Miroslav Posta for bug report.

optparse 1.0.0

CRAN release: 2012-11-21

  • Added description and epilogue arguments to OptionParser to allow users to add more information to generated help messages
  • Slightly alters the generated usage string to match more closely what the Python module does
  • No longer exports S4 classes that represent OptionParser and OptionParserOption
  • Now requires package getopt (>= 1.19) which has also been moved to Imports field from Depends field in DESCRIPTION
  • Now also Suggests stringr package in DESCRIPTION