zsh-users
 help / color / mirror / code / Atom feed
From: TJ Luoma <luomat@gmail.com>
To: Zsh-Users List <zsh-users@zsh.org>
Subject: zparseopts help
Date: Fri, 23 Jan 2015 10:39:32 -0500	[thread overview]
Message-ID: <CADjGqHt4bv3QGOCne_yktMVbeu+TbC+jj_q8wVvHO-9TQt_4ww@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1815 bytes --]

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?

2. Are a series of 'if' statements the best way to handle these sorts of
options?

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!

TjL

ps - if anyone knows of a good place for zparseopts examples, please let me
know. Google was not very much help.

             reply	other threads:[~2015-01-23 15:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-23 15:39 TJ Luoma [this message]
2015-01-23 16:37 ` ZyX
2015-01-23 17:15 ` Bart Schaefer
2015-01-23 17:56   ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADjGqHt4bv3QGOCne_yktMVbeu+TbC+jj_q8wVvHO-9TQt_4ww@mail.gmail.com \
    --to=luomat@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).