diff options
Diffstat (limited to 'example/compflag/main.go')
| -rw-r--r-- | example/compflag/main.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/example/compflag/main.go b/example/compflag/main.go new file mode 100644 index 0000000..84d82d6 --- /dev/null +++ b/example/compflag/main.go @@ -0,0 +1,31 @@ +// compflag shows how to use the github.com/posener/complete/compflag package to have auto bash +// completion for a defined set of flags. +package main + +import ( + "fmt" + "os" + + "github.com/posener/complete/compflag" +) + +var ( + // Add variables to the program. Since we are using the compflag library, we can pass options to + // enable bash completion to the flag values. + name = compflag.String("name", "", "Give your name", compflag.OptValues("foo", "bar", "foo bar")) + something = compflag.String("something", "", "Expect somthing, but we don't know what, so no other completion options will be provided.", compflag.OptValues("")) + nothing = compflag.String("nothing", "", "Expect nothing after flag, so other completion can be provided.") +) + +func main() { + // Parse flags and perform bash completion if needed. + compflag.Parse("stdlib") + + // Program logic. + if *name == "" { + fmt.Println("Your name is missing") + os.Exit(1) + } + + fmt.Println("Hi,", name) +} |
