-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
type-flag.ts
759 lines (678 loc) · 17.4 KB
/
type-flag.ts
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
import { testSuite, expect } from 'manten';
import { typeFlag, type Flags } from '#type-flag';
export default testSuite(({ describe }) => {
describe('type-flag', ({ describe }) => {
describe('Error handling', ({ describe }) => {
describe('Invalid flag name', ({ test }) => {
test('Empty flag name', () => {
expect(() => {
typeFlag({
'': String,
}, []);
}).toThrow(/* 'Invalid flag name: empty' */);
});
test('Reserved characters', () => {
expect(() => {
typeFlag({ 'flag a': String }, []);
}).toThrow(/* Flag name cannot contain the character " " */);
expect(() => {
typeFlag({ 'flag=b': String }, []);
}).toThrow(/* Flag name cannot contain the character "=" */);
expect(() => {
typeFlag({ 'flag:c': String }, []);
}).toThrow(/* Flag name cannot contain the character ":" */);
expect(() => {
typeFlag({ 'flag.d': String }, []);
}).toThrow(/* Flag name cannot contain the character "." */);
});
test('Collision - camelCase to kebab-case', () => {
expect(() => {
typeFlag({
flagA: String,
'flag-a': String,
}, []);
}).toThrow(/* 'Invalid flag name "flagA": collides with flag "flag-a"' */);
});
test('Collision - kebab-case to camelCase', () => {
expect(() => {
typeFlag({
'flag-a': String,
flagA: String,
}, []);
}).toThrow(/* 'Invalid flag name "flag-a": collides with flag "flagA"' */);
});
});
describe('Invalid alias', ({ test }) => {
test('Empty alias', () => {
expect(() => {
typeFlag({
flagA: {
type: String,
alias: '',
},
}, []);
}).toThrow(/* 'Empty alias' */);
});
test('Single-character alias', () => {
expect(() => {
typeFlag({
a: {
type: String,
alias: 'b',
},
}, []);
}).toThrow(
/* Flag alias "b" for flag "a" cannot be defined for a single-character flag */
);
});
test('Multi-character alias', () => {
expect(() => {
typeFlag({
flagA: {
type: String,
alias: 'flag-a',
},
}, []);
}).toThrow(/* 'Multi character' */);
});
test('Collision - alias to alias', () => {
expect(() => {
typeFlag({
flagA: {
type: String,
alias: 'a',
},
flagB: {
type: String,
alias: 'a',
},
}, []);
}).toThrow(/* 'Flag collision: Alias "a" is already used' */);
});
});
});
describe('Types', ({ test }) => {
test('Errors', () => {
typeFlag({
// @ts-expect-error only one element allowed
flagB: [String, String],
// @ts-expect-error only one element allowed
flagC: {
type: [String, String],
alias: 'a',
},
}, []);
});
test('Readonly type', () => {
typeFlag({
flagA: {
type: [String],
},
} as const, []);
const readonly = <F extends Flags>(
options: Readonly<F>,
argv: string[],
) => typeFlag(options, argv);
const parsed = readonly({
flagA: {
type: String,
},
flagB: {
type: [String],
default: () => ['world'],
},
}, ['--flag-a', 'hello']);
expect<string | undefined>(parsed.flags.flagA);
expect<string[]>(parsed.flags.flagB);
});
});
describe('Parsing', ({ describe, test }) => {
describe('edge-cases', ({ test }) => {
test('Object prototype property', () => {
const parsed = typeFlag(
{}, ['--to-string'],
);
expect(parsed.flags).toStrictEqual({});
expect(parsed.unknownFlags).toStrictEqual({
'to-string': [true],
});
});
test('string to boolean', () => {
const parsed = typeFlag({
boolean: Boolean,
}, ['--boolean=value']);
expect(parsed.flags).toStrictEqual({
boolean: true,
});
});
test('end of flags', () => {
const parsed = typeFlag({
string: String,
}, ['--string', '--', 'value']);
expect(parsed.flags).toStrictEqual({
string: '',
});
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['value'],
{ '--': ['value'] },
),
);
});
test('Flag can start with _', () => {
const parsed = typeFlag({
_flag: Boolean,
}, ['--_flag']);
expect(parsed.flags).toStrictEqual({
_flag: true,
});
});
test('invalid consolidated aliases', () => {
const parsed = typeFlag(
{}, ['-invalidAlias'],
);
expect(parsed).toStrictEqual({
_: Object.assign([], { '--': [] }),
flags: {},
unknownFlags: {
i: [true, true, true],
n: [true],
v: [true],
a: [true, true],
l: [true, true],
d: [true],
A: [true],
s: [true],
},
});
});
});
test('end of flags', () => {
const argv = ['--flagA', '--', '--flagB'];
const parsed = typeFlag(
{
flagA: String,
flagB: String,
},
argv,
);
expect<string | undefined>(parsed.flags.flagA).toBe('');
expect<string | undefined>(parsed.flags.flagB).toBe(undefined);
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['--flagB'],
{
'--': ['--flagB'],
},
),
);
expect(argv).toStrictEqual([]);
});
test('strings, booleans, numbers', () => {
const argv = ['--string', 'hello', '--boolean', 'world', '--string', '--number', '2', '--number'];
const parsed = typeFlag(
{
string: String,
boolean: Boolean,
number: Number,
},
argv,
);
expect<string | undefined>(parsed.flags.string).toBe('');
expect<boolean | undefined>(parsed.flags.boolean).toBe(true);
expect<number | undefined>(parsed.flags.number).toBe(Number.NaN);
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['world'],
{ '--': [] },
),
);
expect(argv).toStrictEqual([]);
});
test('convert kebab-case to camelCase', () => {
const argv = ['--some-string=2', '--someString=3', '--some-string=4'];
const parsed = typeFlag(
{
someString: String,
},
argv,
);
expect<string | undefined>(parsed.flags.someString).toBe('4');
expect(argv).toStrictEqual([]);
});
test('kebab-case flags', () => {
const argv = ['--some-string=2', '--someString=3', '--some-string=4'];
const parsed = typeFlag(
{
'some-string': String,
},
argv,
);
expect<string | undefined>(parsed.flags['some-string']).toBe('4');
expect(!('someString' in parsed.flags)).toBe(true);
expect(argv).toStrictEqual([]);
});
test('flag=', () => {
const argv = ['--string=hello', '-s=bye', '--string=', '--boolean=true', '--boolean=false', '--boolean=', 'world', '--number=3.14', '--number='];
const parsed = typeFlag(
{
string: {
type: String,
alias: 's',
},
boolean: Boolean,
number: Number,
},
argv,
);
expect<string | undefined>(parsed.flags.string).toBe('');
expect<boolean | undefined>(parsed.flags.boolean).toBe(true);
expect<number | undefined>(parsed.flags.number).toBe(Number.NaN);
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['world'],
{ '--': [] },
),
);
expect(argv).toStrictEqual([]);
});
test('flag: - to allow the use of = in values (or vice versa)', () => {
const argv = ['--string:A=hello', '-s:B=bye'];
const parsed = typeFlag(
{
string: {
type: String,
alias: 's',
},
},
argv,
);
expect<string | undefined>(parsed.flags.string).toBe('B=bye');
expect(argv).toStrictEqual([]);
});
test('flag: . to allow dot-notation', () => {
const argv = ['--string.A=hello', '-s.B=bye'];
const parsed = typeFlag(
{
string: {
type: String,
alias: 's',
},
},
argv,
);
expect<string | undefined>(parsed.flags.string).toBe('B=bye');
expect(argv).toStrictEqual([]);
});
describe('aliases', ({ test }) => {
test('aliases', () => {
const argv = ['-s', 'hello', '-b', 'world', '-1', 'goodbye'];
const parsed = typeFlag(
{
string: {
type: String,
alias: 's',
},
boolean: {
type: Boolean,
alias: 'b',
},
numberAlias: {
type: String,
alias: '1',
},
},
argv,
);
expect<string | undefined>(parsed.flags.string).toBe('hello');
expect<boolean | undefined>(parsed.flags.boolean).toBe(true);
expect<string | undefined>(parsed.flags.numberAlias).toBe('goodbye');
expect<string[]>(Object.keys(parsed.flags)).toStrictEqual(['string', 'boolean', 'numberAlias']);
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['world'],
{ '--': [] },
),
);
expect(argv).toStrictEqual([]);
});
test('alias with empty string', () => {
const argv = ['-a'];
const parsed = typeFlag(
{
alias: {
type: [String],
alias: 'a',
},
},
argv,
);
expect<string[]>(parsed.flags.alias).toStrictEqual(['']);
expect(argv).toStrictEqual([]);
});
test('alias group with value', () => {
const argv = ['-aliasesa=value'];
const parsed = typeFlag(
{
alias: {
type: [String],
alias: 'a',
},
},
argv,
);
expect<string[]>(parsed.flags.alias).toStrictEqual(['', '', 'value']);
expect(argv).toStrictEqual([]);
});
test('should not accept aliases with --', () => {
const argv = ['--a'];
const parsed = typeFlag(
{
alias: {
type: Boolean,
alias: 'a',
},
},
argv,
);
expect<boolean | undefined>(parsed.flags.alias).toBe(undefined);
expect<(string | boolean)[]>(parsed.unknownFlags.a).toStrictEqual([true]);
expect(argv).toStrictEqual([]);
});
test('single-character alias', () => {
const argv = ['-x', '1', '-y', '2'];
const parsed = typeFlag(
{
x: Number,
y: Number,
},
argv,
);
expect<number | undefined>(parsed.flags.x).toBe(1);
expect<number | undefined>(parsed.flags.y).toBe(2);
});
test('single-character alias grouping', () => {
const argv = ['-am', 'hello'];
const parsed = typeFlag(
{
a: Boolean,
message: {
type: String,
alias: 'm',
},
},
argv,
);
expect<boolean | undefined>(parsed.flags.a).toBe(true);
expect<string | undefined>(parsed.flags.message).toBe('hello');
});
});
test('unknown flags', () => {
const argv = [
'--unknownFlag',
'arg1',
'--unknownFlag=false',
'--unknownFlag=',
'arg2',
'-u',
'arg3',
'-u=value',
'-3',
'-sdf',
'arg4',
'-ff=a',
'--kebab-case',
'--toString',
];
const parsed = typeFlag(
{},
argv,
);
expect<{
[flag: string]: never;
}>(parsed.flags).toStrictEqual({});
type UnknownFlags = {
[flag: string]: (boolean | string)[];
};
expect<UnknownFlags>(parsed.unknownFlags).toStrictEqual({
unknownFlag: [true, 'false', ''],
u: [true, 'value'],
3: [true],
s: [true],
d: [true],
f: [true, true, 'a'],
'kebab-case': [true],
toString: [true],
});
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['arg1', 'arg2', 'arg3', 'arg4'],
{ '--': [] },
),
);
expect(argv).toStrictEqual([]);
});
describe('ignore', ({ test }) => {
test('specific known flag', () => {
const argv = ['--string', 'a', '--string=b', '--unknown', 'c', '--unknown=d', '-u', '-vvv=1'];
const parsed = typeFlag(
{
string: [String],
},
argv,
{
ignore: (type, flagName) => (
type === 'known-flag'
&& flagName === 'string'
),
},
);
expect(parsed).toStrictEqual({
flags: {
string: [],
},
unknownFlags: {
unknown: [true, 'd'],
u: [true],
v: [true, true, '1'],
},
_: Object.assign(
['a', 'c'],
{ '--': [] },
),
});
expect(argv).toStrictEqual(['--string', '--string=b']);
});
test('unknown flags', () => {
const argv = ['--string', 'a', '--string=b', '--unknown', 'c', '--unknown=d', '-u', '-vbv=1', '-us=d'];
const parsed = typeFlag(
{
string: {
type: [String],
alias: 's',
},
boolean: {
type: Boolean,
alias: 'b',
},
},
argv,
{
ignore: type => type === 'unknown-flag',
},
);
expect(parsed).toStrictEqual({
flags: {
string: ['a', 'b', 'd'],
boolean: true,
},
unknownFlags: {},
_: Object.assign(
['c'],
{ '--': [] },
),
});
expect(argv).toStrictEqual(['--unknown', '--unknown=d', '-u', '-vv=1', '-u']);
});
test('after first argument', () => {
const argv = ['--string', 'value', 'first-arg', '--string=b', '--string', 'c', '--unknown=d', '-u', '--', 'hello'];
let ignore = false;
const parsed = typeFlag(
{
string: [String],
},
argv,
{
ignore: (type) => {
if (ignore) {
return true;
}
const isArgument = type === 'argument';
if (isArgument) {
ignore = isArgument;
return isArgument;
}
},
},
);
expect(parsed).toStrictEqual({
flags: {
string: ['value'],
},
unknownFlags: {},
_: Object.assign(
[],
{ '--': [] },
),
});
expect(argv).toStrictEqual(['first-arg', '--string=b', '--string', 'c', '--unknown=d', '-u', '--', 'hello']);
});
test('end of flags', () => {
const argv = ['--string', 'hello', 'a', '--', 'b', '--string=b', '--unknown', '--boolean'];
const parsed = typeFlag(
{
string: {
type: String,
alias: 's',
},
boolean: {
type: Boolean,
alias: 'b',
},
},
argv,
{
ignore: (type, value) => (type === 'argument' && value === '--'),
},
);
expect(parsed).toStrictEqual({
flags: {
string: 'hello',
boolean: undefined,
},
unknownFlags: {},
_: Object.assign(
['a'],
{ '--': [] },
),
});
expect(argv).toStrictEqual(['--', 'b', '--string=b', '--unknown', '--boolean']);
});
});
test('custom type', () => {
const ParseDate = (dateString: string) => new Date(dateString);
const possibleJsFormats = ['cjs', 'esm', 'amd', 'umd'] as const;
type JsFormats = typeof possibleJsFormats[number];
const JsFormat = (format: JsFormats) => {
if (!possibleJsFormats.includes(format)) {
throw new Error(`Invalid format: "${format}"`);
}
return format;
};
const argv = [
'--date=2011-05-03',
'--format=esm',
];
const parsed = typeFlag(
{
date: ParseDate,
format: JsFormat,
},
argv,
);
expect<Date | undefined>(parsed.flags.date).toStrictEqual(ParseDate('2011-05-03'));
expect<JsFormats | undefined>(parsed.flags.format).toBe('esm');
expect(argv).toStrictEqual([]);
});
test('strings, booleans, numbers, array', () => {
const argv = ['--boolean', 'world', '--number', '2', '--number', '--number-array', '1', '--number-array', '2', '--string-array', 'a', '--string-array', 'b'];
const parsed = typeFlag(
{
string: String,
boolean: Boolean,
number: Number,
stringArray: [String],
numberArray: {
type: [Number],
},
},
argv,
);
expect<string | undefined>(parsed.flags.string).toBe(undefined);
expect<boolean | undefined>(parsed.flags.boolean).toBe(true);
expect<number | undefined>(parsed.flags.number).toBe(Number.NaN);
expect<string[]>(parsed.flags.stringArray).toStrictEqual(['a', 'b']);
expect<number[]>(parsed.flags.numberArray).toStrictEqual([1, 2]);
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['world'],
{ '--': [] },
),
);
expect(argv).toStrictEqual([]);
});
describe('Default flag value', ({ test }) => {
test('Types and parsing', () => {
const argv: string[] = [];
const parsed = typeFlag(
{
string: {
type: String,
default: 'hello world',
},
// Ideally, we can block this with a type error but not possible AFAIK
// Supporting reluctantly...
inconsistentTypes: {
type: Boolean,
default: 1,
},
inconsistentTypesB: {
type: Boolean,
default: undefined,
},
arrayOfStrings: {
type: [String],
default: () => ['hello'],
},
inconsistentTypesC: {
type: Number,
default: 'world',
},
noDefault: {
type: [String],
},
},
argv,
);
expect<string>(parsed.flags.string).toBe('hello world');
expect<boolean | number>(parsed.flags.inconsistentTypes).toBe(1);
expect<boolean | undefined>(parsed.flags.inconsistentTypesB).toBe(undefined);
expect<string[]>(parsed.flags.arrayOfStrings).toStrictEqual(['hello']);
expect<string | number>(parsed.flags.inconsistentTypesC).toBe('world');
expect<string[]>(parsed.flags.noDefault).toStrictEqual([]);
expect(argv).toStrictEqual([]);
});
});
});
});
});