-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
249 lines (228 loc) · 5.99 KB
/
parse.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
// 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 (
"strings"
"unicode"
"github.com/sttk/cliargs/errors"
)
var (
empty = make([]string, 0)
rangeOfAlphabets = &unicode.RangeTable{
R16: []unicode.Range16{
{0x0041, 0x005a, 1}, // A-Z
{0x0061, 0x007a, 1}, // a-z
},
}
rangeOfAlNumMarks = &unicode.RangeTable{
R16: []unicode.Range16{
{0x002d, 0x002d, 1}, // -
{0x0030, 0x0039, 1}, // 0-9
{0x0041, 0x005a, 1}, // A-Z
{0x0061, 0x007a, 1}, // a-z
},
}
)
// Parse is the function to parse command line arguments without configurations.
// This function divides command line arguments to command arguments, which are not associated with
// any options, and options, of which each has a name and option arguments.
// If an option appears multiple times in command line arguments, the option has multiple option
// arguments.
// Options are divided to long format options and short format options.
//
// A long format option starts with "--" and follows multiple characters which consists of
// alphabets, numbers, and '-'.
// (A character immediately after the heading "--" allows only an alphabet.)
// A long format option can be followed by "=" and its option argument.
//
// A short format option starts with "-" and follows single character which is an alphabet.
// Multiple short options can be combined into one argument.
// (For example -a -b -c can be combined into -abc.)
// Moreover, a short option can be followed by "=" and its option argument.
// In case of combined short options, only the last short option can take an option argument.
// (For example, -abc=3 is equal to -a -b -c=3.)
func (cmd *Cmd) Parse() error {
var collectArgs = func(a string) {
cmd.Args = append(cmd.Args, a)
}
var collectOpts = func(name string, a ...string) error {
arr, exists := cmd.opts[name]
if !exists {
arr = empty
}
cmd.opts[name] = append(arr, a...)
return nil
}
_, _, err := parseArgs(cmd._args, collectArgs, collectOpts, takeOptArgs, false, cmd.isAfterEndOpt)
return err
}
// ParseUntilSubCmd is the method that parses command line arguments without 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 method, except that
// it only parses the command line arguments before the first command argument.
func (cmd *Cmd) ParseUntilSubCmd() (Cmd, error) {
var collectArgs = func(_arg string) {}
var collectOpts = func(name string, a ...string) error {
arr, exists := cmd.opts[name]
if !exists {
arr = empty
}
cmd.opts[name] = append(arr, a...)
return nil
}
idx, isAfterEndOpt, err := parseArgs(
cmd._args, collectArgs, collectOpts, takeOptArgs, true, cmd.isAfterEndOpt)
if idx < 0 {
return Cmd{}, err
}
return cmd.subCmd(idx, isAfterEndOpt), err
}
func takeOptArgs(_opt string) bool {
return false
}
func parseArgs(
osArgs []string,
collectArgs func(string),
collectOpts func(string, ...string) error,
takeOptArgs func(string) bool,
untilFirstArg bool,
isAfterEndOpt bool,
) (int, bool, error) {
prevOptTakingArgs := ""
var firstErr error = nil
L0:
for iArg, arg := range osArgs {
if isAfterEndOpt {
if untilFirstArg {
return iArg, isAfterEndOpt, firstErr
}
collectArgs(arg)
} else if len(prevOptTakingArgs) > 0 {
err := collectOpts(prevOptTakingArgs, arg)
prevOptTakingArgs = ""
if err != nil {
if firstErr == nil {
firstErr = err
}
continue L0
}
} else if strings.HasPrefix(arg, "--") {
if len(arg) == 2 {
isAfterEndOpt = true
continue L0
}
arg = arg[2:]
i := 0
for _, r := range arg {
if i > 0 {
if r == '=' {
rr := []rune(arg)
err := collectOpts(string(rr[0:i]), string(rr[i+1:]))
if err != nil {
if firstErr == nil {
firstErr = err
}
continue L0
}
break
}
if !unicode.Is(rangeOfAlNumMarks, r) {
if firstErr == nil {
firstErr = errors.OptionContainsInvalidChar{Option: arg}
}
continue L0
}
} else {
if !unicode.Is(rangeOfAlphabets, r) {
if firstErr == nil {
firstErr = errors.OptionContainsInvalidChar{Option: arg}
}
continue L0
}
}
i++
}
if i == len(arg) {
if takeOptArgs(arg) && iArg < len(osArgs)-1 {
prevOptTakingArgs = arg
continue L0
}
err := collectOpts(arg)
if err != nil {
if firstErr == nil {
firstErr = err
}
continue L0
}
}
} else if strings.HasPrefix(arg, "-") {
if len(arg) == 1 {
if untilFirstArg {
return iArg, isAfterEndOpt, firstErr
}
collectArgs(arg)
continue L0
}
arg := arg[1:]
var name string
i := 0
for _, r := range arg {
if i > 0 {
if r == '=' {
if len(name) > 0 {
rr := []rune(arg)
err := collectOpts(name, string(rr[i+1:]))
if err != nil {
if firstErr == nil {
firstErr = err
}
}
}
continue L0
}
if len(name) > 0 {
err := collectOpts(name)
if err != nil {
if firstErr == nil {
firstErr = err
}
}
}
}
if !unicode.Is(rangeOfAlphabets, r) {
if firstErr == nil {
firstErr = errors.OptionContainsInvalidChar{Option: string(r)}
}
name = ""
} else {
name = string(r)
}
i++
}
if i == len(arg) && len(name) > 0 {
if takeOptArgs(name) && iArg < len(osArgs)-1 {
prevOptTakingArgs = name
} else {
err := collectOpts(name)
if err != nil {
if firstErr == nil {
firstErr = err
}
continue L0
}
}
}
} else {
if untilFirstArg {
return iArg, isAfterEndOpt, firstErr
}
collectArgs(arg)
}
}
return -1, isAfterEndOpt, firstErr
}