summaryrefslogtreecommitdiff
path: root/example/stdlib/main.go
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2019-11-18 01:10:06 +0200
committerGitHub <[email protected]>2019-11-18 01:10:06 +0200
commit00c7945465e0c2b97194363055ed1d784f17c395 (patch)
treed3e736b4fb279975bbcc017ae1bad53e454c5773 /example/stdlib/main.go
parent05b68ffc813dd10c420993cb1cf927b346c057b8 (diff)
parent8724aaf18312e54750540a9578e00d61b1c545d8 (diff)
Merge pull request #104 from posener/v2
Complete v2
Diffstat (limited to 'example/stdlib/main.go')
-rw-r--r--example/stdlib/main.go35
1 files changed, 35 insertions, 0 deletions
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)
+}