我正在为 swift 工具(编译器和包管理器)编写一个 zsh 完成脚本。此命令可与或不指定子命令一起使用。例如
# calls to the compiler
# usage: swift [options] <inputs>
1> swift my_file.swift
2> swift -a 1 -b 2 my_file.swift
# calls to build subcommand
3> swift build
4> swift build -c 1
# calls to test subcommand
5> swift test
6> swift test -d 4
到目前为止,我有以下完成脚本。它大部分都有效,但是对于裸呼叫,自动完成功能并不完全正常。它确实进入了 _compiler 方法,但在swift -a
. 它会自动完成,就好像它已经忘记了-a
. 所以swift -a <TAB><TAB>
应该显示选择1-2-3的菜单;但它显示了一个文件列表。因为swift -a -a <TAB><TAB>
它显示了正确的完成,但重复-a
是无效的。
它可能与已更改的状态有关,而对于此特定用例则不应该。但是对于子命令(构建和测试),应更改状态以使其各自的完成正常工作。
我已经阅读了文档,但它非常神秘。我检查了其他各种完成脚本,但无法找出丢失的链接。那么如何为具有可选子命令的命令指定完成,这些子命令具有单独的标志集(并且本身也具有子子命令)?
#compdef swift
local context state state_descr line
typeset -A opt_args
_swift() {
_arguments -C \
': :->command' \
'*:: :->arg'
case $state in
(command)
local tools
tools=(
'build:description for build'
'test:description for test'
)
_alternative \
'tools:common:{_describe "tool" tools }' \
'compiler: :_compiler'
;;
(arg)
case ${words[1]} in
(build)
_build
;;
(test)
_test
;;
(*)
_compiler
;;
esac
;;
esac
}
_compiler() {
_arguments -C \
'(-a)-a[description for a]: :(1 2 3)' \
'(-b)-b[description for b]: :(4 5 6)' \
'*: :_files'
}
_build() {
_arguments -C \
'-c[description for c]: :(1 2 3)'
}
_test() {
_arguments -C \
'-d[description for d]: :(4 5 6)'
}