https://github.com/s0rg/compflag
Auto-completion for stdlib flag items
https://github.com/s0rg/compflag
autocompletion bash-completion golang-library shell zsh-completion
Last synced: 17 days ago
JSON representation
Auto-completion for stdlib flag items
- Host: GitHub
- URL: https://github.com/s0rg/compflag
- Owner: s0rg
- License: mit
- Created: 2022-11-06T04:40:22.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-04-25T12:12:43.000Z (2 months ago)
- Last Synced: 2025-04-25T13:26:30.063Z (2 months ago)
- Topics: autocompletion, bash-completion, golang-library, shell, zsh-completion
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pkg.go.dev/github.com/s0rg/compflag)
[](https://github.com/s0rg/compflag/blob/master/LICENSE)
[](go.mod)
[](https://github.com/s0rg/compflag/tags)[](https://github.com/s0rg/compflag/actions?query=workflow%3Aci)
[](https://goreportcard.com/report/github.com/s0rg/compflag)
[](https://qlty.sh/gh/s0rg/projects/compflag)
[](https://qlty.sh/gh/s0rg/projects/compflag)
# compflag
Auto-completion for stdlib flag items
# usage
you got two options here:
- Call compflag.Complete() somewhere before actual app logic, best point is right at the start:
```go
package mainimport (
"os"
"flag""github.com/s0rg/compflag"
)func main() {
if compflag.Complete() {
os.Exit(0)
}flag.Parse()
// other startup logic...
}
```- Define trigger flag for completion:
```go
package mainimport (
"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:
```bash
complete -C %your-binary-name% %your-binary-name%
```if you prefer flag-triggered version:
```bash
complete -C "%your-binary-name% -%your-flag% bash" %your-binary-name%
```Now enter `%your-binary-name%`, and hit `TAB` twice )
# shell compatability
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`.