-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
87 lines (75 loc) · 1.79 KB
/
command.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
package cli
import (
"fmt"
"strings"
)
type Command struct {
Name string
Description string
Usage string
Flags []Flag
Args []Argument
ArgSets []ArgumentSet
Opts []Option
SubCommands []Command
HideHelp bool
Action func(com Command) error
}
func SubCommandToString(sub *Command) string {
return fmt.Sprintf("Sub: %-10s, %-s", sub.Name, sub.Description)
}
func SubComArrayToStringArray(subArr []Command) (strArr []string) {
for _, sub := range subArr {
strArr = append(strArr, SubCommandToString(&sub))
}
return strArr
}
func (c Command) Print() {
fmt.Println(c.String())
}
func (c Command) String() string {
sep := "\n.."
sa := []string{
fmt.Sprintf("..Name: %s", c.Name),
fmt.Sprintf("Description: %s", c.Description),
fmt.Sprintf("Usage: %s", c.Usage),
strings.Join(SubComArrayToStringArray(c.SubCommands), sep),
strings.Join(ArgArrayToStringArray(c.Args), sep),
strings.Join(ArgSetArrayToStringArray(c.ArgSets), sep),
strings.Join(FlagArrayToStringArray(c.Flags), sep),
strings.Join(OptArrayToStringArray(c.Opts), sep),
}
return strings.Join(sa, sep)
}
func (c Command) hasFlag(flagStr string) (found bool) {
for _, flag := range c.Flags {
if flagStr == flag.ShortName || flagStr == flag.LongName {
return true
}
}
return false
}
func (c Command) hasOption(optStr string) (found bool) {
for _, opt := range c.Opts {
if optStr == opt.ShortName || optStr == opt.LongName {
return true
}
}
return false
}
func (c Command) hasArg(argStr string) (found bool) {
for _, arg := range c.Args {
if argStr == arg.Value {
return true
}
}
return false
}
func (c Command) hasSubCommand(comStr string) (found bool) {
for _, sub := range c.SubCommands {
if comStr == sub.Name {
return true
}
}
return false
}