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

Re: How to complete with _arguments() depending on an option and within a loop

On Tue, May 11, 2021 at 10:13 AM Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> 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


    $ 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.


> Cheers,
> Daniel

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