zsh-workers
 help / Atom feed
* possible bug in _arguments?
@ 2018-12-23 22:34 Max Katsev
  2018-12-24  5:58 ` dana
  0 siblings, 1 reply; 2+ messages in thread
From: Max Katsev @ 2018-12-23 22:34 UTC (permalink / raw)
  To: zsh-workers

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

I want to have completion for a command that looks like "command [optional]
required" (i.e. possible completions are either "command required" or
"command optional required").
My understanding is that "_arguments '::message:(optional)'
':message:(required)'" should achieve this effect.
However when I'm trying to use it on zsh 5.6.2, in addition to the expected
commandlines, it also allows to complete "command required required" where
the required argument is repeated twice (but not more). Is this a bug or am
I missing something? Is there another way to get what I want?

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: possible bug in _arguments?
  2018-12-23 22:34 possible bug in _arguments? Max Katsev
@ 2018-12-24  5:58 ` dana
  0 siblings, 0 replies; 2+ messages in thread
From: dana @ 2018-12-24  5:58 UTC (permalink / raw)
  To: Max Katsev; +Cc: zsh-workers

On 23 Dec 2018, at 16:34, Max Katsev <mkatsev@gmail.com> wrote:
>Is this a bug or am
>I missing something? Is there another way to get what I want?

I don't think optional-operand specs work like that. AFAIK, when a spec for an
optional operand n appears before one for a required operand n+1, all it tells
zsh is that it should offer both sets of possibilities at the command-line
position corresponding to n. You have to figure out yourself what to do with
an earlier operand once you're past that position.

If the command is as simple as you said, you can just do something like this:

  local required

  (( CURRENT > 2 )) &&
  [[ $words[2] == optional ]] &&
  required=':cmd2:(required)'

  _arguments : ':cmd1:(optional required)' $required '*:other args:_files'

If you also need to support options, you can use the *:: form:

  local ret=1 required
  local -a context line state state_descr
  local -A opt_args

  _arguments : -a -b -c '*:: :->next' && ret=0

  [[ $state == next ]] && {
    words=( fake "${(@)words}" )
    (( CURRENT++ ))

    (( CURRENT > 2 )) &&
    [[ $words[2] == optional ]] &&
    required=':cmd2:(required)'

    _arguments : \
      ':cmd1:(optional required)' $required '*:other args:_files' \
    && ret=0
  }

  return ret

Something like that, at least; there are lots of ways you could handle it

dana


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-23 22:34 possible bug in _arguments? Max Katsev
2018-12-24  5:58 ` dana

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/ public-inbox