-
Notifications
You must be signed in to change notification settings - Fork 0
/
args2.spec.js
234 lines (228 loc) · 7.17 KB
/
args2.spec.js
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
describe('boolean args', () => {
it('for schema "l", args "-l", l should be true', () => {
const parser = new ArgsParser('l', '-l')
expect(parser.get('l')).toBe(true)
})
it('for schema "l", args "", l should be false', () => {
const parser = new ArgsParser('l', '')
expect(parser.get('l')).toBe(false)
})
it('g, -g', () => {
const parser = new ArgsParser('g', '-g')
expect(parser.get('g')).toBe(true)
})
it('explicit boolean value', () => {
expect(new ArgsParser('l', '-l true').get('l')).toBe(true)
expect(new ArgsParser('l', '-l false').get('l')).toBe(false)
})
})
describe('number args', () => {
it('p#, -p 8080', () => {
const parser = new ArgsParser('p#', '-p 8080')
expect(parser.get('p')).toBe(8080)
})
it('m#, -m 123', () => {
const parser = new ArgsParser('m#', '-m 123')
expect(parser.get('m')).toBe(123)
})
it('p#, ""', () => {
const parser = new ArgsParser('p#', '')
expect(parser.get('p')).toBe(-1)
})
})
describe('string args', () => {
it('d*, -d /var/logs/', () => {
const parser = new ArgsParser('d*', '-d /var/logs/')
expect(parser.get('d')).toBe('/var/logs/')
})
it('d*', () => {
const parser = new ArgsParser('d*', '')
expect(parser.get('d')).toBe('')
})
it('s*, -s abc', () => {
const parser = new ArgsParser('s*', '-s abc')
expect(parser.get('s')).toBe('abc')
})
})
describe('mixed args', () => {
describe('d*,l "-d /var/logs/ -l"', () => {
let parser;
beforeEach(() => {
parser = new ArgsParser('d*,l', '-d /var/logs/ -l')
})
it('-d should be /var/logs/', () => {
expect(parser.get('d')).toBe('/var/logs/')
})
})
describe('d*,l "-l -d /var/logs/"', () => {
let parser;
beforeEach(() => {
parser = new ArgsParser('d*,l', '-l -d /var/logs/')
})
it('-d should be /var/logs/', () => {
expect(parser.get('d')).toBe('/var/logs/')
})
it('-l should be true', () => {
expect(parser.get('l')).toBe(true)
})
})
describe('d*,l "-d /var/logs/"', () => {
let parser;
beforeEach(() => {
parser = new ArgsParser('d*,l', '-d /var/logs/')
})
it('-l should be false', () => {
expect(parser.get('l')).toBe(false)
})
})
describe('d*,l "-l"', () => {
let parser;
beforeEach(() => {
parser = new ArgsParser('d*,l', '-l')
})
it('-d should be ""', () => {
expect(parser.get('d')).toBe('')
})
})
describe('p#,l, "-l -p 8080', () => {
let parser;
beforeEach(() => {
parser = new ArgsParser('p#,l', '-l -p 8080')
})
it('-p should be 8080', () => {
expect(parser.get('p')).toBe(8080)
})
})
})
describe('exception handling', () => {
describe('missing number value', () => {
it('missing number value', () => {
expect(() => new ArgsParser('p#', '-p').get('p')).toThrow('value is missing for p')
expect(() => new ArgsParser('p#,d*', '-p -d /var/logs/').get('p')).toThrow('value is missing for p')
expect(() => new ArgsParser('p#,d*', '-d /var/logs/ -p').get('p')).toThrow('value is missing for p')
})
it('invalid number value', () => {
expect(() => new ArgsParser('p#', '-p abc').get('p')).toThrow('"abc" is not a number')
})
it('missing string value', () => {
expect(() => new ArgsParser('p#,d*', '-p 8080 -d').get('d')).toThrow('value is missing for d')
expect(() => new ArgsParser('p#,d*', '-d -p 8080').get('d')).toThrow('value is missing for d')
expect(() => new ArgsParser('d*', '-d').get('d')).toThrow('value is missing for d')
})
it('invalid boolean value', () => {
expect(() => new ArgsParser('l', '-l foo').get('l')).toThrow('foo is not a boolean value')
})
it('unknown arg', () => {
expect(() => new ArgsParser('p#,d*,l', '-d /var/logs/ -p 8080 -l -g')).toThrow('unknown arg: -g')
})
})
})
class ArgsParser {
constructor(schema, args) {
this.args = args
this.parseSchema(schema)
this.parseArgs()
}
parseSchema(raw) {
this.schema = {}
raw.split(',').map(raw => raw.trim()).forEach(raw => {
const name = raw.substring(0, 1)
const type = raw.substring(1)
this.schema[name] = createArg(type, this.args, name)
})
}
parseArgs() {
const tokens = this.args.split(' ')
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i]
if (token.startsWith('-')) {
const argName = token.substring(1)
const arg = this.schema[argName]
if (arg) {
arg.set(tokens, i)
} else {
throw new Error(`unknown arg: ${token}`)
}
}
}
}
get(name) {
return this.schema[name].get()
}
}
function createArg(rawType, args, name) {
switch (rawType) {
case '#':
return new NumberArg(args, name)
case '*':
return new StringArg(args, name)
default:
return new BooleanArg(args, name)
}
}
class NumberArg {
constructor(args, name) {
this.args = args
this.name = name
}
get() {
return this.val || -1
}
set(tokens, i) {
const nextToken = tokens[i + 1]
if (typeof nextToken === 'string') {
if (nextToken.startsWith('-')) {
throw new Error(`value is missing for ${this.name}`)
} else {
const value = parseInt(nextToken)
if (isNaN(value)) {
throw new Error(`"${nextToken}" is not a number`)
} else {
this.val = value
}
}
} else if (typeof nextToken === 'undefined') {
throw new Error(`value is missing for ${this.name}`)
}
}
}
class StringArg {
constructor(args, name) {
this.args = args
this.name = name
}
get() {
return this.val || ''
}
set(tokens, i) {
const val = tokens[i + 1]
if (!val || val.startsWith('-')) {
throw new Error(`value is missing for ${this.name}`)
}
this.val = tokens[i + 1]
}
}
class BooleanArg {
constructor(args, name) {
this.args = args
this.name = name
}
get() {
return this.val || false
}
set(tokens, i) {
const nextToken = tokens[i + 1]
if (typeof nextToken === 'string') {
if (!nextToken.startsWith('-') && nextToken !== "true" && nextToken !== "false") {
throw new Error(`${nextToken} is not a boolean value`)
}
if (nextToken === "false") {
this.val = false
} else {
this.val = true
}
} else {
this.val = true
}
}
}