-
Notifications
You must be signed in to change notification settings - Fork 70
/
parse.test.ts
1330 lines (1210 loc) · 41.3 KB
/
parse.test.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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable max-nested-callbacks */
import {assert, expect} from 'chai'
import * as fs from 'fs'
import {flags, parse} from '../../src/parser'
import {Interfaces} from '../../src'
import {URL} from 'url'
import {directory, file} from '../../src/parser/flags'
import * as sinon from 'sinon'
const stripAnsi = require('strip-ansi')
describe('parse', () => {
it('--bool', async () => {
const out = await parse(['--bool'], {
flags: {
bool: flags.boolean(),
},
})
expect(out).to.deep.include({flags: {bool: true}})
})
it('arg1', async () => {
const out = await parse(['arg1'], {
args: [{name: 'foo'}],
})
expect(out.argv).to.deep.equal(['arg1'])
expect(out.args).to.deep.equal({foo: 'arg1'})
})
it('arg1 arg2', async () => {
const out = await parse(['arg1', 'arg2'], {
args: [{name: 'foo'}, {name: 'bar'}],
})
expect(out.argv).to.deep.equal(['arg1', 'arg2'])
expect(out.args).to.deep.equal({foo: 'arg1', bar: 'arg2'})
})
describe('output: array', () => {
it('--bool', async () => {
const out = await parse(['--bool'], {
flags: {
bool: flags.boolean(),
},
})
expect(out.raw[0]).to.deep.include({flag: 'bool'})
})
it('arg1', async () => {
const out = await parse(['arg1'], {
args: [{name: 'foo'}],
})
expect(out.raw[0]).to.have.property('input', 'arg1')
})
it('parses args and flags', async () => {
const out = await parse(['foo', '--myflag', 'bar', 'baz'], {
args: [{name: 'myarg'}, {name: 'myarg2'}],
flags: {myflag: flags.string()},
})
expect(out.argv[0]).to.equal('foo')
expect(out.argv[1]).to.equal('baz')
expect(out.flags.myflag).to.equal('bar')
})
describe('flags', () => {
it('parses flags', async () => {
const out = await parse(['--myflag', '--myflag2'], {
flags: {myflag: flags.boolean(), myflag2: flags.boolean()},
})
expect(Boolean(out.flags.myflag)).to.equal(true)
expect(Boolean(out.flags.myflag2)).to.equal(true)
})
it('parses short flags', async () => {
const out = await parse(['-mf'], {
flags: {
force: flags.boolean({char: 'f'}),
myflag: flags.boolean({char: 'm'}),
},
})
expect(Boolean(out.flags.myflag)).to.equal(true)
expect(Boolean(out.flags.force)).to.equal(true)
})
})
it('parses flag value with "=" to separate', async () => {
const out = await parse(['--myflag=foo'], {
flags: {
myflag: flags.string({char: 'm'}),
},
})
expect(out.flags).to.deep.equal({myflag: 'foo'})
})
it('parses flag value with "=" in value', async () => {
const out = await parse(['--myflag', '=foo'], {
flags: {
myflag: flags.string({char: 'm'}),
},
})
expect(out.flags).to.deep.equal({myflag: '=foo'})
})
it('parses short flag value with "="', async () => {
const out = await parse(['-m=foo'], {
flags: {
myflag: flags.string({char: 'm'}),
},
})
expect(out.flags).to.deep.equal({myflag: 'foo'})
})
it('parses value of ""', async () => {
const out = await parse(['-m', ''], {
flags: {
myflag: flags.string({char: 'm'}),
},
})
expect(out.flags).to.deep.equal({myflag: ''})
})
it('requires required flag', async () => {
let message = ''
try {
await parse([], {
flags: {
myflag: flags.string({
description: 'flag description',
required: true,
}),
},
})
} catch (error: any) {
message = stripAnsi(error.message)
}
expect(message).to.equal(
'The following error occurred:\n Missing required flag myflag\nSee more help with --help',
)
})
it('removes flags from argv', async () => {
const out = await parse(['--myflag', 'bar', 'foo'], {
args: [{name: 'myarg'}],
flags: {myflag: flags.string()},
})
expect(out.flags).to.deep.equal({myflag: 'bar'})
expect(out.argv).to.deep.equal(['foo'])
})
describe('args', () => {
it('requires required args with names', async () => {
let message = ''
try {
await parse(['arg1'], {
args: [
{name: 'arg1', required: true},
{
description: 'arg2 desc',
name: 'arg2',
required: true,
},
{
description: 'arg3 desc',
name: 'arg3',
required: true,
},
],
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal(`Missing 2 required args:
arg2 arg2 desc
arg3 arg3 desc
See more help with --help`)
})
it('too many args', async () => {
let message = ''
try {
await parse(['arg1', 'arg2'], {
args: [{name: 'arg1', required: true}],
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('Unexpected argument: arg2\nSee more help with --help')
})
it('parses args', async () => {
const out = await parse(['foo', 'bar'], {
args: [{name: 'myarg'}, {name: 'myarg2'}],
})
expect(out.argv).to.deep.equal(['foo', 'bar'])
})
it('skips optional args', async () => {
const out = await parse(['foo'], {
args: [{name: 'myarg'}, {name: 'myarg2'}],
})
expect(out.argv).to.deep.equal(['foo'])
})
it('skips non-required args', async () => {
const out = await parse(['foo'], {
args: [
{name: 'myarg', required: false},
{name: 'myarg2', required: false},
],
})
expect(out.argv).to.deep.equal(['foo'])
})
it('parses something looking like a flag as an arg', async () => {
const out = await parse(['--foo'], {
args: [{name: 'myarg'}],
})
expect(out.argv).to.deep.equal(['--foo'])
})
it('parses - as an arg', async () => {
const out = await parse(['-'], {
args: [{name: 'myarg'}],
})
expect(out.argv).to.deep.equal(['-'])
})
})
describe('args - no args passed in, with defaults', () => {
it('two args: only first is required, only second has a default', async () => {
let message = ''
try {
await parse([], {
args: [
{name: 'arg1', required: true},
{name: 'arg2', required: false, default: 'some_default'},
],
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal(`Missing 1 required arg:
arg1
See more help with --help`)
})
it('two args: only first is required, only first has a default', async () => {
await parse([], {
args: [
{name: 'arg1', required: true, default: 'my_default'},
{name: 'arg2', required: false},
],
})
// won't reach here if thrown
expect(() => {}).to.not.throw()
})
it('two args: both have a default, only first is required', async () => {
await parse([], {
args: [
{name: 'arg1', required: true, default: 'my_default'},
{name: 'arg2', required: false, default: 'some_default'},
],
})
// won't reach here if thrown
expect(() => {}).to.not.throw()
})
})
describe('optional args should always be after required args', () => {
it('required arg after optional arg', async () => {
let message = ''
try {
await parse([], {
args: [
{name: 'arg1', required: false},
{name: 'arg2', required: true, default: 'some_default'},
],
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal(`Invalid argument spec:
arg1 (optional)
arg2 (required)
See more help with --help`)
})
it('required arg after multiple optional args', async () => {
let message = ''
try {
await parse([], {
args: [
{name: 'arg1', required: false},
{name: 'arg2', required: false, default: 'my_default'},
{name: 'arg3', required: false},
{name: 'arg4', required: true},
],
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal(`Invalid argument spec:
arg1 (optional)
arg2 (optional)
arg3 (optional)
arg4 (required)
See more help with --help`)
})
})
describe('multiple flags', () => {
it('parses multiple flags', async () => {
const out = await parse(['--bar', 'a', '--bar=b', '--foo=c', '--baz=d'], {
flags: {
foo: flags.string(),
bar: flags.string({multiple: true, required: true}),
baz: flags.string({required: true}),
},
})
expect(out.flags.foo!.toUpperCase()).to.equal('C')
expect(out.flags.baz.toUpperCase()).to.equal('D')
expect(out.flags.bar.join('|')).to.equal('a|b')
})
it('parses multiple flags on custom flags', async () => {
const out = await parse(['--foo', 'a', '--foo=b'], {
flags: {
foo: flags.option({multiple: true, parse: async i => i}),
},
})
expect(out.flags).to.deep.include({foo: ['a', 'b']})
})
})
describe('strict: false', () => {
it('skips flag parsing after "--"', async () => {
const out = await parse(['foo', 'bar', '--', '--myflag'], {
args: [{name: 'argOne'}],
flags: {myflag: flags.boolean()},
strict: false,
})
expect(out.argv).to.deep.equal(['foo', 'bar', '--myflag'])
expect(out.args).to.deep.equal({argOne: 'foo'})
})
describe('--: false', () => {
it('can be disabled', async () => {
const out = await parse(['foo', 'bar', '--', '--myflag'], {
args: [{name: 'argOne'}],
strict: false,
'--': false,
})
expect(out.argv).to.deep.equal(['foo', 'bar', '--', '--myflag'])
expect(out.args).to.deep.equal({argOne: 'foo'})
})
})
it('does not repeat arguments', async () => {
const out = await parse(['foo', '--myflag=foo bar'], {
strict: false,
})
expect(out.argv).to.deep.equal(['foo', '--myflag=foo bar'])
})
})
describe('integer flag', () => {
it('parses integers', async () => {
const out = await parse(['--int', '100'], {
flags: {int: flags.integer(), s: flags.string()},
})
expect(out.flags).to.deep.include({int: 100})
})
it('parses zero', async () => {
const out = await parse(['--int', '0'], {
flags: {int: flags.integer(), s: flags.string()},
})
expect(out.flags).to.deep.include({int: 0})
})
it('parses negative integers', async () => {
const out = await parse(['--int', '-123'], {
flags: {int: flags.integer(), s: flags.string()},
})
expect(out.flags).to.deep.include({int: -123})
})
it('does not parse floats', async () => {
let message = ''
try {
await parse(['--int', '3.14'], {
flags: {int: flags.integer()},
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('Expected an integer but received: 3.14')
})
it('does not parse fractions', async () => {
let message = ''
try {
await parse(['--int', '3/4'], {
flags: {int: flags.integer()},
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('Expected an integer but received: 3/4')
})
it('does not parse strings', async () => {
let message = ''
try {
await parse(['--int', 's10'], {
flags: {int: flags.integer()},
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('Expected an integer but received: s10')
})
describe('min/max', () => {
it('min pass equal', async () => {
const out = await parse(['--int', '10'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
expect(out.flags).to.deep.include({int: 10})
})
it('min pass gt', async () => {
const out = await parse(['--int', '11'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
expect(out.flags).to.deep.include({int: 11})
})
it('max pass lt', async () => {
const out = await parse(['--int', '19'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
expect(out.flags).to.deep.include({int: 19})
})
it('max pass equal', async () => {
const out = await parse(['--int', '20'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
expect(out.flags).to.deep.include({int: 20})
})
it('min fail lt', async () => {
let message = ''
try {
await parse(['--int', '9'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('Expected an integer greater than or equal to 10 but received: 9')
})
it('max fail gt', async () => {
let message = ''
try {
await parse(['--int', '21'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('Expected an integer less than or equal to 20 but received: 21')
})
})
})
describe('custom parse functions', () => {
const testIntPass = 6
const testIntFail = 7
const customParseException = 'NOT_OK'
const validateEvenNumberString = async (input:string) => Number.parseInt(input, 10) % 2 === 0 ? Number.parseInt(input, 10) : assert.fail(customParseException)
it('accepts custom parse that passes', async () => {
const out = await parse([`--int=${testIntPass}`], {
flags: {int: flags.integer({parse: validateEvenNumberString})},
})
expect(out.flags).to.deep.include({int: testIntPass})
})
it('accepts custom parse that fails', async () => {
try {
const out = await parse([`--int=${testIntFail}`], {
flags: {int: flags.integer({parse: validateEvenNumberString})},
})
throw new Error(`Should have thrown an error ${JSON.stringify(out)}`)
} catch (error_) {
const error = error_ as Error
expect(error.message).to.equal(
customParseException)
}
})
})
})
it('--no-color', async () => {
const out = await parse(['--no-color'], {})
expect(out.flags).to.deep.include({color: false})
})
describe('parse', () => {
it('parse', async () => {
const out = await parse(['--foo=bar', '100'], {
args: [{name: 'num', parse: async i => Number.parseInt(i, 10)}],
flags: {foo: flags.string({parse: async input => input.toUpperCase()})},
})
expect(out.flags).to.deep.include({foo: 'BAR'})
expect(out.args).to.deep.include({num: 100})
expect(out.argv).to.deep.equal([100])
})
it('parse with a default does not parse default', async () => {
const out = await parse([], {
flags: {foo: flags.string({parse: async input => input.toUpperCase(), default: 'baz'})},
})
expect(out.flags).to.deep.include({foo: 'baz'})
})
describe('parse with a default/value of another type (class)', async () => {
class TestClass {
public prop: string;
constructor(input: string) {
this.prop = input
}
}
it('uses default via value', async () => {
const out = await parse([], {
flags: {
foo: flags.build<TestClass>({
parse: async input => new TestClass(input),
default: new TestClass('baz'),
})(),
},
})
expect(out.flags.foo?.prop).to.equal('baz')
})
it('uses default via function', async () => {
const out = await parse([], {
flags: {
foo: flags.build<TestClass>({
parse: async input => new TestClass(input),
default: async () => new TestClass('baz'),
})(),
},
})
expect(out.flags.foo?.prop).to.equal('baz')
})
it('uses parser when value provided', async () => {
const out = await parse(['--foo=bar'], {
flags: {
foo: flags.build<TestClass>({
parse: async input => new TestClass(input),
default: new TestClass('baz'),
})(),
},
})
expect(out.flags.foo?.prop).to.equal('bar')
})
})
// it('gets arg/flag in context', async () => {
// const out = await parse({
// args: [{ name: 'num', parse: (_, ctx) => ctx.arg.name!.toUpperCase() }],
// argv: ['--foo=bar', '100'],
// flags: { foo: flags.string({ parse: (_, ctx) => ctx.flag.name.toUpperCase() }) },
// })
// expect(out.flags).to.deep.include({ foo: 'FOO' })
// expect(out.args).to.deep.include({ num: 'NUM' })
// })
})
describe('flag with multiple inputs', () => {
it('flag multiple with flag in the middle', async () => {
const out = await parse(['--foo=bar', '--foo', '100', '--hello', 'world'], {
flags: {foo: flags.string({multiple: true}), hello: flags.string()},
})
expect(out.flags).to.deep.include({foo: ['bar', '100']})
expect(out.flags).to.deep.include({hello: 'world'})
})
it('flag multiple without flag in the middle', async () => {
const out = await parse(
['--foo', './a.txt', './b.txt', './c.txt', '--hello', 'world'],
{
flags: {
foo: flags.string({multiple: true}),
hello: flags.string(),
},
},
)
expect(out.flags).to.deep.include({
foo: ['./a.txt', './b.txt', './c.txt'],
})
expect(out.flags).to.deep.include({hello: 'world'})
})
it('flag multiple with arguments', async () => {
const out = await parse(
['--foo', './a.txt', './b.txt', './c.txt', '--', '15'],
{
args: [{name: 'num'}],
flags: {foo: flags.string({multiple: true})},
},
)
expect(out.flags).to.deep.include({
foo: ['./a.txt', './b.txt', './c.txt'],
})
expect(out.args).to.deep.include({num: '15'})
})
})
describe('defaults', () => {
it('generates metadata for defaults', async () => {
const out = await parse(['-n', 'heroku'], {
flags: {
name: flags.string({
char: 'n',
}),
startup: flags.string({
char: 's',
default: 'apero',
}),
},
})
expect(out.metadata.flags).to.deep.equal({
startup: {setFromDefault: true},
})
})
it('defaults', async () => {
const out = await parse([], {
args: [{name: 'baz', default: 'BAZ'}],
flags: {foo: flags.string({default: 'bar'})},
})
expect(out.args).to.deep.include({baz: 'BAZ'})
expect(out.argv).to.deep.equal(['BAZ'])
expect(out.flags).to.deep.include({foo: 'bar'})
})
it('accepts falsy', async () => {
const out = await parse([], {
args: [{name: 'baz', default: false}],
})
expect(out.args).to.deep.include({baz: false})
})
it('default as function', async () => {
const out = await parse([], {
args: [{name: 'baz', default: () => 'BAZ'}],
flags: {foo: flags.string({default: async () => 'bar'})},
})
expect(out.args).to.deep.include({baz: 'BAZ'})
expect(out.argv).to.deep.equal(['BAZ'])
expect(out.flags).to.deep.include({foo: 'bar'})
})
it('default has options', async () => {
const def: Interfaces.Default<string | undefined> = async ({options}) =>
options.description
const out = await parse([], {
// args: [{ name: 'baz', default: () => 'BAZ' }],
flags: {foo: flags.string({description: 'bar', default: def})},
})
// expect(out.args).to.deep.include({ baz: 'BAZ' })
// expect(out.argv).to.deep.include(['BAZ'])
expect(out.flags).to.deep.include({foo: 'bar'})
})
it('can default to a different flag', async () => {
const def: Interfaces.Default<string | undefined> = async opts => opts.flags.foo
const out = await parse(['--foo=bar'], {
flags: {
bar: flags.string({
default: def,
}),
foo: flags.string(),
},
})
expect(out.flags).to.deep.include({foo: 'bar', bar: 'bar'})
})
})
describe('boolean defaults', () => {
it('default is true', async () => {
const out = await parse([], {
flags: {
color: flags.boolean({default: true}),
},
})
expect(out).to.deep.include({flags: {color: true}})
})
it('default is false', async () => {
const out = await parse([], {
flags: {
color: flags.boolean({default: false}),
},
})
expect(out).to.deep.include({flags: {color: false}})
})
it('default as function', async () => {
const out = await parse([], {
flags: {
color: flags.boolean({default: async () => true}),
},
})
expect(out).to.deep.include({flags: {color: true}})
})
it('overridden true default', async () => {
const out = await parse(['--no-color'], {
flags: {
color: flags.boolean({allowNo: true, default: true}),
},
})
expect(out).to.deep.include({flags: {color: false}})
})
it('overridden false default', async () => {
const out = await parse(['--color'], {
flags: {
color: flags.boolean({default: false}),
},
})
expect(out).to.deep.include({flags: {color: true}})
})
})
describe('custom option', () => {
it('can pass parse fn', async () => {
const foo = flags.option({char: 'f', parse: async () => 100})
const out = await parse(['-f', 'bar'], {
flags: {foo},
})
expect(out.flags).to.deep.include({foo: 100})
})
})
describe('build', () => {
it('can pass parse fn', async () => {
const foo = flags.build({char: 'f', parse: async () => 100})
const out = await parse(['-f', 'bar'], {
flags: {foo: foo()},
})
expect(out.flags).to.deep.include({foo: 100})
})
it('does not require parse fn', async () => {
const foo = flags.build({char: 'f'})
const out = await parse(['-f', 'bar'], {
flags: {foo: foo()},
})
expect(out.flags).to.deep.include({foo: 'bar'})
})
})
describe('flag options', () => {
it('accepts valid option', async () => {
const out = await parse(['--foo', 'myotheropt'], {
flags: {foo: flags.string({options: ['myopt', 'myotheropt']})},
})
expect(out.flags.foo).to.equal('myotheropt')
})
it('fails when invalid', async () => {
let message = ''
try {
await parse(['--foo', 'invalidopt'], {
flags: {foo: flags.string({options: ['myopt', 'myotheropt']})},
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('Expected --foo=invalidopt to be one of: myopt, myotheropt\nSee more help with --help')
})
it('fails when invalid env var', async () => {
let message = ''
process.env.TEST_FOO = 'invalidopt'
try {
await parse([], {
flags: {foo: flags.string({options: ['myopt', 'myotheropt'], env: 'TEST_FOO'})},
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('Expected --foo=invalidopt to be one of: myopt, myotheropt\nSee more help with --help')
})
it('accepts valid option env var', async () => {
process.env.TEST_FOO = 'myopt'
const out = await parse([], {
flags: {foo: flags.string({options: ['myopt', 'myotheropt'], env: 'TEST_FOO'})},
})
expect(out.flags.foo).to.equal('myopt')
})
})
describe('url flag', () => {
it('accepts valid url', async () => {
const out = await parse(['--foo', 'https://example.com'], {
flags: {foo: flags.url()},
})
expect(out.flags.foo).to.be.instanceOf(URL)
expect(out.flags.foo?.href).to.equal('https://example.com/')
})
it('fails when invalid', async () => {
let message = ''
try {
await parse(['--foo', 'example'], {
flags: {foo: flags.url()},
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('Expected a valid url but received: example')
})
})
describe('arg options', () => {
it('accepts valid option', async () => {
const out = await parse(['myotheropt'], {
args: [{name: 'foo', options: ['myopt', 'myotheropt']}],
})
expect(out.args.foo).to.equal('myotheropt')
})
it('fails when invalid', async () => {
let message = ''
try {
await parse(['invalidopt'], {
args: [{name: 'foo', options: ['myopt', 'myotheropt']}],
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('Expected invalidopt to be one of: myopt, myotheropt\nSee more help with --help')
})
})
describe('env', () => {
describe('string', () => {
it('accepts as environment variable', async () => {
process.env.TEST_FOO = '101'
const out = await parse([], {
flags: {foo: flags.string({env: 'TEST_FOO'})},
})
expect(out.flags.foo).to.equal('101')
delete process.env.TEST_FOO
})
})
describe('boolean', () => {
const truthy = ['true', 'TRUE', '1', 'yes', 'YES', 'y', 'Y']
for (const value of truthy) {
it(`accepts '${value}' as a truthy environment variable`, async () => {
process.env.TEST_FOO = value
const out = await parse([], {
flags: {
foo: flags.boolean({env: 'TEST_FOO'}),
},
})
expect(out.flags.foo).to.be.true
delete process.env.TEST_FOO
})
}
const falsy = ['false', 'FALSE', '0', 'no', 'NO', 'n', 'N']
for (const value of falsy) {
it(`accepts '${value}' as a falsy environment variable`, async () => {
process.env.TEST_FOO = value
const out = await parse([], {
flags: {
foo: flags.boolean({env: 'TEST_FOO'}),
},
})
expect(out.flags.foo).to.be.false
delete process.env.TEST_FOO
})
}
})
})
describe('flag context', () => {
it('accepts context in parse', async () => {
const out = await parse(['--foo'], {
context: {a: 101},
flags: {
foo: flags.boolean({
parse: async (_: any, ctx: any) => ctx.a,
}),
},
})
expect(out.flags.foo).to.equal(101)
})
})
it('parses multiple flags', async () => {
const out = await parse(['--foo=a', '--foo', 'b'], {
flags: {foo: flags.string()},
})
expect(out.flags.foo).to.equal('b')
})
describe('dependsOn', () => {
it('ignores', async () => {
await parse([], {
flags: {
foo: flags.string({dependsOn: ['bar']}),
bar: flags.string({char: 'b'}),
},
})
})
it('succeeds', async () => {
const out = await parse(['--foo', 'a', '-bb'], {
flags: {
foo: flags.string({dependsOn: ['bar']}),
bar: flags.string({char: 'b'}),
},
})
expect(out.flags.foo).to.equal('a')
expect(out.flags.bar).to.equal('b')
})
it('fails', async () => {
let message = ''
try {
await parse(['--foo', 'a'], {
flags: {
foo: flags.string({dependsOn: ['bar']}),
bar: flags.string({char: 'b'}),
},
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('The following error occurred:\n All of the following must be provided when using --foo: --bar\nSee more help with --help')
})
})
describe('exclusive', () => {
it('ignores', async () => {
await parse([], {
flags: {
foo: flags.string({exclusive: ['bar']}),
bar: flags.string({char: 'b'}),
},
})
})
it('succeeds', async () => {
const out = await parse(['--foo', 'a'], {
flags: {
foo: flags.string({exclusive: ['bar']}),
bar: flags.string({char: 'b'}),
},
})
expect(out.flags.foo).to.equal('a')
})
it('fails', async () => {
let message = ''
try {
await parse(['--foo', 'a', '-bb'], {
flags: {
foo: flags.string({exclusive: ['bar']}),
bar: flags.string({char: 'b'}),
},
})
} catch (error: any) {
message = error.message
}
expect(message).to.equal('The following error occurred:\n --bar=b cannot also be provided when using --foo\nSee more help with --help')
})
})