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
我已经设法通过以下代码获得了对这两个参数 apps
和 USERS
的建议,但我的行为与命令可以处理的内容不一致。
(_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'
}
上面的代码有点工作,除了:
- 输入单个应用名称后,切换到用户。
-u
需要一个用户(多个 -u
实例是有效的)
我尝试了 *:apps:_ynh_app_list
,但是 ynh app addaccess foo-app1 -u user1 <TAB>
调用了 _ynh_app_list
而不是 _ynh_users_list
我想得到的是:
ynh app addaccess <TAB>
显示 __ynh_app_list
提供的补全
ynh app addaccess foo-app1 <TAB>
仍然显示 __ynh_app_list
提供的补全
- 一旦输入
-u
,无论 -u
之后的字数是多少,所有补全建议都应来自 __ynh_users_list
:yunohost app addaccess foo-app1 bar-app2 -u barfoo1 foobar2 <TAB>
仍然以一个补全用户名形式 __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
我正在尝试为 YunoHost 编写完成插件。我正在努力处理以下情况,其中参数(可选或非可选)可以采用多个值:
ynh app addaccess apps [apps ...] [-u [USERS [USERS ...]]]
典型用法:
ynh app addaccess foo-app1 bar-app2 -u barfoo1 foobar2
我已经设法通过以下代码获得了对这两个参数 apps
和 USERS
的建议,但我的行为与命令可以处理的内容不一致。
(_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'
}
上面的代码有点工作,除了:
- 输入单个应用名称后,切换到用户。
-u
需要一个用户(多个-u
实例是有效的)
我尝试了 *:apps:_ynh_app_list
,但是 ynh app addaccess foo-app1 -u user1 <TAB>
调用了 _ynh_app_list
而不是 _ynh_users_list
我想得到的是:
ynh app addaccess <TAB>
显示__ynh_app_list
提供的补全
ynh app addaccess foo-app1 <TAB>
仍然显示__ynh_app_list
提供的补全
- 一旦输入
-u
,无论-u
之后的字数是多少,所有补全建议都应来自__ynh_users_list
:yunohost app addaccess foo-app1 bar-app2 -u barfoo1 foobar2 <TAB>
仍然以一个补全用户名形式__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