go-flags icon indicating copy to clipboard operation
go-flags copied to clipboard

Add a working completion example for 'zsh' shell

Open anatol opened this issue 4 years ago • 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.

anatol avatar Nov 01 '21 17:11 anatol

https://github.com/jessevdk/go-flags/pull/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.

drshriveer avatar Sep 05 '24 19:09 drshriveer