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

Re: How to handle unknown options in zparseopts

On Aug 31,  5:44pm, DervishD wrote:
> zparseopts -D -A OPTIONS -- -help+
> tmp=$argv[1]
> [[ "$tmp[1]" == "-" ]] && {
>     print "ERROR, unknown option \"$tmp\""
>     return 1
> }
>     That's pretty ugly and not very clean, neither :(

Why are you messing around with the tmp variable?

    zparseopts -D -A OPTIONS -- -help+
    [[ $1 = -* ]] && {
	print -u2 "ERROR, unknown option \"$1\""
	return 1

>     But, worse, zparseopts spits an error in this case:
>     set -- -a
>     zparseopts -a array -- a:

    zparseopts -- a::=array
    (( $#array == 1 )) && {
	print -u2 "ERROR, required argument of \"-a\" is missing"
	return 1

When you use 'a:' you tell zparseopts that *it* should require an
argument; but what you tell zparseopts doesn't have to literally
reflect whether *you* require one.

However, that does mean that the argument must be given in the same
word as the option (e.g. "-axyz" rather than "-a xyz") so that may
not fit your needs.  There are two drawbacks to zparseopts; that's
one of them, and the other is that it doesn't differentiate empty
strings from omitted arguments very effectively.

You might want to try a hybrid approach: Call zparseopts -D -E to
strip all the long options, then use getopts to process the rest of
them and issue error messages.  However, that means that the order of
the options on the command line must not matter.

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