trevorld/r-optparse

help requested behaviour of parser_args doesn't match behaviour in README

alexpenson opened this issue · 2 comments

suppressPackageStartupMessages(library("optparse"))
option_list <- list(
  make_option(c("-v", "--verbose"), action="store_true", default=TRUE,
              help="Print extra output [default]"),
  make_option(c("-q", "--quietly"), action="store_false",
              dest="verbose", help="Print little output"),
  make_option(c("-c", "--count"), type="integer", default=5,
              help="Number of random normals to generate [default %default]",
              metavar="number")
)
parser <- OptionParser(option_list=option_list)
parse_args(
  parser,
  args = c("--verbose", "--count=11"))
parse_args(parser, args = c("--help"))
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS release 6.9 (Final)

Matrix products: default
BLAS: /opt/common/CentOS_6-dev/R/R-3.5.0/lib64/R/lib/libRblas.so
LAPACK: /opt/common/CentOS_6-dev/R/R-3.5.0/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] optparse_1.6.0

loaded via a namespace (and not attached):
[1] compiler_3.5.0 getopt_1.20.2

What exactly is the issue? This is the expected behaviour assuming interactive() is TRUE and you didn't manually set the print_help_and_exit argument of parse_args to FALSE. If you didn't manually set print_help_and_exit to FALSE and interactive() is FALSE it will instead call quit instead of throwing an error. If print_help_and_exit is manually set to FALSE if won't print out a help message nor throw an error/quit. optparse is intended primarily for writing Rscripts where printing a usage message and then killing scripts early if a help flag is observed is the desired behaviour of 99% of users but perhaps you might be interested instead of manually setting print_help_and_exit to FALSE and then manually calling the print_help function and then manually calling quit / killing the script early yourself instead of having parse_args doing that for you.

> print_help(parser)
Usage: %prog [options]


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

	-q, --quietly
		Print little output

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

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

Although the behaviour is correct the documentation in the README does not fully match the behaviour and needs to be updated to match the behaviour.