ZSH 完成 - 每个参数多个项目

ZSH completion - Multiple items per argument

我正在尝试为 YunoHost 编写完成插件。我正在努力处理以下情况,其中参数(可选或非可选)可以采用多个值:

ynh app addaccess apps [apps ...] [-u [USERS [USERS ...]]]

典型用法:

ynh app addaccess foo-app1 bar-app2 -u barfoo1 foobar2

我已经设法通过以下代码获得了对这两个参数 appsUSERS 的建议,但我的行为与命令可以处理的内容不一致。 (_ynh_app_list_ynh_users_list 是对 compadd 的调用)

_yunohost_app_addaccess() {
    _arguments -s -C \
        '1:apps:_ynh_app_list' \
        '*'{-u,--users}'[users]:users:_ynh_users_list'
}

上面的代码有点工作,除了:

我尝试了 *:apps:_ynh_app_list,但是 ynh app addaccess foo-app1 -u user1 <TAB> 调用了 _ynh_app_list 而不是 _ynh_users_list


我想得到的是:

是否有可能实现这一点,至少是最后一项 ([-u USER [USER...]])?非常感谢! :)

使用排除列表 option/argument 功能可以挽救:

_yunohost_app_addaccess() {
    _arguments -s -C \
        {1,'*'}:apps:_ynh_app_list \
        '(*)'{-u,--users}'[users]:*:users:_ynh_users_list'
}

ynh addaccess app [APPS ...] 部分可以通过正常参数 SPEC 来完成; 1:MESSAGE:ACTION*:MESSAGE:ACTION.
大括号扩展简写:{1,'*'}:apps:_ynh_app_list

[-u USER [USER...]] 部分可以由 OPTSPEC 完成,前面有排除列表,:*PATTERN:MESSAGE:ACTION 为空 PATTERN'(*)-OPTNAME[EXPLANATION]:*:MESSAGE:ACTION'.


这里是排除列表的 zsh 手册供参考:

_arguments ...
...
Each of the forms above may be preceded by a list in parentheses of option names and argument numbers. If the given option is on the command line, the options and arguments indicated in parentheses will not be offered. For example, (-two -three 1)-one:... completes the option -one; if this appears on the command line, the options -two and -three and the first ordinary argument will not be completed after it. (-foo):... specifies an ordinary argument completion; -foo will not be completed if that argument is already present.

Other items may appear in the list of excluded options to indicate various other items that should not be applied when the current specification is matched: a single star (*) for the rest arguments (i.e. a specification of the form *:...); a colon (:) for all normal (non-option-) arguments; and a hyphen (-) for all options. For example, if (*) appears before an option and the option appears on the command line, the list of remaining arguments (those shown in the above table beginning with *:) will not be completed.

--- zshcompsys(1), _arguments, zshcompsys - zsh completion system