所以我不确定这是optparse-applicative的脚本有问题,还是我用错了。
在optparse-applicative readme中,它指出可以通过自动完成脚本和zsh选项使程序可用。对于我的程序setup:
$> setup --zsh-completion-script `which setup`输出:
#compdef setup
local request
local completions
local word
local index=$((CURRENT - 1))
request=(--bash-completion-enriched --bash-completion-index $index)
for arg in ${words[@]}; do
request=(${request[@]} --bash-completion-word $arg)
done
IFS=$'\n' completions=($( /Users/anrothan/.local/bin/setup "${request[@]}" ))
for word in $completions; do
local -a parts
# Split the line at a tab if there is one.
IFS=$'\t' parts=($( echo $word ))
if [[ -n $parts[2] ]]; then
if [[ $word[1] == "-" ]]; then
local desc=("$parts[1] ($parts[2])")
compadd -d desc -- $parts[1]
else
local desc=($(print -f "%-019s -- %s" $parts[1] $parts[2]))
compadd -l -d desc -- $parts[1]
fi
else
compadd -f -- $word
fi
done我在我的zsh中运行了以下代码(我使用了oh- my -zsh,但我删除了它,这仍然发生在一个最小配置中,只需要添加一个小路径来获取setup脚本)。
autoload -U +X compinit && compinit
autoload -U +X bashcompinit && bashcompinit
source <(setup --zsh-completion-script `which setup`)我多次收到以下错误:
/dev/fd/11:compadd:24: can only be called from completion function
我已经运行了compinit,完成脚本在我看来似乎是正确的,我看了看周围,但我似乎不明白为什么会发生这个错误……
发布于 2020-05-18 10:36:30
您不需要编写zsh完成脚本,只需将它们添加到fpath参数中即可。
因此,只需将setup --zsh-completion-script $(which setup)的输出放在$HOME/.config/zsh/completions中名为_setup的文件中。
fpath=($HOME/.config/zsh/completions $fpath)
autoload -U compinit && compinithttps://stackoverflow.com/questions/54157564
复制相似问题