optparse-applicative 的 zsh 自动补全脚本中的 compadd 失败

3

我不确定是optparse-applicative的脚本有问题还是我使用方法不对。

在optparse-applicative的自述文件中,它说明程序可以通过自动完成脚本来使用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

我在我的zshrc中运行以下内容(我使用oh-my-zsh,但我已将其删除,并且在仅具有小的PATH添加以获取setup脚本的最小配置中仍会出现此问题)。

autoload -U +X compinit && compinit
autoload -U +X bashcompinit && bashcompinit
source <(setup --zsh-completion-script `which setup`)

我多次遇到以下错误:
/dev/fd/11:compadd:24: 只能从完成函数中调用
我已经运行了compinit,并且完成脚本似乎看起来没问题,我也查看了周围的内容,但是我似乎无法弄清楚为什么会发生这个错误...
1个回答

1

你不需要源zsh补全脚本,只需要将它们添加到你的fpath参数中。

所以只需将setup --zsh-completion-script $(which setup)的输出放入一个名为_setup的文件中,放在$HOME/.config/zsh/completions目录下即可。

fpath=($HOME/.config/zsh/completions $fpath)
autoload -U compinit && compinit

看起来你的格式可能出了问题。你介意重新评估一下,并确保内联代码引用使用 反引号 标记吗? - Jeremy Caney

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接