Like flag
, but with bash completion support.
- Fully compatible with standard library
flag
package - Bash completions for flag names and flag values
- Additional flag types provided:
File
/Dir
: file completion flagBool
: bool flag (that does not complete)Choice
: choices flagStringCompleter
: custom completions- Any other value that implements the
Completer
interface.
Here is an example
import (
- "flag"
+ "github.com/posener/flag"
)
var (
- file = flag.String("file", "", "file value")
+ file = flag.File("file", "*.md", "", "file value")
- dir = flag.String("dir", "", "dir value")
+ dir = flag.Dir("dir", "*", "", "dir value")
b = flag.Bool("bool", false, "bool value")
s = flag.String("any", "", "string value")
- opts = flag.String("choose", "", "some items to choose from")
+ opts = flag.Choice("choose", []string{"work", "drink"}, "", "some items to choose from")
)
func main() {
+ flag.SetInstallFlags("complete", "uncomplete")
flag.Parse()
+ if flag.Complete() { // runs bash completion if necessary
+ return // return from main without executing the rest of the command
+ }
...
}