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 18:13, dana <dana@xxxxxxx> wrote:
>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

After i hit send it occurred to me that you can deal with this if you use -E,
but you'll have to do a second pass yourself to catch any bad options that it
leaves behind:

  myfunc() {
    local i
    local -a opts

    zparseopts -D -E -a opts a b: c

    print -r "options: $opts"

    for (( i = 1; i <= $#; i++ )); do
      [[ $argv[i] == (-|--) ]] && {
        argv[i]=()
        break
      }
      [[ $argv[i] == -* ]] && {
        print -ru2 "bad option: $argv[i]"
        return 1
      }
    done

    print -r "operands: $*"
  }

I *think* that handles everything...?

dana



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