-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommands.go
86 lines (72 loc) · 1.77 KB
/
commands.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
package cli
import "fmt"
func HelpCommand() Command {
return Command{
Name: "help",
Usage: Usage("Show information about a command"),
Action: ActionFunc(func(cmd *Command) ActionRunner {
// Do not mutate the previous path.
path := cmd.Path()
newPath := make([]string, len(path)-1)
copy(newPath, path[:len(path)-1])
path = newPath
_ = RestStringsVar(cmd, &path, "command")
return func(cmd *Command) error {
cmd, err := cmd.App().Command(path...)
if err != nil {
return err
}
return cmd.App().Help(cmd, cmd.Stdout())
}
}),
}
}
func CompletionCommand() Command {
return Command{
Name: "completion",
Usage: Usage("Generate completion script"),
Action: ActionFunc(func(cmd *Command) ActionRunner {
shell := StringArg(cmd, "shell")
return func(cmd *Command) error {
var generator CompletionGenerator
switch *shell {
case "zsh":
generator = &ZSHCompletionGenerator{}
default:
return fmt.Errorf("unknown shell: '%s'", *shell)
}
root, err := cmd.App().RootCommand()
if err != nil {
return err
}
return generator.CompletionGenerate(root, cmd.Stdout())
}
}),
}
}
func HelpCommandFlag() CommandFlag {
return CommandFlag{
Long: "help",
Short: "h",
Usage: Usage("Show information about a command"),
Action: ActionRunner(func(cmd *Command) error {
return cmd.App().Help(cmd, cmd.Stdout())
}),
}
}
func VersionCommandFlag(version string) CommandFlag {
return CommandFlag{
Long: "version",
Short: "v",
Usage: Usage("Print version information and quit"),
Action: ActionRunner(func(cmd *Command) error {
if _, err := cmd.Printf(version); err != nil {
return err
}
if _, err := cmd.Printf("\n"); err != nil {
return err
}
return nil
}),
}
}