-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion.go
49 lines (41 loc) · 1.1 KB
/
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
package command
import (
"fmt"
"os"
"strings"
"github.com/urfave/cli"
)
// Completion handles bash completion for the commands
func Completion(c *cli.Context) {
lastParam := os.Args[len(os.Args)-2]
noCompletionFlags := []string{"--apiKey", "--filter", "--baseURL", "--after"}
if ContainsString(lastParam, noCompletionFlags) {
return
}
fileCompletionFlags := []string{"--outputFolder", "--xmlFile"}
if ContainsString(lastParam, fileCompletionFlags) {
fmt.Fprintln(c.App.Writer, "fileCompletion")
return
}
if len(os.Args) > 2 {
completeFlags(c)
}
}
func completeFlags(c *cli.Context) {
for _, flag := range c.App.Command(os.Args[1]).Flags {
name := strings.Split(flag.GetName(), ",")[0]
if !c.IsSet(name) {
fmt.Fprintf(c.App.Writer, "--%s\n", name)
}
}
}
// RootCompletion prints the list of root commands as the root completion method
// This is similar to the default method, but it excludes aliases
func RootCompletion(c *cli.Context) {
for _, command := range c.App.Commands {
if command.Hidden {
continue
}
fmt.Fprintf(c.App.Writer, "%s:%s\n", command.Name, command.Usage)
}
}