Auto-completion for stdlib flag items
you got two options here:
- Call compflag.Complete() somewhere before actual app logic, best point is right at the start:
package main
import (
"os"
"flag"
"github.com/s0rg/compflag"
)
func main() {
if compflag.Complete() {
os.Exit(0)
}
flag.Parse()
// other startup logic...
}
- Define trigger flag for completion:
package main
import (
"os"
"flag"
"github.com/s0rg/compflag"
)
func main() {
compflag.Var("complete")
flag.Parse()
// other startup logic...
}
Please note, that you need to exit app if any completion happened.
Build your app, put binary somewhere in your "PATH", then run:
complete -C %your-binary-name% %your-binary-name%
if you prefer flag-triggered version:
complete -C "%your-binary-name% -%your-flag% bash" %your-binary-name%
Now enter %your-binary-name%
, and hit TAB
twice )
This will work with any shell compatible with complete
(bash and zsh are both good with it), for zsh you may need
also use bashcompinit
in addition to compinit
.