-
Notifications
You must be signed in to change notification settings - Fork 118
/
yargs-parser.js
2091 lines (1819 loc) · 60.7 KB
/
yargs-parser.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
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
/* global beforeEach, describe, it */
require('chai').should()
var expect = require('chai').expect
var fs = require('fs')
var parser = require('../')
var path = require('path')
describe('yargs-parser', function () {
it('should parse a "short boolean"', function () {
var parse = parser([ '-b' ])
parse.should.have.property('b').to.be.ok.and.be.a('boolean')
parse.should.have.property('_').with.length(0)
})
it('should parse a "long boolean"', function () {
var parse = parser('--bool')
parse.should.have.property('bool', true)
parse.should.have.property('_').with.length(0)
})
it('should place bare options in the _ array', function () {
var parse = parser('foo bar baz')
parse.should.have.property('_').and.deep.equal(['foo', 'bar', 'baz'])
})
it('should set the value of the final option in a group to the next supplied value', function () {
var parse = parser(['-cats', 'meow'])
parse.should.have.property('c', true)
parse.should.have.property('a', true)
parse.should.have.property('t', true)
parse.should.have.property('s', 'meow')
parse.should.have.property('_').with.length(0)
})
it('should set the value of a single long option to the next supplied value', function () {
var parse = parser(['--pow', 'xixxle'])
parse.should.have.property('pow', 'xixxle')
parse.should.have.property('_').with.length(0)
})
it('should set the value of a single long option to the next supplied value, even if the value is empty', function () {
var parse = parser(['--pow', ''])
parse.should.have.property('pow', '')
parse.should.have.property('_').with.length(0)
})
it('should set the value of a single long option if an = was used', function () {
var parse = parser(['--pow=xixxle'])
parse.should.have.property('pow', 'xixxle')
parse.should.have.property('_').with.length(0)
})
it('should set the value of multiple long options to the next supplied values relative to each', function () {
var parse = parser(['--host', 'localhost', '--port', '555'])
parse.should.have.property('host', 'localhost')
parse.should.have.property('port', 555)
parse.should.have.property('_').with.length(0)
})
it('should set the value of multiple long options if = signs were used', function () {
var parse = parser(['--host=localhost', '--port=555'])
parse.should.have.property('host', 'localhost')
parse.should.have.property('port', 555)
parse.should.have.property('_').with.length(0)
})
it('should still set values appropriately if a mix of short, long, and grouped short options are specified', function () {
var parse = parser(['-h', 'localhost', '-fp', '555', 'script.js'])
parse.should.have.property('f', true)
parse.should.have.property('p', 555)
parse.should.have.property('h', 'localhost')
parse.should.have.property('_').and.deep.equal(['script.js'])
})
it('should still set values appropriately if a mix of short and long options are specified', function () {
var parse = parser(['-h', 'localhost', '--port', '555'])
parse.should.have.property('h', 'localhost')
parse.should.have.property('port', 555)
parse.should.have.property('_').with.length(0)
})
it('should explicitly set a boolean option to false if preceeded by "--no-"', function () {
var parse = parser(['--no-moo'])
parse.should.have.property('moo', false)
parse.should.have.property('_').with.length(0)
})
it('should still set values appropriately if we supply a comprehensive list of various types of options', function () {
var parse = parser([
'--name=meowmers', 'bare', '-cats', 'woo',
'-h', 'awesome', '--multi=quux',
'--key', 'value',
'-b', '--bool', '--no-meep', '--multi=baz',
'--', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'
])
parse.should.have.property('c', true)
parse.should.have.property('a', true)
parse.should.have.property('t', true)
parse.should.have.property('s', 'woo')
parse.should.have.property('h', 'awesome')
parse.should.have.property('b', true)
parse.should.have.property('bool', true)
parse.should.have.property('key', 'value')
parse.should.have.property('multi').and.deep.equal(['quux', 'baz'])
parse.should.have.property('meep', false)
parse.should.have.property('name', 'meowmers')
parse.should.have.property('_').and.deep.equal(['bare', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'])
})
it('should parse numbers appropriately', function () {
var argv = parser([
'-x', '1234',
'-y', '5.67',
'-z', '1e7',
'-w', '10f',
'--hex', '0xdeadbeef',
'789'
])
argv.should.have.property('x', 1234).and.be.a('number')
argv.should.have.property('y', 5.67).and.be.a('number')
argv.should.have.property('z', 1e7).and.be.a('number')
argv.should.have.property('w', '10f').and.be.a('string')
argv.should.have.property('hex', 0xdeadbeef).and.be.a('number')
argv.should.have.property('_').and.deep.equal([789])
argv._[0].should.be.a('number')
})
// addresses: https://github.com/yargs/yargs-parser/issues/33
it('should handle parsing negative #s', function () {
var argv = parser([
'-33', '-177', '33',
'--n1', '-33',
'-n', '-44',
'--n2=-55',
'--foo.bar', '-33',
'-o=-55',
'--bounds', '-180', '99', '-180', '90',
'--other', '-99', '-220'
], {
array: 'bounds',
narg: {'other': 2}
})
argv._.should.deep.equal([-33, -177, 33])
argv.n1.should.equal(-33)
argv.n.should.equal(-44)
argv.n2.should.equal(-55)
argv.foo.bar.should.equal(-33)
argv.o.should.equal(-55)
argv.bounds.should.deep.equal([-180, 99, -180, 90])
argv.other.should.deep.equal([-99, -220])
})
it('should set the value of a single short option to the next supplied value, even if the value is empty', function () {
var parse = parser(['-p', ''])
parse.should.have.property('p', '')
parse.should.have.property('_').with.length(0)
})
it('should not set the next value as the value of a short option if that option is explicitly defined as a boolean', function () {
var parse = parser([ '-t', 'moo' ], {
boolean: 't'
})
parse.should.have.property('t', true).and.be.a('boolean')
parse.should.have.property('_').and.deep.equal(['moo'])
})
it('should set boolean options values if the next value is "true" or "false"', function () {
var parse = parser(['--verbose', 'false', 'moo', '-t', 'true'], {
boolean: ['t', 'verbose'],
default: {
verbose: true
}
})
parse.should.have.property('verbose', false).and.be.a('boolean')
parse.should.have.property('t', true).and.be.a('boolean')
parse.should.have.property('_').and.deep.equal(['moo'])
})
it('should not set boolean options values if the next value only contains the words "true" or "false"', function () {
var parse = parser(['--verbose', 'aaatrueaaa', 'moo', '-t', 'aaafalseaaa'], {
boolean: ['t', 'verbose']
})
parse.should.have.property('verbose', true).and.be.a('boolean')
parse.should.have.property('t', true).and.be.a('boolean')
parse.should.have.property('_').and.deep.equal(['aaatrueaaa', 'moo', 'aaafalseaaa'])
})
it('should allow defining options as boolean in groups', function () {
var parse = parser([ '-x', '-z', 'one', 'two', 'three' ], {
boolean: ['x', 'y', 'z']
})
parse.should.have.property('x', true).and.be.a('boolean')
parse.should.have.property('y', false).and.be.a('boolean')
parse.should.have.property('z', true).and.be.a('boolean')
parse.should.have.property('_').and.deep.equal(['one', 'two', 'three'])
})
it('should preserve newlines in option values', function () {
var args = parser(['-s', 'X\nX'])
args.should.have.property('_').with.length(0)
args.should.have.property('s', 'X\nX')
// reproduce in bash:
// VALUE="new
// line"
// node program.js --s="$VALUE"
args = parser(['--s=X\nX'])
args.should.have.property('_').with.length(0)
args.should.have.property('s', 'X\nX')
})
it('should not convert numbers to type number if explicitly defined as strings', function () {
var s = parser([ '-s', '0001234' ], {
string: 's'
}).s
s.should.be.a('string').and.equal('0001234')
var x = parser([ '-x', '56' ], {
string: ['x']
}).x
x.should.be.a('string').and.equal('56')
})
it('should default numbers to undefined', function () {
var n = parser([ '-n' ], {
number: ['n']
}).n
expect(n).to.equal(undefined)
})
it('should default number to NaN if value is not a valid number', function () {
var n = parser([ '-n', 'string' ], {
number: ['n']
}).n
expect(n).to.deep.equal(NaN)
})
// Fixes: https://github.com/bcoe/yargs/issues/68
it('should parse flag arguments with no right-hand-value as strings, if defined as strings', function () {
var s = parser([ '-s' ], {
string: ['s']
}).s
s.should.be.a('string').and.equal('')
s = parser([ '-sf' ], {
string: ['s']
}).s
s.should.be.a('string').and.equal('')
s = parser([ '--string' ], {
string: ['string']
}).string
s.should.be.a('string').and.equal('')
})
it('should leave all non-hyphenated values as strings if _ is defined as a string', function () {
var s = parser([ ' ', ' ' ], {
string: ['_']
})._
s.should.have.length(2)
s[0].should.be.a('string').and.equal(' ')
s[1].should.be.a('string').and.equal(' ')
})
describe('normalize', function () {
it('should normalize redundant paths', function () {
var a = parser([ '-s', ['', 'tmp', '..', ''].join(path.sep) ], {
alias: {
s: ['save']
},
normalize: 's'
})
a.should.have.property('s', path.sep)
a.should.have.property('save', path.sep)
})
it('should normalize redundant paths when a value is later assigned', function () {
var a = parser(['-s'], {
normalize: ['s']
})
a.should.have.property('s', true)
a.s = ['', 'path', 'to', 'new', 'dir', '..', '..', ''].join(path.sep)
a.s.should.equal(['', 'path', 'to', ''].join(path.sep))
})
it('should normalize when key is also an array', function () {
var a = parser([ '-s', ['', 'tmp', '..', ''].join(path.sep), ['', 'path', 'to', 'new', 'dir', '..', '..', ''].join(path.sep) ], {
alias: {
s: ['save']
},
normalize: 's',
array: 's'
})
var expected = [path.sep, ['', 'path', 'to', ''].join(path.sep)]
a.should.have.property('s').and.deep.equal(expected)
a.should.have.property('save').and.deep.equal(expected)
})
})
describe('alias', function () {
it('should set alias value to the same value as the full option', function () {
var argv = parser([ '-f', '11', '--zoom', '55' ], {
alias: {
z: ['zoom']
}
})
argv.should.have.property('zoom', 55)
argv.should.have.property('z', 55)
argv.should.have.property('f', 11)
})
it('should allow multiple aliases to be specified', function () {
var argv = parser([ '-f', '11', '--zoom', '55' ], {
alias: {
z: ['zm', 'zoom']
}
})
argv.should.have.property('zoom', 55)
argv.should.have.property('z', 55)
argv.should.have.property('zm', 55)
argv.should.have.property('f', 11)
})
// regression, see https://github.com/chevex/yargs/issues/63
it('should not add the same key to argv multiple times, when creating camel-case aliases', function () {
var argv = parser(['--health-check=banana', '--second-key', 'apple', '-t=blarg'], {
alias: {
h: ['health-check'],
'second-key': ['s'],
'third-key': ['t']
},
default: {
h: 'apple',
'second-key': 'banana',
'third-key': 'third'
}
})
// before this fix, yargs failed parsing
// one but not all forms of an arg.
argv.secondKey.should.eql('apple')
argv.s.should.eql('apple')
argv['second-key'].should.eql('apple')
argv.healthCheck.should.eql('banana')
argv.h.should.eql('banana')
argv['health-check'].should.eql('banana')
argv.thirdKey.should.eql('blarg')
argv.t.should.eql('blarg')
argv['third-key'].should.eql('blarg')
})
it('should allow transitive aliases to be specified', function () {
var argv = parser([ '-f', '11', '--zoom', '55' ], {
alias: {
z: 'zm',
zm: 'zoom'
}
})
argv.should.have.property('zoom', 55)
argv.should.have.property('z', 55)
argv.should.have.property('zm', 55)
argv.should.have.property('f', 11)
})
it('should merge two lists of aliases if they collide', function () {
var argv = parser(['-f', '11', '--zoom', '55'], {
alias: {
z: 'zm',
zoom: 'zoop',
zm: 'zoom'
}
})
argv.should.have.property('zoom', 55)
argv.should.have.property('zoop', 55)
argv.should.have.property('z', 55)
argv.should.have.property('zm', 55)
argv.should.have.property('f', 11)
})
})
it('should assign data after forward slash to the option before the slash', function () {
var parse = parser(['-I/foo/bar/baz'])
parse.should.have.property('_').with.length(0)
parse.should.have.property('I', '/foo/bar/baz')
parse = parser(['-xyz/foo/bar/baz'])
parse.should.have.property('x', true)
parse.should.have.property('y', true)
parse.should.have.property('z', '/foo/bar/baz')
parse.should.have.property('_').with.length(0)
})
describe('config', function () {
var jsonPath = path.resolve(__dirname, './fixtures/config.json')
// See: https://github.com/chevex/yargs/issues/12
it('should load options and values from default config if specified', function () {
var argv = parser([ '--foo', 'bar' ], {
alias: {
z: 'zoom'
},
default: {
settings: jsonPath
},
config: 'settings'
})
argv.should.have.property('herp', 'derp')
argv.should.have.property('zoom', 55)
argv.should.have.property('foo').and.deep.equal('bar')
})
it('should use value from config file, if argv value is using default value', function () {
var argv = parser([], {
alias: {
z: 'zoom'
},
config: ['settings'],
default: {
settings: jsonPath,
foo: 'banana'
}
})
argv.should.have.property('herp', 'derp')
argv.should.have.property('zoom', 55)
argv.should.have.property('foo').and.deep.equal('baz')
})
it('should use value from config file, if argv key is a boolean', function () {
var argv = parser([], {
config: ['settings'],
default: {
settings: jsonPath
},
boolean: ['truthy']
})
argv.should.have.property('truthy', true)
})
it('should use value from cli, if cli overrides boolean argv key', function () {
var argv = parser(['--no-truthy'], {
config: ['settings'],
default: {
settings: jsonPath
},
boolean: ['truthy']
})
argv.should.have.property('truthy', false)
})
it('should use cli value, if cli value is set and both cli and default value match', function () {
var argv = parser(['--foo', 'banana'], {
alias: {
z: 'zoom'
},
config: ['settings'],
default: {
settings: jsonPath,
foo: 'banana'
}
})
argv.should.have.property('herp', 'derp')
argv.should.have.property('zoom', 55)
argv.should.have.property('foo').and.deep.equal('banana')
})
it("should allow config to be set as flag in 'option'", function () {
var argv = parser([ '--settings', jsonPath, '--foo', 'bar' ], {
alias: {
z: 'zoom'
},
config: ['settings']
})
argv.should.have.property('herp', 'derp')
argv.should.have.property('zoom', 55)
argv.should.have.property('foo').and.deep.equal('bar')
})
it('should load options and values from a JS file when config has .js extention', function () {
var jsPath = path.resolve(__dirname, './fixtures/settings.js')
var argv = parser([ '--settings', jsPath, '--foo', 'bar' ], {
config: ['settings']
})
argv.should.have.property('herp', 'derp')
argv.should.have.property('foo', 'bar')
argv.should.have.property('calculate').and.be.a('function')
})
it('should raise an appropriate error if JSON file is not found', function () {
var argv = parser.detailed(['--settings', 'fake.json', '--foo', 'bar'], {
alias: {
z: 'zoom'
},
config: ['settings']
})
argv.error.message.should.equal('Invalid JSON config file: fake.json')
})
// see: https://github.com/bcoe/yargs/issues/172
it('should not raise an exception if config file is set as default argument value', function () {
var argv = parser.detailed([], {
default: {
config: 'foo.json'
},
config: ['config']
})
expect(argv.error).to.equal(null)
})
it('should load nested options from config file', function () {
var jsonPath = path.resolve(__dirname, './fixtures/nested_config.json')
var argv = parser(['--settings', jsonPath, '--nested.foo', 'bar'], {
config: ['settings']
})
argv.should.have.property('a', 'a')
argv.should.have.property('b', 'b')
argv.should.have.property('nested').and.deep.equal({
foo: 'bar',
bar: 'bar'
})
})
it('should use nested value from config file, if argv value is using default value', function () {
var jsonPath = path.resolve(__dirname, './fixtures/nested_config.json')
var argv = parser(['--settings', jsonPath], {
config: ['settings'],
default: {
'nested.foo': 'banana'
}
})
argv.should.have.property('a', 'a')
argv.should.have.property('b', 'b')
argv.should.have.property('nested').and.deep.equal({
foo: 'baz',
bar: 'bar'
})
})
it('allows a custom parsing function to be provided', function () {
var jsPath = path.resolve(__dirname, './fixtures/config.txt')
var argv = parser([ '--settings', jsPath, '--foo', 'bar' ], {
config: {
settings: function (configPath) {
// as an example, parse an environment
// variable style config:
// FOO=99
// BATMAN=grumpy
var config = {}
var txt = fs.readFileSync(configPath, 'utf-8')
txt.split(/\r?\n/).forEach(function (l) {
var kv = l.split('=')
config[kv[0].toLowerCase()] = kv[1]
})
return config
}
}
})
argv.batman.should.equal('grumpy')
argv.awesome.should.equal('banana')
argv.foo.should.equal('bar')
})
it('allows a custom parsing function to be provided as an alias', function () {
var jsPath = path.resolve(__dirname, './fixtures/config.json')
var argv = parser([ '--settings', jsPath, '--foo', 'bar' ], {
config: {
s: function (configPath) {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'))
}
},
alias: {
s: ['settings']
}
})
argv.should.have.property('herp', 'derp')
argv.should.have.property('foo', 'bar')
})
it('outputs an error returned by the parsing function', function () {
var argv = parser.detailed(['--settings=./package.json'], {
config: {
settings: function (configPath) {
return Error('someone set us up the bomb')
}
}
})
argv.error.message.should.equal('someone set us up the bomb')
})
it('outputs an error if thrown by the parsing function', function () {
var argv = parser.detailed(['--settings=./package.json'], {
config: {
settings: function (configPath) {
throw Error('someone set us up the bomb')
}
}
})
argv.error.message.should.equal('someone set us up the bomb')
})
})
describe('config objects', function () {
it('should load options from config object', function () {
var argv = parser([ '--foo', 'bar' ], {
configObjects: [{
apple: 'apple',
banana: 42,
foo: 'baz'
}]
})
argv.should.have.property('apple', 'apple')
argv.should.have.property('banana', 42)
argv.should.have.property('foo', 'bar')
})
it('should use value from config object, if argv value is using default value', function () {
var argv = parser([], {
configObjects: [{
apple: 'apple',
banana: 42,
foo: 'baz'
}],
default: {
foo: 'banana'
}
})
argv.should.have.property('apple', 'apple')
argv.should.have.property('banana', 42)
argv.should.have.property('foo', 'baz')
})
it('should use value from config object to all aliases', function () {
var argv = parser([], {
configObjects: [{
apple: 'apple',
banana: 42,
foo: 'baz'
}],
alias: {
a: ['apple'],
banana: ['b']
}
})
argv.should.have.property('apple', 'apple')
argv.should.have.property('a', 'apple')
argv.should.have.property('banana', 42)
argv.should.have.property('b', 42)
argv.should.have.property('foo', 'baz')
})
it('should load nested options from config object', function () {
var argv = parser(['--nested.foo', 'bar'], {
configObjects: [{
a: 'a',
nested: {
foo: 'baz',
bar: 'bar'
},
b: 'b'
}]
})
argv.should.have.property('a', 'a')
argv.should.have.property('b', 'b')
argv.should.have.property('nested').and.deep.equal({
foo: 'bar',
bar: 'bar'
})
})
it('should use nested value from config object, if argv value is using default value', function () {
var argv = parser([], {
configObjects: [{
a: 'a',
nested: {
foo: 'baz',
bar: 'bar'
},
b: 'b'
}],
default: {
'nested.foo': 'banana'
}
})
argv.should.have.property('a', 'a')
argv.should.have.property('b', 'b')
argv.should.have.property('nested').and.deep.equal({
foo: 'baz',
bar: 'bar'
})
})
})
describe('dot notation', function () {
it('should allow object graph traversal via dot notation', function () {
var argv = parser([
'--foo.bar', '3', '--foo.baz', '4',
'--foo.quux.quibble', '5', '--foo.quux.o_O',
'--beep.boop'
])
argv.should.have.property('foo').and.deep.equal({
bar: 3,
baz: 4,
quux: {
quibble: 5,
o_O: true
}
})
argv.should.have.property('beep').and.deep.equal({ boop: true })
})
it('should apply defaults to dot notation arguments', function () {
var argv = parser([], {
default: {
'foo.bar': 99
}
})
argv.foo.bar.should.eql(99)
})
// see #279
it('should allow default to be overridden when an alias is provided', function () {
var argv = parser(['--foo.bar', '200'], {
default: {
'foo.bar': 99
}
})
argv.foo.bar.should.eql(200)
})
// see #279
it('should also override alias', function () {
var argv = parser(['--foo.bar', '200'], {
alias: {
'foo.bar': ['f']
},
default: {
'foo.bar': 99
}
})
argv.f.should.eql(200)
})
// see #279
it('should not set an undefined dot notation key', function () {
var argv = parser(['--foo.bar', '200'], {
default: {
'foo.bar': 99
},
alias: {
'foo.bar': ['f']
}
})
;('foo.bar' in argv).should.equal(false)
})
it('should respect .string() for dot notation arguments', function () {
var argv = parser(['--foo.bar', '99', '--bar.foo=99'], {
string: ['foo.bar']
})
argv.foo.bar.should.eql('99')
argv.bar.foo.should.eql(99)
})
it('should populate aliases when dot notation is used', function () {
var argv = parser(['--foo.bar', '99'], {
alias: {
foo: ['f']
}
})
argv.f.bar.should.eql(99)
})
it('should populate aliases when nested dot notation is used', function () {
var argv = parser(['--foo.bar.snuh', '99', '--foo.apple', '33', '--foo.bar.cool', '11'], {
alias: {
foo: ['f']
}
})
argv.f.bar.snuh.should.eql(99)
argv.foo.bar.snuh.should.eql(99)
argv.f.apple.should.eql(33)
argv.foo.apple.should.eql(33)
argv.f.bar.cool.should.eql(11)
argv.foo.bar.cool.should.eql(11)
})
it("should allow flags to use dot notation, when seperated by '='", function () {
var argv = parser(['-f.foo=99'])
argv.f.foo.should.eql(99)
})
it("should allow flags to use dot notation, when seperated by ' '", function () {
var argv = parser(['-f.foo', '99'])
argv.f.foo.should.eql(99)
})
it('should allow flags to use dot notation when no right-hand-side is given', function () {
var argv = parser(['-f.foo', '99', '-f.bar'])
argv.f.foo.should.eql(99)
argv.f.bar.should.eql(true)
})
})
it('should set boolean and alias using explicit true', function () {
var aliased = [ '-h', 'true' ]
var aliasedArgv = parser(aliased, {
boolean: ['h'],
alias: {
h: ['herp']
}
})
aliasedArgv.should.have.property('herp', true)
aliasedArgv.should.have.property('h', true)
aliasedArgv.should.have.property('_').with.length(0)
})
// regression, see https://github.com/substack/node-optimist/issues/71
it('should set boolean and --x=true', function () {
var parsed = parser(['--boool', '--other=true'], {
boolean: ['boool']
})
parsed.should.have.property('boool', true)
parsed.should.have.property('other', 'true')
parsed = parser(['--boool', '--other=false'], {
boolean: ['boool']
})
parsed.should.have.property('boool', true)
parsed.should.have.property('other', 'false')
})
// regression, see https://github.com/chevex/yargs/issues/66
it('should set boolean options values if next value is "true" or "false" with = as separator', function () {
var argv = parser(['--bool=false'], {
boolean: ['b'],
alias: {
b: ['bool']
},
default: {
b: true
}
})
argv.bool.should.eql(false)
})
describe('short options', function () {
it('should set the value of multiple single short options to the next supplied values relative to each', function () {
var parse = parser(['-h', 'localhost', '-p', '555'])
parse.should.have.property('h', 'localhost')
parse.should.have.property('p', 555)
parse.should.have.property('_').with.length(0)
})
it('should set the value of a single short option to the next supplied value', function () {
var parse = parser(['-h', 'localhost'])
parse.should.have.property('h', 'localhost')
parse.should.have.property('_').with.length(0)
})
it('should expand grouped short options to a hash with a key for each', function () {
var parse = parser(['-cats'])
parse.should.have.property('c', true)
parse.should.have.property('a', true)
parse.should.have.property('t', true)
parse.should.have.property('s', true)
parse.should.have.property('_').with.length(0)
})
it('should set n to the numeric value 123', function () {
var argv = parser([ '-n123' ])
argv.should.have.property('n', 123)
})
it('should set n to the numeric value 123, with n at the end of a group', function () {
var argv = parser([ '-ab5n123' ])
argv.should.have.property('a', true)
argv.should.have.property('b', true)
argv.should.have.property('5', true)
argv.should.have.property('n', 123)
argv.should.have.property('_').with.length(0)
})
it('should set n to the numeric value 123, with = as separator', function () {
var argv = parser([ '-n=123' ])
argv.should.have.property('n', 123)
})
it('should set n to the numeric value 123, with n at the end of a group and = as separator', function () {
var argv = parser([ '-ab5n=123' ])
argv.should.have.property('a', true)
argv.should.have.property('b', true)
argv.should.have.property('5', true)
argv.should.have.property('n', 123)
argv.should.have.property('_').with.length(0)
})
})
describe('whitespace', function () {
it('should be whitespace', function () {
var argv = parser([ '-x', '\t' ])
argv.should.have.property('x', '\t')
})
})
describe('boolean modifier function', function () {
it('should prevent yargs from sucking in the next option as the value of the first option', function () {
// Arrange & Act
var result = parser(['-b', '123'], {
boolean: ['b']
})
// Assert
result.should.have.property('b').that.is.a('boolean').and.is.true
result.should.have.property('_').and.deep.equal([123])
})
})
describe('defaults', function () {
function checkNoArgs (opts, hasAlias) {
it('should set defaults if no args', function () {
var result = parser([], opts)
result.should.have.property('flag', true)
if (hasAlias) {
result.should.have.property('f', true)
}
})
}
function checkExtraArg (opts, hasAlias) {
it('should set defaults if one extra arg', function () {
var result = parser(['extra'], opts)
result.should.have.property('flag', true)
result.should.have.property('_').and.deep.equal(['extra'])
if (hasAlias) {
result.should.have.property('f', true)
}
})
}
function checkStringArg (opts, hasAlias) {
it('should set defaults even if arg looks like a string', function () {
var result = parser([ '--flag', 'extra' ], opts)
result.should.have.property('flag', true)
result.should.have.property('_').and.deep.equal(['extra'])
if (hasAlias) {
result.should.have.property('f', true)
}
})
}
describe('for options with aliases', function () {
var opts = {
alias: {
flag: ['f']
},
default: {
flag: true
}
}
checkNoArgs(opts, true)
checkExtraArg(opts, true)
})
describe('for typed options without aliases', function () {
var opts = {
boolean: ['flag'],