3

So I'm not exactly sure whether this is something wrong with optparse-applicative's script or if I'm using it wrong.

In the optparse-applicative readme, it states that programs are made available with automatic completion scripts, with options for zsh. For my program setup:

$> setup --zsh-completion-script `which setup`

Outputs:

#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

I'm running the following in my zshrc (I use oh-my-zsh, but I removed it and this still happens in a bare-minimum config with only a small PATH addition to get the setup script).

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

I get the following error several times:

/dev/fd/11:compadd:24: can only be called from completion function

I've run compinit, and the completion script seems to look right to me, and I've looked around but I can't seem to figure out why this error is happening...

Anrothan
  • 500
  • 5
  • 13

1 Answers1

1

You don't need to source zsh-completion scripts, they just need to be added to your fpath parameter.

So just place the output of setup --zsh-completion-script $(which setup) in a file call _setup in $HOME/.config/zsh/completions.

fpath=($HOME/.config/zsh/completions $fpath)
autoload -U compinit && compinit
Huw
  • 11
  • 2
  • It looks like your formatting might have gotten messed up. Do you mind reevaluating it, and ensuring that inline code references are flagged with `back ticks`? – Jeremy Caney May 18 '20 at 03:21