-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcmd_completion.go
89 lines (73 loc) · 1.74 KB
/
cmd_completion.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"strings"
"github.com/ipinfo/cli/lib/complete"
"github.com/ipinfo/cli/lib/complete/install"
"github.com/ipinfo/cli/lib/complete/predict"
"github.com/spf13/pflag"
)
var completionsCompletion = &complete.Command{
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
"--help": predict.Nothing,
},
Args: predict.Set([]string{
"install",
"bash",
"zsh",
"fish",
}),
}
func printHelpCompletion() {
fmt.Printf(
`Usage: %s completion [<opts>] [install | bash | zsh | fish]
Description:
Install or print out the code needed to do shell auto-completion.
The current explicitly supported shells are:
- bash
- zsh
- fish
Examples:
# Attempt auto-installation on any of the supported shells.
$ %[1]s completion install
# Output auto-completion script for bash for manual installation.
$ %[1]s completion bash
# Output auto-completion script for zsh for manual installation.
$ %[1]s completion zsh
# Output auto-completion script for fish for manual installation.
$ %[1]s completion fish
Options:
--help, -h
show help.
`, progBase)
}
func cmdCompletion() error {
pflag.Parse()
args := pflag.Args()[1:]
if fHelp || len(args) == 0 || len(args) > 1 {
printHelpCompletion()
return nil
}
var installStr string
var err error
switch strings.ToLower(args[0]) {
case "install":
return install.Install(progBase)
case "bash":
installStr, err = install.BashCmd(progBase)
case "zsh":
installStr, err = install.ZshCmd(progBase)
case "fish":
installStr, err = install.FishCmd(progBase)
default:
fmt.Printf("err: %s is not a valid subcommand\n\n", args[0])
printHelpCompletion()
return nil
}
if err != nil {
return err
}
fmt.Println(installStr)
return nil
}