From: Philippe Proulx <eeppeliteloop@gmail.com>
To: Daniel Shahaf <d.s@daniel.shahaf.name>
Cc: zsh-users@zsh.org
Subject: Re: How to complete with _arguments() depending on an option and within a loop
Date: Wed, 12 May 2021 09:44:06 -0400 [thread overview]
Message-ID: <CAB4xu_09nbsdpRviaB_+wcEmnccUqXL1r2MYVSmv_yP80Ukhqg@mail.gmail.com> (raw)
In-Reply-To: <27484ec7-fc56-4164-b9b6-edfb62df0514@www.fastmail.com>
On Tue, May 11, 2021 at 10:13 AM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
>
> Philippe Proulx wrote on Mon, 10 May 2021 17:30 +00:00:
> > $ prog --state=foo --meow --state=bar cola --mix --lol=34 \
> > --state=foo --meow --zoom
> >
> > In a Zsh completion script, how would you complete the options above,
> > knowing that:
> ⋮
> > I managed to implement such a completion script "manually", with the
> > `words` and `CURRENT` variables, compset(), and _describe() to add
> > completions. It works, but I feel like I'm rewriting parts of
> > _arguments().
> >
> > Is there a known way to achieve this mostly with _arguments()?
>
> Look at _beep. It does basically what you're describing, and does use
> _arguments for the core logic.
>
> I'd go for something along the lines of:
>
> _prog() {
> local -a args=( --state=': :(foo bar)' )
> if ((CURRENT > 1)); then
> case ${words[(Ib.CURRENT-1.)--state=*]} in
> (*=foo) args+=( -meow -zoom );;
> (*=bar) args+=( --mix --lol :cola );;
> esac
> fi
> (munge $words as _beep does)
> _arguments : "${args[@]}"
> }
This seems fragile.
For example (I know it's far-fetched here, but just to make my point),
what if your device file is literally named `--new` in the current
working directory, such that you would write:
$ beep --device=--new
or
$ beep --device --new
Now, completion doesn't work as expected:
$ beep --device --new <tab>
Zsh offers `--device` again, because the completion script "saw" the
`--new` word, without taking any parsing into account.
My point being: I'll still need to handle command-line parsing manually,
including supporting both `--opt=arg` and `--opt arg` forms, etc. to
find where the current state (equivalent of the `--new` option of
beep(1)) really begins.
Phil
>
> Cheers,
>
> Daniel
next prev parent reply other threads:[~2021-05-12 13:45 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-10 17:30 Philippe Proulx
2021-05-11 14:12 ` Daniel Shahaf
2021-05-12 13:44 ` Philippe Proulx [this message]
2021-05-12 15:11 ` Bart Schaefer
2021-05-12 15:14 ` Philippe Proulx
2021-05-12 15:50 ` Bart Schaefer
2021-05-12 16:14 ` 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=CAB4xu_09nbsdpRviaB_+wcEmnccUqXL1r2MYVSmv_yP80Ukhqg@mail.gmail.com \
--to=eeppeliteloop@gmail.com \
--cc=d.s@daniel.shahaf.name \
--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).