-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion.go
More file actions
63 lines (50 loc) · 1.54 KB
/
completion.go
File metadata and controls
63 lines (50 loc) · 1.54 KB
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
package cli
// ShellCompletion interface for different shell implementations
type ShellCompletion interface {
// GetCompletions returns completion suggestions for a command
GetCompletions(cmd *Command, args []string) []string
// Register adds the completion command to the given command
Register(cmd *Command)
// GenerateScript generates the shell completion script
GenerateScript(cmd *Command) string
}
// AddCompletion registers completion commands for all supported shells
func AddCompletion(rootCmd *Command) {
// Register bash completion
bashComp := &BashCompletion{}
bashComp.Register(rootCmd)
// Register zsh completion
zshComp := &ZshCompletion{}
zshComp.Register(rootCmd)
// Register fish completion
fishComp := &FishCompletion{}
fishComp.Register(rootCmd)
// Register PowerShell completion
psComp := &PowerShellCompletion{}
psComp.Register(rootCmd)
}
// getCompletionWords returns completion words for a command (shared implementation)
func getCompletionWords(cmd *Command) []string {
var words []string
// Add visible subcommands
for name, subcmd := range cmd.GetCommands() {
if !subcmd.IsHidden() {
words = append(words, name)
}
}
// Add all available flags
allFlags := cmd.getAllFlags()
for _, flag := range allFlags {
// Skip hidden flags
if flag.IsHidden() {
continue
}
// Add primary name with --
words = append(words, "--"+flag.PrimaryName())
// Add short name with - if it exists
if shortName := flag.ShortName(); shortName != "" {
words = append(words, "-"+shortName)
}
}
return words
}