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

Re: possible bug in _arguments?



On 23 Dec 2018, at 16:34, Max Katsev <mkatsev@xxxxxxxxx> 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



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