-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.go
213 lines (183 loc) · 5.4 KB
/
args.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package clui
import (
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/spoke-d/clui/group"
)
// GlobalArgs is used to construct the arguments used for the CLI.
// The global arguments are then passed to the command once found, without the
// global flags.
type GlobalArgs struct {
commands *group.Group
commandFlags []string
subCommand string
subCommandArgs []string
subCommandFlags []string
isHelp, isVersion, isDebug, isDevMode bool
requiresInstall, requiresUninstall bool
requiresNoColor bool
requiresNoSubKeys bool
}
// NewGlobalArgs creates a new GlobalArgs type for processing arguments passed
// to the cli.
func NewGlobalArgs(commands *group.Group) *GlobalArgs {
return &GlobalArgs{
commands: commands,
}
}
// SubCommand returns the sub command name.
func (a *GlobalArgs) SubCommand() string {
return a.subCommand
}
// SubCommandArgs returns the sub command arguments.
func (a *GlobalArgs) SubCommandArgs() []string {
return a.subCommandArgs
}
// SubCommandFlags returns the sub command flags.
func (a *GlobalArgs) SubCommandFlags() []string {
return a.subCommandFlags
}
// CommandFlags returns the command arguments.
func (a *GlobalArgs) CommandFlags() []string {
return a.commandFlags
}
// Help returns if the operator has passed the help flag.
func (a *GlobalArgs) Help() bool {
return a.isHelp
}
// Version returns if the operator has passed the version flag.
func (a *GlobalArgs) Version() bool {
return a.isVersion
}
// Debug returns if the operator has passed the debug flag.
func (a *GlobalArgs) Debug() bool {
return a.isDebug
}
// DevMode returns if the operator has passed the devMode flag.
func (a *GlobalArgs) DevMode() bool {
return a.isDevMode
}
// RequiresInstall returns if the operator has passed the requires install flag.
func (a *GlobalArgs) RequiresInstall() bool {
return a.requiresInstall
}
// RequiresUninstall returns if the operator has passed the requires uninstall
// flag.
func (a *GlobalArgs) RequiresUninstall() bool {
return a.requiresUninstall
}
// RequiresNoColor returns if the operator has passed the no color output flag.
func (a *GlobalArgs) RequiresNoColor() bool {
return a.requiresNoColor
}
// RequiresNoSubKeys returns if the help should include subkeys.
func (a *GlobalArgs) RequiresNoSubKeys() bool {
return a.requiresNoSubKeys
}
// Process consumes the arguments and correctly separates them between global
// flags and arguments and command flags and arguments.
func (a *GlobalArgs) Process(args []string) error {
var processed []string
// first remove the default
for _, arg := range args {
if arg == "--" {
break
}
switch arg {
case "-h", "-help", "--help":
a.isHelp = true
case "-v", "-version", "--version":
a.isVersion = true
case "--debug":
a.isDebug = true
case "--dev-mode":
a.isDevMode = true
case "--no-color":
a.requiresNoColor = true
case "--no-sub-keys":
a.requiresNoSubKeys = true
case "--autocomplete-install":
a.requiresInstall = true
case "--autocomplete-uninstall":
a.requiresUninstall = true
default:
processed = append(processed, arg)
}
}
if a.requiresInstall && a.requiresUninstall {
return errors.Errorf("both autocomplete flags can not be used at the same time")
}
for i, arg := range processed {
if a.subCommand == "" {
if arg != "" && arg[0] == '-' {
// Record the arg...
a.commandFlags = append(a.commandFlags, arg)
}
}
// If we didn't find a subCommand yet and this is the first non-flag
// argument, then this is our subCommand.
if a.subCommand == "" && arg != "" && arg[0] != '-' {
a.subCommand = arg
if a.commands.Nested() {
// If the command has a space in it, then it is invalid.
// Set a blank command so that it fails.
if strings.ContainsRune(arg, ' ') {
a.subCommand = ""
return nil
}
// Determine the argument we look to end subCommands.
// We look at all arguments until one has a space. This
// disallows commands like: ./cli foo "bar baz".
// An argument with a space is always an argument.
var j int
for k, v := range processed[i:] {
if strings.ContainsRune(v, ' ') {
break
}
j = i + k + 1
}
// Nested CLI the subCommand is actually the entire arg list up
// to a flag that is still a valid subCommand.
searchKey := strings.Join(processed[i:j], " ")
k, ok := a.commands.LongestPrefix(searchKey)
if ok {
// k could be a prefix that doesn't contain the full command
// such as "foo", instead of "foobar", so we need to verify
// that we have an entire key. To do that, we look for an
// ending in a space of end of a string.
verify, err := regexp.Compile(regexp.QuoteMeta(k) + `( |$)`)
if err != nil {
return err
}
if verify.MatchString(searchKey) {
a.subCommand = k
i += strings.Count(k, " ")
}
}
}
// The remaining processed the subCommand arguments
a.subCommandArgs = removeFlags(processed[i+1:])
a.subCommandFlags = removeNonFlags(processed[i+1:])
}
}
return nil
}
func removeFlags(args []string) []string {
var result []string
for _, v := range args {
if v == "-" || !strings.HasPrefix(v, "-") {
result = append(result, v)
}
}
return result
}
func removeNonFlags(args []string) []string {
var result []string
for _, v := range args {
if strings.HasPrefix(v, "-") {
result = append(result, v)
}
}
return result
}