forked from vercel/arg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
553 lines (487 loc) · 11 KB
/
test.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/* global test */
/* eslint-disable no-unused-expressions */
const expect = require('chai').expect;
const arg = require('.');
test('basic parses arguments from process.argv', () => {
const curArgs = process.argv;
process.argv = ['node', 'test.js', '--foo', '1337', '-B', 'hello', '--mcgee'];
try {
const args = arg({
'--foo': Number,
'--bar': String,
'--mcgee': Boolean,
'-B': '--bar'
});
expect(args).to.exist;
expect(args['--foo']).to.equal(1337);
expect(args['--bar']).to.equal('hello');
expect(args['--mcgee']).to.equal(true);
} finally {
process.argv = curArgs;
}
});
test('arg with no arguments', () => {
expect(() => arg()).to.throw(
arg.ArgError,
'argument specification object is required'
);
});
test('basic extra arguments parsing', () => {
const argv = ['hi', 'hello', 'there', '-'];
expect(arg({}, { argv })).to.deep.equal({ _: argv });
});
test('basic string parsing', () => {
const argv = ['hey', '--foo', 'hi', 'hello'];
expect(arg({ '--foo': String }, { argv })).to.deep.equal({
_: ['hey', 'hello'],
'--foo': 'hi'
});
});
test('basic string parsing (equals long-arg)', () => {
const argv = ['hey', '--foo=hi', 'hello'];
expect(arg({ '--foo': String }, { argv })).to.deep.equal({
_: ['hey', 'hello'],
'--foo': 'hi'
});
});
test('basic string parsing (equals long-arg-with-equals)', () => {
const argv = ['hey', '--foo=hi.hello?q=p', 'hello'];
expect(arg({ '--foo': String }, { argv })).to.deep.equal({
_: ['hey', 'hello'],
'--foo': 'hi.hello?q=p'
});
});
test('basic number parsing', () => {
const argv = ['hey', '--foo', '1234', 'hello'];
expect(arg({ '--foo': Number }, { argv })).to.deep.equal({
_: ['hey', 'hello'],
'--foo': 1234
});
});
test('basic boolean parsing', () => {
const argv = ['hey', '--foo', '1234', 'hello'];
expect(arg({ '--foo': Boolean }, { argv })).to.deep.equal({
_: ['hey', '1234', 'hello'],
'--foo': true
});
});
test('basic custom type parsing', () => {
const argv = ['hey', '--foo', '1234', 'hello'];
const customType = (val, name) => `:${name}:${val}:`;
expect(arg({ '--foo': customType }, { argv })).to.deep.equal({
_: ['hey', 'hello'],
'--foo': ':--foo:1234:'
});
});
test('basic string parsing (array)', () => {
const argv = ['hey', '--foo', 'hi', 'hello', '--foo', 'hey'];
expect(arg({ '--foo': [String] }, { argv })).to.deep.equal({
_: ['hey', 'hello'],
'--foo': ['hi', 'hey']
});
});
test('basic number parsing (array)', () => {
const argv = ['hey', '--foo', '1234', 'hello', '--foo', '5432'];
expect(arg({ '--foo': [Number] }, { argv })).to.deep.equal({
_: ['hey', 'hello'],
'--foo': [1234, 5432]
});
});
test('basic boolean parsing (array)', () => {
const argv = ['hey', '--foo', '1234', 'hello', '--foo', 'hallo'];
expect(arg({ '--foo': [Boolean] }, { argv })).to.deep.equal({
_: ['hey', '1234', 'hello', 'hallo'],
'--foo': [true, true]
});
});
test('basic custom type parsing (array)', () => {
const argv = ['hey', '--foo', '1234', 'hello', '--foo', '8911hi'];
const customType = (val, name) => `:${name}:${val}:`;
expect(arg({ '--foo': [customType] }, { argv })).to.deep.equal({
_: ['hey', 'hello'],
'--foo': [':--foo:1234:', ':--foo:8911hi:']
});
});
test('basic alias parsing', () => {
const argv = [
'--foo',
'1234',
'-B',
'-',
'hello',
'--not-foo-or-bar',
'ohai'
];
const opts = {
'--foo': Number,
'--bar': String,
'--another-arg': Boolean,
'-a': '--another-arg',
'--not-foo-or-bar': '--another-arg',
'-B': '--bar'
};
expect(arg(opts, { argv })).to.deep.equal({
_: ['hello', 'ohai'],
'--foo': 1234,
'--bar': '-',
'--another-arg': true
});
});
test('double-dash parsing', () => {
const argv = [
'--foo',
'1234',
'hi',
'--foo',
'5678',
'there',
'--',
'--foo',
'2468'
];
expect(arg({ '--foo': Number }, { argv })).to.deep.equal({
_: ['hi', 'there', '--foo', '2468'],
'--foo': 5678
});
});
test('error: invalid option', () => {
const argv = ['--foo', '1234', '--bar', '8765'];
expect(() => arg({ '--foo': Number }, { argv })).to.throw(
arg.ArgError,
'unknown or unexpected option: --bar'
);
});
test('error: expected argument', () => {
const argv = ['--foo', '--bar', '1234'];
expect(() => arg({ '--foo': String, '--bar': Number }, { argv })).to.throw(
arg.ArgError,
'option requires argument: --foo'
);
});
test('error: expected argument (end flag)', () => {
const argv = ['--foo', '--bar'];
expect(() => arg({ '--foo': Boolean, '--bar': Number }, { argv })).to.throw(
arg.ArgError,
'option requires argument: --bar'
);
});
test('error: expected argument (alias)', () => {
const argv = ['--foo', '--bar', '1234'];
expect(() =>
arg(
{ '--realfoo': String, '--foo': '--realfoo', '--bar': Number },
{ argv }
)
).to.throw(
arg.ArgError,
'option requires argument: --foo (alias for --realfoo)'
);
});
test('error: expected argument (end flag) (alias)', () => {
const argv = ['--foo', '--bar'];
expect(() =>
arg(
{ '--foo': Boolean, '--realbar': Number, '--bar': '--realbar' },
{ argv }
)
).to.throw(
arg.ArgError,
'option requires argument: --bar (alias for --realbar)'
);
});
test('error: non-function type', () => {
const argv = [];
expect(() => arg({ '--foo': 10 }, { argv })).to.throw(
arg.ArgError,
'type missing or not a function or valid array type: --foo'
);
expect(() => arg({ '--foo': null }, { argv })).to.throw(
arg.ArgError,
'type missing or not a function or valid array type: --foo'
);
expect(() => arg({ '--foo': undefined }, { argv })).to.throw(
arg.ArgError,
'type missing or not a function or valid array type: --foo'
);
});
test('error: no singular - keys allowed', () => {
const argv = ['--foo', '--bar', '1234'];
expect(() => arg({ '-': Boolean, '--bar': Number }, { argv })).to.throw(
arg.ArgError,
"argument key must have a name; singular '-' keys are not allowed: -"
);
});
test('error: no multi character short arguments', () => {
const argv = ['--foo', '--bar', '1234'];
expect(() => arg({ '-abc': Boolean, '--bar': Number }, { argv })).to.throw(
arg.ArgError,
'short argument keys (with a single hyphen) must have only one character: -abc'
);
});
test('permissive mode allows unknown args', () => {
const argv = [
'foo',
'--real',
'nice',
'--unreal',
'stillnice',
'-a',
'1',
'-b',
'2',
'goodbye'
];
const result = arg(
{
'--real': String,
'--first': Number,
'-a': '--first'
},
{
argv,
permissive: true
}
);
expect(result).to.deep.equal({
_: ['foo', '--unreal', 'stillnice', '-b', '2', 'goodbye'],
'--real': 'nice',
'--first': 1
});
});
test('permissive mode works with no argv specified', () => {
const curArgs = process.argv;
process.argv = ['node', 'test.js', '--foo', '1337', '-B', 'hello', '--mcgee'];
try {
const result = arg(
{
'--foo': Number,
'--mcgee': Boolean,
'--unused': Boolean
},
{
permissive: true
}
);
expect(result).to.deep.equal({
_: ['-B', 'hello'],
'--foo': 1337,
'--mcgee': true
});
} finally {
process.argv = curArgs;
}
});
test('ensure that all argument properties start with a hyphen', () => {
expect(() =>
arg({
'--foo': Number,
bar: String,
'--baz': Boolean
})
).to.throw(arg.ArgError, "argument key must start with '-' but found: 'bar'");
});
test('ensure argument property is not an empty string', () => {
expect(() =>
arg({
'': Number
})
).to.throw(arg.ArgError, 'argument key cannot be an empty string');
});
test('types with the Flag symbol should be passed true instead of an argument', () => {
const argv = ['--mcgee', '--foo', 'bar', '--baz', '10', 'qix'];
const result = arg(
{
'--mcgee': Boolean,
'--foo': arg.flag(() => 1337),
'--baz': Number
},
{
argv
}
);
expect(result).to.deep.equal({
_: ['bar', 'qix'],
'--mcgee': true,
'--foo': 1337,
'--baz': 10
});
});
test('COUNT should count the number of times a flag has been passed', () => {
const argv = ['--verbose', '-v', '--verbose', 'foo', '-vvvv', '-vv'];
const result = arg(
{
'--verbose': arg.COUNT,
'-v': '--verbose'
},
{
argv
}
);
expect(result).to.deep.equal({
_: ['foo'],
'--verbose': 9
});
});
test('should parse combined shortarg flags', () => {
const argv = ['-vv', '-sd', 'foo', '-vdv'];
const result = arg(
{
'-v': [Boolean],
'-s': Boolean,
'-d': arg.COUNT
},
{
argv
}
);
expect(result).to.deep.equal({
_: ['foo'],
'-v': [true, true, true, true],
'-s': true,
'-d': 2
});
});
test('should parse combined shortarg alias flags', () => {
const argv = ['-vv', '--verbose', '-dvd', 'foo', '--dee', '-vdv'];
const result = arg(
{
'--verbose': [Boolean],
'-v': '--verbose',
'--dee': arg.COUNT,
'-d': '--dee'
},
{
argv
}
);
expect(result).to.deep.equal({
_: ['foo'],
'--verbose': [true, true, true, true, true, true],
'--dee': 4
});
});
test('should allow a non-flag shortarg to suffix a string of shortarg flags', () => {
const argv = ['-vvLo', 'foo'];
const result = arg(
{
'-v': arg.COUNT,
'-L': Boolean,
'-o': String
},
{
argv
}
);
expect(result).to.deep.equal({
_: [],
'-v': 2,
'-L': true,
'-o': 'foo'
});
});
test('should error if a non-flag shortarg comes before a shortarg flag in a condensed shortarg argument', () => {
const argv = ['-vsv', 'foo'];
expect(() =>
arg(
{
'-v': arg.COUNT,
'-s': String
},
{
argv
}
)
).to.throw(
arg.ArgError,
'option requires argument (but was followed by another short argument): -s'
);
});
test('should stop parsing early with positional argument', () => {
const argv = ['-d', 'script', '--foo', 'bar'];
const result = arg(
{
'-d': Boolean
},
{
argv,
stopAtPositional: true
}
);
expect(result).to.deep.equal({
_: ['script', '--foo', 'bar'],
'-d': true
});
});
test('should stop parsing early with permissive', () => {
const argv = ['-dvd', '--foo', 'bar'];
const result = arg(
{
'-d': arg.COUNT
},
{
argv,
stopAtPositional: true,
permissive: true
}
);
expect(result).to.deep.equal({
_: ['-v', '--foo', 'bar'],
'-d': 2
});
});
test('should parse negative numbers (GNU equals form)', () => {
const argv = ['--int=-5'];
const result = arg(
{
'--int': Number
},
{
argv
}
);
expect(result).to.deep.equal({
_: [],
'--int': -5
});
});
test('should parse negative numbers (separate argument form)', () => {
const argv = ['--int', '-5'];
const result = arg(
{
'--int': Number
},
{
argv
}
);
expect(result).to.deep.equal({
_: [],
'--int': -5
});
});
test('should error if numeric type is followed by non-negative, non-argument', () => {
const argv = ['--int', '-abc'];
expect(() =>
arg(
{
'--int': Number
},
{
argv
}
)
).to.throw(arg.ArgError, 'option requires argument: --int');
});
test('should error if negative numeric argument is passed to non-negative argument', () => {
const argv = ['--str', '-15'];
expect(() =>
arg(
{
'--str': String
},
{
argv
}
)
).to.throw(arg.ArgError, 'option requires argument: --str');
});