jessevdk/go-flags

Add a working completion example for 'zsh' shell

anatol opened this issue · 1 comments

Currently documentation provides an example of completion for bash shell only.

zsh is one of the popular shells. it would be great to have completion example for this shell as well.

#414

here ya go:

#compdef examples
compdef _examples examples

_examples() {
  local IFS=$'\n'
  local -a completions
  completions=($(GO_FLAGS_COMPLETION=1 "${words[@]}"))
  if [[ -n "$completions" ]]; then
    _describe 'command' completions
  fi
	return 1
}

# don't run the completion function when being source-ed or eval-ed
if [ "$funcstack[1]" = "_examples" ]; then
    _examples "$@"
fi

Where examples is just the name of the installed executable.

Note: This script needs to be installed in one of the $fpath directors, and needs to be loaded / inited via something like autoload -U compinit in your .zshrc. I'm a little unclear on specifics here, but if it helps this is what I have in my .zshrc:

fpath=(~/.zsh_completions $fpath)
autoload -U compinit
compinit

And I put the script in ~/.zsh_completions.