zsh-users
 help / color / mirror / code / Atom feed
From: "Daniel Shahaf" <d.s@daniel.shahaf.name>
To: "Philippe Proulx" <eeppeliteloop@gmail.com>, zsh-users@zsh.org
Subject: Re: How to complete with _arguments() depending on an option and within a loop
Date: Tue, 11 May 2021 14:12:59 +0000	[thread overview]
Message-ID: <27484ec7-fc56-4164-b9b6-edfb62df0514@www.fastmail.com> (raw)
In-Reply-To: <CAB4xu_0RM08GhVq3crr3eA0qpYumEEP5vZL5PgoFuaaY4q9JBA@mail.gmail.com>

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


  reply	other threads:[~2021-05-11 14:14 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 [this message]
2021-05-12 13:44   ` Philippe Proulx
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=27484ec7-fc56-4164-b9b6-edfb62df0514@www.fastmail.com \
    --to=d.s@daniel.shahaf.name \
    --cc=eeppeliteloop@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).