为多个命令定义 ZSH 补全函数 (compdef)

Defining ZSH completion function (compdef) for multiple commands

我有一个名为

的 ZSH 完成脚本
#compdef kubens
_arguments "1: :(- $(kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'))"

这提供了 kubens 命令的完成。但是,如果用户提供了某个安装参数,该程序将与 kns 名称链接,因此我尝试使用相同的 #compdef.

为这两个命令提供完成

我该如何实现?

我们可以对 #compdef 行使用 cmd=service 形式:

#compdef kubens kns=kubens

尽管我们可以在 #compdef 行中使用多个 名称 ,但当 cmd 行为相同时,可以使用 cmd=service 形式作为 service:

#compdef name ... [ -{p|P} pattern ... [ -N name ... ] ]

The file will be made autoloadable and the function defined in it will be called when completing names, each of which is either the name of a command whose arguments are to be completed ...
...
Each name may also be of the form ‘cmd=service’. When completing the command cmd, the function typically behaves as if the command (or special context) service was being completed instead.

-- zshcompsys(1): Completion System, INITIALIZATION, Autoloaded files, #compdef