Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate completions #4

Open
juanibiapina opened this issue Mar 10, 2019 · 0 comments
Open

Generate completions #4

juanibiapina opened this issue Mar 10, 2019 · 0 comments

Comments

@juanibiapina
Copy link
Owner

juanibiapina commented Mar 10, 2019

Provide a command to generate completions for a CLI. Possible example usage:

$ sub --name awesomecli --bin bin/awesomecli --relative ".." gen-completions zsh > completions/awesomecli.zsh

Possible template for zsh (using old completions system):

if [[ ! -o interactive ]]; then
    return
fi

compctl -K _awesomecli awesomecli

_awesomecli() {
  local words completions
  read -cA words

  if [ "${#words}" -eq 2 ]; then
    completions="$(awesomecli completions)"
  else
    completions="$(awesomecli completions "${words[@]:1:-1}")"
  fi

  reply=("${(ps:\n:)completions}")
}

and bash:

_awesomecli() {
  COMPREPLY=()
  local word="${COMP_WORDS[COMP_CWORD]}"

  if [ "$COMP_CWORD" -eq 1 ]; then
    COMPREPLY=( $(compgen -W "$(awesomecli completions)" -- "$word") )
  else
    local completions="$(awesomecli completions "${COMP_WORDS[@]:1}")"
    COMPREPLY=( $(compgen -W "$completions" -- "$word") )
  fi
}

complete -F _awesomecli awesomecli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant