Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: zparseopts help
23.01.2015, 18:42, "TJ Luoma" <luomat@xxxxxxxxx>:
> As I've mentioned before, I have been using zsh forever but feel like I
> missed out on learning about 97% of what it can do.
>
> For example, today I learned about zparseopts.
>
> (Feel free to mock.)
>
> I took a look through the `man` entry, but… I don't know what it is about
> my brain, but I can read and re-read man pages and still fail to understand
> what it is saying. I do much better with examples.
>
> I did some googling and found
> http://emg-2.blogspot.com/2008/01/zsh-unique-features.html and once I had
> that I could go back and look at the `man` page to understand what it was
> doing.
>
> So here is my first attempt at using `zparseopts`:
>
> zparseopts -D -E -A MyVariableNameHere -- a b -orange -grape -apple
>
> if (( ${+MyVariableNameHere[-a]} )); then
> echo "Apple";
> fi
>
> if (( ${+MyVariableNameHere[--apple]} )); then
> echo "Apple";
> fi
>
> if (( ${+MyVariableNameHere[-b]} )); then
> echo "Banana";
> fi
>
> if (( ${+MyVariableNameHere[--orange]} )); then
> echo "orange";
> fi
>
> if (( ${+MyVariableNameHere[--grape]} )); then
> echo "grape";
> fi
>
> Questions:
>
> 1. Is there a way to combine the -a and --apple statements into one?
zparseopts -- a=A -apple=A
if (( $#A )) ; then
echo Apple
fi
>
> 2. Are a series of 'if' statements the best way to handle these sorts of
> options?
I guess this is why zparseopts has `option_definition=ARRNAME` syntax.
>
> What I have been doing is something like this:
>
> for MyVariableNameHere in "$@"
> do
> case "$MyVariableNameHere" in
> -a|--apple)
> echo "Apple"
> shift
> ;;
> -b|--banana)
> echo "Banana"
> shift
> ;;
> esac
> done
>
> but that has the disadvantage of not being able to parse "-ab" as two
> separate arguments. OTOH it's very readable and I don't have to worry about
> very many chances of missing a closing bracket or brace!
I did not know about `zparseopts` and have written the following interesting piece of code which handles `-ab`, `-c{arg}`, `--foo {arg}`. No `--foo={arg}`, but you can easily imagine how it would look:
```
function parseargs()
{
local -i ENDOFARGS=0
local -i OUTPUT=0
for arg in $@ ;
do
if (( OUTPUT == 1 ))
then
OUTPUTFILE=${arg}
OUTPUT=0
continue
elif (( OUTPUT == 2 ))
then
OUTPUT=0
fi
[[ "${METHOD}" == "1" ]] &&
METHOD=${arg} && continue
[[ "${ARCHTYPE}" == "1" ]] &&
ARCHTYPE=${arg} && continue
if (( ! $ENDOFARGS )) && [[ ${arg[1]} == "-" ]]
then
case ${arg} in
--) ENDOFARGS=1 ;;
--type) ARCHTYPE=1 ;;
--method) METHOD=1 ;;
--debug) DEBUG=1 ;;
--help) Help ;;
--stdout) setargument 1 1 ;;
--decompress) setargument 2 1 ;;
--compress) setargument 3 1 ;;
--test) setargument 4 1 ;;
--force) setargument 5 1 ;;
--keep) setargument 6 1 ;;
--quiet) setargument 7 1 ;;
--show-progress) setargument 8 1 ;;
--fast) setargument 9 1 ;;
--best) setargument 9 9 ;;
--onefile) setargument 10 1 ;;
--verbose) setargument 11 1 ;;
-) setargument 12 1 ;;
--*) eerror 64 "Unknown key: ${arg}!" ;;
-*) local -i I=2
while [[ ! -z "${arg[$I]}" ]]
do
local CUR="${arg[$I]}"
if (( OUTPUT ))
then
OUTPUT=2
OUTPUTFILE="${arg[$I,-1]}" && break
fi
case ${CUR} in
o) OUTPUT=1 ;;
c) setargument 1 1 ;;
d) setargument 2 1 ;;
z) setargument 3 1 ;;
t) setargument 4 1 ;;
f) setargument 5 1 ;;
k) setargument 6 1 ;;
q) setargument 7 1 ;;
p) setargument 8 1 ;;
O) setargument 10 1 ;;
v) setargument 11 1 ;;
[1-9]) setargument 9 ${CUR} ;;
*) eerror 64 "Unknown key: -${CUR}!" ;;
esac
(( I++ ))
done ;;
esac
else
FILES+=( ${arg:a} )
fi
done
[[ -z $ARCHTYPE ]] && EXT="7z" \
|| EXT="$ARCHTYPE"
[[ "${OUTPUTFILE}" == "-" ]] && setargument 1 1 && unset OUTPUTFILE
ARGUMENTS[3]=$(( ! ${ARGUMENTS[2]} && ! ${ARGUMENTS[4]} ))
(( ${ARGUMENTS[3]} )) && [[ -z ${OUTPUTFILE} ]] && [[ ! -z ${FILES} ]] &&\
OUTPUTFILE="${FILES[1]}.$EXT"
}
```
Not particularly readable (and is in fact one part of my first big zsh scripts).
(If you are wondering this is the part of a wrapper script for `7z` archiver that makes it able to be used as `gzip` (but only if both `-so` and `-si` options are fully supported and they are not).)
>
> TjL
>
> ps - if anyone knows of a good place for zparseopts examples, please let me
> know. Google was not very much help.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author