我正在尝试为 Symfony 的控制台创建一个 ZSH 自动完成脚本,我几乎完成了它,但我在最后一部分阻止了它。由于函数返回,我需要创建一个数组,但我不知道如何处理。这是我写的代码:
_find_console () {
echo "php $(find . -maxdepth 2 -mindepth 1 -name 'console' -type f | head -n 1)"
}
_console_get_command_list () {
`_find_console` --no-ansi | \
sed "1,/Available commands/d" | \
awk '/ [a-z]+/ { print $0 }' | \
sed -E 's/^[ ]+//g' | \
sed -E 's/[:]+/\\:/g' | \
sed -E 's/[ ]{2,}/\:/g'
}
_console () {
local -a commands
commands=(`_console_get_command_list`)
_describe 'commands' commands
}
compdef _console php console
compdef _console console
如果我执行命令行,它会以正确的方式对其进行格式化。但与 ZSH 一起使用时,输出是这样的:
acme:hello -- Hello
assets:install -- Installs
cache:warmup -- Warms
格式化函数返回:
acme\:hello:Hello World example command
assetic\:dump:Dumps all assets to the filesystem
cache\:warmup:Warms up an empty cache
未显示的单词用作命令完成。这是一个示例:Asciinema 示例。
如果我将返回值直接用于数组中,则效果很好。