diff options
Diffstat (limited to 'example')
| -rw-r--r-- | example/command/main.go | 45 | ||||
| -rw-r--r-- | example/compflag/main.go | 31 | ||||
| -rw-r--r-- | example/self/main.go | 53 | ||||
| -rw-r--r-- | example/stdlib/main.go | 35 |
4 files changed, 111 insertions, 53 deletions
diff --git a/example/command/main.go b/example/command/main.go new file mode 100644 index 0000000..0b073d9 --- /dev/null +++ b/example/command/main.go @@ -0,0 +1,45 @@ +// command shows how to have bash completion to an arbitrary Go program using the `complete.Command` +// struct. +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/posener/complete" + "github.com/posener/complete/predict" +) + +var ( + // Add variables to the program. + name = flag.String("name", "", "Give your name") + something = flag.String("something", "", "Expect somthing, but we don't know what, so no other completion options will be provided.") + nothing = flag.String("nothing", "", "Expect nothing after flag, so other completion can be provided.") +) + +func main() { + // Create the complete command. + // Here we define completion values for each flag. + cmd := &complete.Command{ + Flags: map[string]complete.Predictor{ + "name": predict.Set{"foo", "bar", "foo bar"}, + "something": predict.Something, + "nothing": predict.Nothing, + }, + } + + // Run the completion. + cmd.Complete("command") + + // Parse the flags. + flag.Parse() + + // Program logic. + if *name == "" { + fmt.Println("Your name is missing") + os.Exit(1) + } + + fmt.Println("Hi,", name) +} 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) +} diff --git a/example/self/main.go b/example/self/main.go deleted file mode 100644 index 9479e64..0000000 --- a/example/self/main.go +++ /dev/null @@ -1,53 +0,0 @@ -// Package self -// a program that complete itself -package main - -import ( - "flag" - "fmt" - "os" - - "github.com/posener/complete" -) - -func main() { - - // add a variable to the program - var name string - flag.StringVar(&name, "name", "", "Give your name") - - // create the complete command - cmp := complete.New( - "self", - complete.Command{Flags: complete.Flags{"-name": complete.PredictAnything}}, - ) - - // AddFlags adds the completion flags to the program flags, - // in case of using non-default flag set, it is possible to pass - // it as an argument. - // it is possible to set custom flags name - // so when one will type 'self -h', he will see '-complete' to install the - // completion and -uncomplete to uninstall it. - cmp.CLI.InstallName = "complete" - cmp.CLI.UninstallName = "uncomplete" - cmp.AddFlags(nil) - - // parse the flags - both the program's flags and the completion flags - flag.Parse() - - // run the completion, in case that the completion was invoked - // and ran as a completion script or handled a flag that passed - // as argument, the Run method will return true, - // in that case, our program have nothing to do and should return. - if cmp.Complete() { - return - } - - // if the completion did not do anything, we can run our program logic here. - if name == "" { - fmt.Println("Your name is missing") - os.Exit(1) - } - - fmt.Println("Hi,", name) -} diff --git a/example/stdlib/main.go b/example/stdlib/main.go new file mode 100644 index 0000000..03c6391 --- /dev/null +++ b/example/stdlib/main.go @@ -0,0 +1,35 @@ +// stdlib shows how to have flags bash completion to an arbitrary Go program that uses the standard +// library flag package. +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/posener/complete" +) + +var ( + // Add variables to the program. + name = flag.String("name", "", "Give your name") + something = flag.String("something", "", "Expect somthing, but we don't know what, so no other completion options will be provided.") + nothing = flag.String("nothing", "", "Expect nothing after flag, so other completion can be provided.") +) + +func main() { + // Run the completion. Notice that since we are using standard library flags, only the flag + // names will be completed and not their values. + complete.CommandLine("stdlib") + + // Parse the flags. + flag.Parse() + + // Program logic. + if *name == "" { + fmt.Println("Your name is missing") + os.Exit(1) + } + + fmt.Println("Hi,", name) +} |
