Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Optimal use of zparseopts
- X-seq: zsh-users 8681
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxxxxx
- Subject: Re: Optimal use of zparseopts
- Date: Fri, 15 Apr 2005 03:11:58 +0000
- In-reply-to: <20050415014635.GA7437@xxxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <20050415014635.GA7437@xxxxxxxxxx>
On Apr 15, 3:46am, Mariusz Gniazdowski wrote:
}
} I'm trying to use zparseopts but it can be done in many ways.
} Which is the most optimal?
As usual, it depends on what you're eventually going to do with the
values you've parsed.
One use of zparseopts is in a wrapper function, where the only purpose
is to consume the options and arguments that actually belong to some
*other* program, and store them until that other program is called. In
this case you don't care what you parsed, only that you can reguritate
it at need, so tossing them into a single array may be adequate. The
completion system uses zparseopts this way a lot.
Another use is to parse options that are actually meaninful to the
function that calls zparseopts. In this case you're more likely to want
either an associative array, or one array per option, or a mixture.
Still another use is to throw away options that you're not interested in,
perhaps because you've already examined them some other way. That's what
the -D option is for. And so on.
} - zparseopts can put options into arrays, like ( -l -h -t )
}
} How to best test if some value is in array?
Any of a number of ways; for example:
if (( ${+array[(r)-l]} )); then ...
} - It can put values into arrays too
}
} How to reach it? Find key, then take [keyidx+1] ?
Normally you would not need to do this. Instead of
zparseopts -a array l: h t:
and then trying to examine $array, you'd do
zparseopts l:=lop h=hop t:=top
which sets any or all of the three arrays $lop, $hop, and $top, so you
can examine them separately in whatever manner you like.
if [[ $lop[1] == -l ]]; then echo The -l option is $lop[2] ; fi
if [[ $top[1] != -t ]]; then echo There is no -t option ; fi
} - It can put values into associative arrays
}
} How to test if option without argument has ben given?
if (( ${+hash[-h]} )); then ...
} I tried to use -K option, but it's like not working:
It's working. The doc says:
-K
With this option, the arrays specified with the -a and -A
options and with the `=array' forms are kept unchanged when
none of the specs for them is used. This allows assignment
of default values to them before calling zparseopts.
Note "when *none* of the specs" are used. In your example, you used
the spec for the -si option, which is not "none", so the entire default
value of the hash is discarded.
It also appears that your example is missing the trailing colons on all
of the option names, to indicate that they each want an argument.
} - How to handle the same option given many times?
}
} Like:
} fun -s abab -s ab -s x
RTFM.
name+
If a `+' appears after name, the option is appended to array
each time it is found in the positional parameters; without
the `+' only the _last_ occurrence of the option is preserved.
Hence
zparseopts s+:=sop
would set sop=(-s abab -s ab -s x) given the "fun" call above. This is
a case where "zparseopts -A" may not be very useful, because
zparseopts -A hash s+:
would set hash[-s]="abababx" in that same situation.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author