要检查当前命令行,可以使用$words https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html#index-words and $CURRENT https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html#index-CURRENT这是完成特殊参数。
CURRENT https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html#index-CURRENT
这是当前单词的编号,即光标当前在单词数组中所在的单词。
...
words https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html#index-words
该数组包含当前正在编辑的命令行上出现的单词。
--- zshcompwid(1),完成特殊参数,zshcompwid - zsh 完成小部件 https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html#Completion-Special-Parameters
完成功能可以修改$words
and $CURRENT
(和/或其他变量),然后根据其修改后的命令行启动整个完成系统。例如:
$ mycmd ls -al<TAB> ;# This is the input, and
;# $words == ("mycmd" "ls" "-al") ;# original value for $words.
;# $words=("ls" "-al") ;# We could update $words for making zsh
;# $ ls -al<TAB> ;# to start the completion system with
;# its modified command line.
;# Finally, _ls would be called.
效用函数_normal https://zsh.sourceforge.io/Doc/Release/Completion-System.html#index-_005fnormal可用于。
_normal https://zsh.sourceforge.io/Doc/Release/Completion-System.html#index-_005fnormal
...
第二种用途是重新检查由$words
数组和$CURRENT
修改后的参数。
-- zshcompsys(1),实用函数,_normal https://zsh.sourceforge.io/Doc/Release/Completion-System.html#index-_005fnormal
_mycmd
可以列出如下:
_mycmd () {
if ((CURRENT == 2)); then
compadd foo ssh ls
elif ((CURRENT > 2)); then
case "$words[2]" in
(ssh|ls)
shift words
((CURRENT--))
_normal -p mycmd
;;
(foo)
_nothing
;;
(*)
_message "mycmd: invalid subcommand or arguments"
;;
esac
fi
return $?
}
甚至,更多地使用完成内置/实用函数,如下所示:
_mycmd () {
local curcontext="${curcontext}" state context line
local -A opt_args
_arguments '*:: :->subcmd'
if [[ "$state" == "subcmd" ]]; then
if ((CURRENT == 1)); then
_describe -t mycmd-subcmd "mycmd command" '(foo ssh ls)'
else
curcontext="${curcontext%:*:*}:mycmd-$words[1]:"
case "$words[1]" in
(ssh|ls)
compset -n 1
_normal -p $service
;;
(foo)
_nothing
;;
(*)
_message "mycmd: invalid subcommand or arguments"
;;
esac
fi
fi
return $?
}