我想为具有虚拟文件树的工具创建 zsh 完成。例如,我的文件树如下所示:
/
|- foo/
| |- bar
| |- baz/
| |- qux
|- foobar
Run Code Online (Sandbox Code Playgroud)
我的工具mycmd有一个用于列出当前目录的子命令:
$ mycmd ls
foo/
foobar
$ mycmd ls foo/
bar
baz/
Run Code Online (Sandbox Code Playgroud)
我实际的 zsh 完成看起来是这样的:
_mycmd_ls() {
if [ ! -z "$words[-1]" ]; then
dir=$(dirname /$words[-1])
lastpart=$(basename $words[-1])
items=$(mycmd ls $dir | grep "^$lastpart")
else
items=$(mycmd ls)
fi
_values -s ' ' 'items' ${(uozf)items}
}
_mycmd() {
local -a commands
commands=(
'ls:list items in directory'
)
_arguments -C -s -S -n …Run Code Online (Sandbox Code Playgroud)