Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How to yield an error with zparseopts when non-matching options are seen?



On 2 Dec 2018, at 16:28, Peng Yu <pengyu.ut@xxxxxxxxx> wrote:
>In the following example, -b has not been defined as an option, but
>`zparseopts` return successfully. Is there a way to let it yield an
>error when it sees such a case?

Not that i know of. If it didn't swallow up `--` you could check to see if the
first element remaining in argv is any other string beginning with a hyphen, but
since it does you can only rely on that method when no other argument to your
function/script can ever start with a hyphen:

  % myfunc() {
    local -a opts
    zparseopts -D -a opts a b c
    print -r - $? / $opts / $@
  }
  % myfunc -a -b -foo
  0 / -a -b / -foo
  % myfunc -a -b -- -foo
  0 / -a -b / -foo

If you're certain that that's the case (e.g., the first operand must be a digit
or an absolute path or something), then just abort if `[[ $1 == -* ]]`

dana



Messages sorted by: Reverse Date, Date, Thread, Author