File: main.go

package info (click to toggle)
golang-github-posener-complete 1.1%2Bgit20180108.57878c9-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports
  • size: 200 kB
  • sloc: sh: 9; makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,351 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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)
}