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



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[@]}"
}

Cheers,

Daniel




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