trevorld/r-optparse

No error/warning about "no such option" when 'positional_arguments == TRUE'

greg-minshall opened this issue · 4 comments

hi. thanks for doing these. (as my sister says, "no good deed goes unpunished".)

i may be missing something that's obvious, but in my simple attempt, i don't see optparse complaining about invalid options. i.e., i would think the following would give an error:

  require(optparse)
  options <- list(
    make_option(c("-v", "--verbose"), action="store_true", default=TRUE,
        help="Print extra output [default]")
  )
  p <- OptionParser(option_list=options)
  ## i would think this would fail with s.th. like "unrecognized option"
  args <- parse_args2(p, args=c("-v", "-x", "--foo", "this", "that", "the other"))

the unix shell command, and, lazy that i am to check, i think getopt(3), etc., do:

{1001} getopt abc -x
getopt: invalid option -- 'x'
 --

again, thanks, and sorry if i'm missing the blindingly obvious.

(sorry about the weird formatting.)

I can confirm that currently the optparse package does not throw a warning/error for "invalid options" when positional_arguments == TRUE (as is the case for parse_args2).

I played around with with Python's optparse module and it seems that it does throw an error in such a circumstance with the message "no such option:":

~/tmp$ python test.py -v foo -x
Usage: test.py [options]

test.py: error: no such option: -x

Throwing an error might be a non-reverse-compatible change for certain users but perhaps I should at least try to detect such situations and at least throw a warning instead of simply interpreting them as positional arguments.

Trevor, thanks for the reply. maybe a flag to OptionParser() or parse_args() that defaults to the current behavior (with or without a warning), but allows one to choose to have an error thrown if desired?

thanks!