jawher / mow.cli

A versatile library for building CLI applications in Go
MIT License
872 stars 55 forks source link

Implement out-of-the-box support for shell autocompletion (source code available!) #113

Open skyzyx opened 3 years ago

skyzyx commented 3 years ago

I haven't looked closely at the internals recently, so I'd like to leave that part to you. But by exposing a model of the command structure in #106 (responding to #104), I was able to write some re-usable code that provides shell auto-completion for Bash and Zsh. (I haven't attempted Fish or Powershell yet, but it'd be awesome if someone who knows those shells better than I could contribute those bits).

Here is the code. MIT licensed. Please steal and adapt. https://gist.github.com/skyzyx/149caddbf0c949da58618cb03b08e4d9

After compiling down to a single binary (e.g., mycmd), I'm able to setup autocompletion thusly:

Bash:

complete -C mycmd mycmd

Zsh:

autoload -U +X bashcompinit && bashcompinit
complete -C mycmd mycmd