-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-with.go
278 lines (244 loc) · 6.6 KB
/
parse-with.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (C) 2023-2024 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.
package cliargs
import (
"github.com/sttk/cliargs/errors"
)
const anyOption = "*"
// ParseWith is the method which parses command line arguments with option configurations.
// This method divides command line arguments to command arguments and options.
//
// And an option consists of a name and an option argument.
// Options are separated to long format options and short format options.
// About long/short format options, since they are same with Parse method, see the comment of
// that method.
//
// This method allows only options declared in option configurations, basically.
// An option configuration has fields: StoreKey, Names, HasArg, IsArray, Defaults, Desc,
// ArgInHelp, and Validator.
//
// When an option matches one of the Names in the option configurations, the option is registered
// into Cmd with StoreKey.
// If both HasArg and IsArray are true, the option can have one or multiple option arguments, and
// if HasArg is true and IsArray is false, the option can have only one option argument, otherwise
// the option cannot have option arguments.
// If Defaults field is specified and no option value is given in command line arguments, the value
// of Defaults is set as the option arguments.
//
// If options not declared in option configurations are given in command line arguments, this
// method basically returns UnconfiguradOption error.
// However, if you want to allow other options, add an option configuration of which StoreKey or
// the first element of Names is "*".
//
// The option configurations used to parsing are set into this Cmd instance, and it can be
// retrieved from its field: Cmd#OptCfgs.
func (cmd *Cmd) ParseWith(optCfgs []OptCfg) error {
_, _, err := cmd.parseArgsWith(optCfgs, false)
cmd.OptCfgs = optCfgs
return err
}
// ParseUntilSubCmdWith is the method which parses command line arguments with option
// configurations but stops parsing when encountering first command argument.
//
// This method creates and returns a new Cmd instance that holds the command line arguments
// starting from the first command argument.
//
// This method parses command line arguments in the same way as the Cmd#parse_with method,
// except that it only parses the command line arguments before the first command argument.
//
// The option configurations used to parsing are set into this Cmd instance, and it can be
// retrieved from its field: Cmd#OptCfgs.
func (cmd *Cmd) ParseUntilSubCmdWith(optCfgs []OptCfg) (Cmd, error) {
idx, isAfterEndOpt, err := cmd.parseArgsWith(optCfgs, true)
cmd.OptCfgs = optCfgs
if idx < 0 {
return Cmd{}, err
}
return cmd.subCmd(idx, isAfterEndOpt), err
}
func (cmd *Cmd) parseArgsWith(
optCfgs []OptCfg,
untilFirstArg bool,
) (int, bool, error) {
const ANY_OPT = "*"
hasAnyOpt := false
var EMPTY_STRUCT struct{}
optMap := make(map[string]struct{})
cfgMap := make(map[string]int)
for i, cfg := range optCfgs {
var names []string
for _, nm := range cfg.Names {
if len(nm) > 0 {
names = append(names, nm)
break
}
}
storeKey := cfg.StoreKey
if len(storeKey) == 0 && len(names) > 0 {
storeKey = names[0]
}
if len(storeKey) == 0 {
continue
}
if storeKey == ANY_OPT {
hasAnyOpt = true
continue
}
var firstName string
if len(names) > 0 {
firstName = names[0]
} else {
firstName = storeKey
}
_, exists := optMap[storeKey]
if exists {
e := errors.StoreKeyIsDuplicated{StoreKey: storeKey, Name: firstName}
return -1, cmd.isAfterEndOpt, e
}
optMap[storeKey] = EMPTY_STRUCT
if !cfg.HasArg {
if cfg.IsArray {
e := errors.ConfigIsArrayButHasNoArg{StoreKey: storeKey, Name: firstName}
return -1, cmd.isAfterEndOpt, e
}
if cfg.Defaults != nil {
e := errors.ConfigHasDefaultsButHasNoArg{StoreKey: storeKey, Name: firstName}
return -1, cmd.isAfterEndOpt, e
}
}
if len(names) == 0 {
cfgMap[firstName] = i
} else {
for _, nm := range cfg.Names {
_, exists := cfgMap[nm]
if exists {
e := errors.OptionNameIsDuplicated{StoreKey: storeKey, Name: nm}
return -1, cmd.isAfterEndOpt, e
}
cfgMap[nm] = i
}
}
}
var takeOptArgs = func(opt string) bool {
i, exists := cfgMap[opt]
if exists {
return optCfgs[i].HasArg
}
return false
}
var collectArgs = func(arg string) {
cmd.Args = append(cmd.Args, arg)
}
var collectOpts = func(name string, a ...string) error {
i, exists := cfgMap[name]
if exists {
cfg := optCfgs[i]
var storeKey string
if len(cfg.StoreKey) > 0 {
storeKey = cfg.StoreKey
} else {
for _, nm := range cfg.Names {
if len(nm) > 0 {
storeKey = nm
break
}
}
}
if len(a) > 0 {
if !cfg.HasArg {
return errors.OptionTakesNoArg{
Option: name,
StoreKey: storeKey,
}
}
arr, _ := cmd.opts[storeKey]
if len(arr) > 0 {
if !cfg.IsArray {
return errors.OptionIsNotArray{
StoreKey: storeKey,
Option: name,
}
}
if cfg.Validator != nil {
err := (*cfg.Validator)(storeKey, name, a[0])
if err != nil {
return err
}
}
cmd.opts[storeKey] = append(arr, a[0])
} else {
if cfg.Validator != nil {
err := (*cfg.Validator)(storeKey, name, a[0])
if err != nil {
return err
}
}
cmd.opts[storeKey] = append(arr, a[0])
}
} else {
if cfg.HasArg {
return errors.OptionNeedsArg{
Option: name,
StoreKey: storeKey,
}
}
_, exists := cmd.opts[storeKey]
if !exists {
cmd.opts[storeKey] = nil
}
}
return nil
} else {
if !hasAnyOpt {
return errors.UnconfiguredOption{
Option: name,
}
}
if len(a) > 0 {
cmd.opts[name] = append(cmd.opts[name], a[0])
} else {
cmd.opts[name] = nil
}
return nil
}
}
idx, isAfterEndOpt, err := parseArgs(
cmd._args,
collectArgs,
collectOpts,
takeOptArgs,
untilFirstArg,
cmd.isAfterEndOpt,
)
for _, cfg := range optCfgs {
storeKey := cfg.StoreKey
if len(storeKey) == 0 && len(cfg.Names) > 0 {
for _, nm := range cfg.Names {
if len(nm) > 0 {
storeKey = nm
break
}
}
}
if len(storeKey) == 0 {
continue
}
if storeKey == ANY_OPT {
continue
}
arr, exists := cmd.opts[storeKey]
if !exists && cfg.Defaults != nil {
arr = cfg.Defaults
cmd.opts[storeKey] = arr
exists = true
}
if exists && cfg.onParsed != nil {
e := (*cfg.onParsed)(arr)
if e != nil && err == nil {
err = e
}
}
}
return idx, isAfterEndOpt, err
}