forked from yargs/yargs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.js
1468 lines (1323 loc) · 48.1 KB
/
command.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
'use strict'
/* global describe, it, beforeEach */
const yargs = require('../')
const expect = require('chai').expect
const checkOutput = require('./helpers/utils').checkOutput
require('chai').should()
const noop = () => {}
describe('Command', () => {
beforeEach(() => {
yargs.reset()
})
describe('positional arguments', () => {
it('parses command string and populates optional and required positional arguments', () => {
const y = yargs([])
.command('foo <bar> [awesome]', 'my awesome command', yargs => yargs)
const command = y.getCommandInstance()
const handlers = command.getCommandHandlers()
handlers.foo.demanded.should.deep.include({
cmd: ['bar'],
variadic: false
})
handlers.foo.optional.should.deep.include({
cmd: ['awesome'],
variadic: false
})
})
it('populates inner argv with positional arguments', (done) => {
yargs('foo hello world')
.command('foo <bar> [awesome]', 'my awesome command', noop, (argv) => {
argv._.should.include('foo')
argv.bar.should.equal('hello')
argv.awesome.should.equal('world')
return done()
})
.parse()
})
it('populates outer argv with positional arguments', () => {
const argv = yargs('foo hello world')
.command('foo <bar> [awesome]')
.parse()
argv._.should.include('foo')
argv.bar.should.equal('hello')
argv.awesome.should.equal('world')
})
it('populates argv with camel-case variants of arguments when possible', () => {
const argv = yargs('foo hello world')
.command('foo <foo-bar> [baz-qux]')
.parse()
argv._.should.include('foo')
argv['foo-bar'].should.equal('hello')
argv.fooBar.should.equal('hello')
argv.bazQux.should.equal('world')
argv['baz-qux'].should.equal('world')
})
it('populates argv with camel-case variants of variadic args when possible', () => {
const argv = yargs('foo hello world !')
.command('foo <foo-bar> [baz-qux..]')
.parse()
argv._.should.include('foo')
argv['foo-bar'].should.equal('hello')
argv.fooBar.should.equal('hello')
argv.bazQux.should.deep.equal(['world', '!'])
argv['baz-qux'].should.deep.equal(['world', '!'])
})
it('populates subcommand\'s inner argv with positional arguments', () => {
yargs('foo bar hello world')
.command('foo', 'my awesome command', yargs => yargs.command(
'bar <greeting> [recipient]',
'subcommands are cool',
noop,
(argv) => {
argv._.should.deep.equal(['foo', 'bar'])
argv.greeting.should.equal('hello')
argv.recipient.should.equal('world')
}
))
.parse()
})
it('ignores positional args for aliases', () => {
const y = yargs([])
.command(['foo [awesome]', 'wat <yo>'], 'my awesome command')
const command = y.getCommandInstance()
const handlers = command.getCommandHandlers()
handlers.foo.optional.should.deep.include({
cmd: ['awesome'],
variadic: false
})
handlers.foo.demanded.should.deep.equal([])
expect(handlers.wat).to.equal(undefined)
command.getCommands().should.deep.equal(['foo', 'wat'])
})
it('does not overwrite existing values in argv for keys that are not positional', () => {
const argv = yargs('foo foo.js --reporter=html')
.command('foo <file>')
.default('reporter', 'text')
.parse()
argv.file.should.equal('foo.js')
argv.reporter.should.equal('html')
})
// bug reported by @boneskull during mocha migration.
it('does not load config twice when command executed', () => {
let parseCount = 0
yargs('cmd --config=.foo.json')
.command(
'$0 [foo..]',
'does a thing',
yargs =>
yargs
.option('config', {
default: '.foo.json'
})
.positional('foo', {
description: 'bar'
})
.config('config', filepath => {
parseCount++
return {}
}),
argv => {}
)
.parse()
parseCount.should.equal(1)
})
})
describe('variadic', () => {
it('allows required arguments to be variadic', () => {
const argv = yargs('foo /root file1 file2 file3')
.command('foo <root> <files..>')
.parse()
argv.root.should.equal('/root')
argv.files.should.deep.equal(['file1', 'file2', 'file3'])
})
it('allows optional arguments to be variadic', () => {
const argv = yargs('foo /root file1 file2 file3')
.command('foo <root> [files..]')
.parse()
argv.root.should.equal('/root')
argv.files.should.deep.equal(['file1', 'file2', 'file3'])
})
it('fails if required arguments are missing', (done) => {
yargs('foo /root')
.command('foo <root> <files..>')
.fail((err) => {
err.should.match(/Not enough non-option arguments/)
return done()
})
.parse()
})
it('does not fail if zero optional arguments are provided', () => {
const argv = yargs('foo /root')
.command('foo <root> [files...]')
.parse()
argv.root.should.equal('/root')
argv.files.should.deep.equal([])
})
it('only allows the last argument to be variadic', () => {
const argv = yargs('foo /root file1 file2')
.command('foo <root..> <file>')
.parse()
argv.root.should.equal('/root')
argv.file.should.equal('file1')
argv._.should.include('file2')
})
// addresses: https://github.com/yargs/yargs/issues/1246
it('allows camel-case, variadic arguments, and strict mode to be combined', () => {
const argv = yargs('ls one two three')
.command('ls [expandMe...]')
.strict()
.parse()
argv.expandMe.should.deep.equal(['one', 'two', 'three'])
argv['expand-me'].should.deep.equal(['one', 'two', 'three'])
})
})
describe('missing positional arguments', () => {
it('fails if a required argument is missing', (done) => {
const argv = yargs('foo hello')
.command('foo <bar> <awesome>')
.fail((err) => {
err.should.match(/got 1, need at least 2/)
return done()
})
.parse()
argv.bar.should.equal('hello')
})
it('does not fail if optional argument is missing', () => {
const argv = yargs('foo hello')
.command('foo <bar> [awesome]')
.parse()
expect(argv.awesome).to.equal(undefined)
argv.bar.should.equal('hello')
})
})
describe('API', () => {
it('accepts string, string as first 2 arguments', () => {
const cmd = 'foo'
const desc = 'i\'m not feeling very creative at the moment'
const isDefault = false
const aliases = []
const y = yargs([]).command(cmd, desc)
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([cmd, desc, isDefault, aliases])
})
it('accepts array, string as first 2 arguments', () => {
const aliases = ['bar', 'baz']
const cmd = 'foo <qux>'
const desc = 'i\'m not feeling very creative at the moment'
const isDefault = false
const y = yargs([]).command([cmd].concat(aliases), desc)
const usageCommands = y.getUsageInstance().getCommands()
usageCommands[0].should.deep.equal([cmd, desc, isDefault, aliases])
const cmdCommands = y.getCommandInstance().getCommands()
cmdCommands.should.deep.equal(['foo', 'bar', 'baz'])
})
it('accepts string, boolean as first 2 arguments', () => {
const cmd = 'foo'
const desc = false
const y = yargs([]).command(cmd, desc)
const commands = y.getUsageInstance().getCommands()
commands.should.deep.equal([])
})
it('accepts array, boolean as first 2 arguments', () => {
const aliases = ['bar', 'baz']
const cmd = 'foo <qux>'
const desc = false
const y = yargs([]).command([cmd].concat(aliases), desc)
const usageCommands = y.getUsageInstance().getCommands()
usageCommands.should.deep.equal([])
const cmdCommands = y.getCommandInstance().getCommands()
cmdCommands.should.deep.equal(['foo', 'bar', 'baz'])
})
it('accepts function as 3rd argument', () => {
const cmd = 'foo'
const desc = 'i\'m not feeling very creative at the moment'
const builder = yargs => yargs
const y = yargs([]).command(cmd, desc, builder)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(cmd)
handlers.foo.builder.should.equal(builder)
})
it('accepts options object as 3rd argument', () => {
const cmd = 'foo'
const desc = 'i\'m not feeling very creative at the moment'
const builder = {
hello: { default: 'world' }
}
const y = yargs([]).command(cmd, desc, builder)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(cmd)
handlers.foo.builder.should.equal(builder)
})
it('accepts module (with builder function and handler function) as 3rd argument', () => {
const cmd = 'foo'
const desc = 'i\'m not feeling very creative at the moment'
const module = {
builder (yargs) { return yargs },
handler (argv) {}
}
const y = yargs([]).command(cmd, desc, module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(cmd)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
})
it('accepts module (with builder object and handler function) as 3rd argument', () => {
const cmd = 'foo'
const desc = 'i\'m not feeling very creative at the moment'
const module = {
builder: {
hello: { default: 'world' }
},
handler (argv) {}
}
const y = yargs([]).command(cmd, desc, module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(cmd)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
})
it('accepts module (describe key, builder function) as 1st argument', () => {
const module = {
command: 'foo',
describe: 'i\'m not feeling very creative at the moment',
builder (yargs) { return yargs },
handler (argv) {}
}
const isDefault = false
const aliases = []
const y = yargs([]).command(module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe, isDefault, aliases])
})
it('accepts module (description key, builder function) as 1st argument', () => {
const module = {
command: 'foo',
description: 'i\'m not feeling very creative at the moment',
builder (yargs) { return yargs },
handler (argv) {}
}
const isDefault = false
const aliases = []
const y = yargs([]).command(module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.description, isDefault, aliases])
})
it('accepts module (desc key, builder function) as 1st argument', () => {
const module = {
command: 'foo',
desc: 'i\'m not feeling very creative at the moment',
builder (yargs) { return yargs },
handler (argv) {}
}
const isDefault = false
const aliases = []
const y = yargs([]).command(module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.desc, isDefault, aliases])
})
it('accepts module (false describe, builder function) as 1st argument', () => {
const module = {
command: 'foo',
describe: false,
builder (yargs) { return yargs },
handler (argv) {}
}
const y = yargs([]).command(module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const commands = y.getUsageInstance().getCommands()
commands.should.deep.equal([])
})
it('accepts module (missing describe, builder function) as 1st argument', () => {
const module = {
command: 'foo',
builder (yargs) { return yargs },
handler (argv) {}
}
const y = yargs([]).command(module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const commands = y.getUsageInstance().getCommands()
commands.should.deep.equal([])
})
it('accepts module (describe key, builder object) as 1st argument', () => {
const module = {
command: 'foo',
describe: 'i\'m not feeling very creative at the moment',
builder: {
hello: {
default: 'world'
}
},
handler (argv) {}
}
const isDefault = false
const aliases = []
const y = yargs([]).command(module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe, isDefault, aliases])
})
it('accepts module (missing handler function) as 1st argument', () => {
const module = {
command: 'foo',
describe: 'i\'m not feeling very creative at the moment',
builder: {
hello: {
default: 'world'
}
}
}
const isDefault = false
const aliases = []
const y = yargs([]).command(module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
expect(typeof handlers.foo.handler).to.equal('function')
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe, isDefault, aliases])
})
it('accepts module (with command array) as 1st argument', () => {
const module = {
command: ['foo <qux>', 'bar', 'baz'],
describe: 'i\'m not feeling very creative at the moment',
builder (yargs) { return yargs },
handler (argv) {}
}
const isDefault = false
const y = yargs([]).command(module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command[0])
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const usageCommands = y.getUsageInstance().getCommands()
usageCommands[0].should.deep.equal([module.command[0], module.describe, isDefault, ['bar', 'baz']])
const cmdCommands = y.getCommandInstance().getCommands()
cmdCommands.should.deep.equal(['foo', 'bar', 'baz'])
})
it('accepts module (with command string and aliases array) as 1st argument', () => {
const module = {
command: 'foo <qux>',
aliases: ['bar', 'baz'],
describe: 'i\'m not feeling very creative at the moment',
builder (yargs) { return yargs },
handler (argv) {}
}
const isDefault = false
const y = yargs([]).command(module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const usageCommands = y.getUsageInstance().getCommands()
usageCommands[0].should.deep.equal([module.command, module.describe, isDefault, module.aliases])
const cmdCommands = y.getCommandInstance().getCommands()
cmdCommands.should.deep.equal(['foo', 'bar', 'baz'])
})
it('accepts module (with command array and aliases array) as 1st argument', () => {
const module = {
command: ['foo <qux>', 'bar'],
aliases: ['baz', 'nat'],
describe: 'i\'m not feeling very creative at the moment',
builder (yargs) { return yargs },
handler (argv) {}
}
const isDefault = false
const y = yargs([]).command(module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command[0])
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const usageCommands = y.getUsageInstance().getCommands()
usageCommands[0].should.deep.equal([module.command[0], module.describe, isDefault, ['bar', 'baz', 'nat']])
const cmdCommands = y.getCommandInstance().getCommands()
cmdCommands.should.deep.equal(['foo', 'bar', 'baz', 'nat'])
})
it('accepts module (with command string and aliases string) as 1st argument', () => {
const module = {
command: 'foo <qux>',
aliases: 'bar',
describe: 'i\'m not feeling very creative at the moment',
builder (yargs) { return yargs },
handler (argv) {}
}
const isDefault = false
const y = yargs([]).command(module)
const handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const usageCommands = y.getUsageInstance().getCommands()
usageCommands[0].should.deep.equal([module.command, module.describe, isDefault, ['bar']])
const cmdCommands = y.getCommandInstance().getCommands()
cmdCommands.should.deep.equal(['foo', 'bar'])
})
})
describe('commandDir', () => {
it('supports relative dirs', () => {
const r = checkOutput(() => yargs('--help').wrap(null)
.commandDir('fixtures/cmddir')
.parse())
r.exit.should.equal(true)
r.errors.length.should.equal(0)
r.should.have.property('logs')
r.logs.join('\n').split(/\n+/).should.deep.equal([
'usage [command]',
'Commands:',
' usage dream [command] [opts] Go to sleep and dream',
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]'
])
})
it('supports nested subcommands', () => {
const r = checkOutput(() => yargs('dream --help').wrap(null)
.commandDir('fixtures/cmddir')
.parse(), [ './command' ])
r.exit.should.equal(true)
r.errors.length.should.equal(0)
r.logs[0].split(/\n+/).should.deep.equal([
'command dream [command] [opts]',
'Go to sleep and dream',
'Commands:',
' command dream of-memory <memory> Dream about a specific memory',
' command dream within-a-dream [command] [opts] Dream within a dream',
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]',
' --shared Is the dream shared with others? [boolean]',
' --extract Attempt extraction? [boolean]'
])
})
it('supports a "recurse" boolean option', () => {
const r = checkOutput(() => yargs('--help').wrap(null)
.commandDir('fixtures/cmddir', { recurse: true })
.parse())
r.exit.should.equal(true)
r.errors.length.should.equal(0)
r.logs.join('\n').split(/\n+/).should.deep.equal([
'usage [command]',
'Commands:',
' usage limbo [opts] Get lost in pure subconscious',
' usage inception [command] [opts] Enter another dream, where inception is possible',
' usage within-a-dream [command] [opts] Dream within a dream',
' usage dream [command] [opts] Go to sleep and dream',
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]'
])
})
it('supports a "visit" function option', () => {
let commandObject
let pathToFile
let filename
const r = checkOutput(() => yargs('--help').wrap(null)
.commandDir('fixtures/cmddir', {
visit (_commandObject, _pathToFile, _filename) {
commandObject = _commandObject
pathToFile = _pathToFile
filename = _filename
return false // exclude command
}
})
.parse())
commandObject.should.have.property('command').and.equal('dream [command] [opts]')
commandObject.should.have.property('desc').and.equal('Go to sleep and dream')
commandObject.should.have.property('builder')
commandObject.should.have.property('handler')
pathToFile.should.contain(require('path').join('test', 'fixtures', 'cmddir', 'dream.js'))
filename.should.equal('dream.js')
r.exit.should.equal(true)
r.errors.length.should.equal(0)
r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]'
])
})
it('detects and ignores cyclic dir references', () => {
const r = checkOutput(() => yargs('cyclic --help').wrap(null)
.commandDir('fixtures/cmddir_cyclic')
.parse(), [ './command' ])
r.exit.should.equal(true)
r.errors.length.should.equal(0)
r.should.have.property('logs')
r.logs.join('\n').split(/\n+/).should.deep.equal([
'command cyclic',
'Attempts to (re)apply its own dir',
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]'
])
})
it('derives \'command\' string from filename when not exported', () => {
const r = checkOutput(() => yargs('--help').wrap(null)
.commandDir('fixtures/cmddir_noname')
.parse())
r.exit.should.equal(true)
r.errors.length.should.equal(0)
r.should.have.property('logs')
r.logs.join('\n').split(/\n+/).should.deep.equal([
'usage [command]',
'Commands:',
' usage nameless Command name derived from module filename',
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]'
])
})
})
describe('help command', () => {
it('displays command help appropriately', () => {
const sub = {
command: 'sub',
desc: 'Run the subcommand',
builder: {},
handler (argv) {}
}
const cmd = {
command: 'cmd <sub>',
desc: 'Try a command',
builder (yargs) {
return yargs.command(sub)
},
handler (argv) {}
}
const helpCmd = checkOutput(() => yargs('help cmd')
.wrap(null)
.command(cmd)
.parse(), [ './command' ])
const cmdHelp = checkOutput(() => yargs('cmd help')
.wrap(null)
.command(cmd)
.parse(), [ './command' ])
const helpCmdSub = checkOutput(() => yargs('help cmd sub')
.wrap(null)
.command(cmd)
.parse(), [ './command' ])
const cmdHelpSub = checkOutput(() => yargs('cmd help sub')
.wrap(null)
.command(cmd)
.parse(), [ './command' ])
const cmdSubHelp = checkOutput(() => yargs('cmd sub help')
.wrap(null)
.command(cmd)
.parse(), [ './command' ])
const expectedCmd = [
'command cmd <sub>',
'Try a command',
'Commands:',
' command cmd sub Run the subcommand',
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]'
]
const expectedSub = [
'command cmd sub',
'Run the subcommand',
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]'
]
// no help is output if help isn't last
// positional argument.
helpCmd.logs.should.eql([])
helpCmdSub.logs.should.eql([])
cmdHelpSub.logs.should.eql([])
// shows help if it is the last positional argument.
cmdHelp.logs.join('\n').split(/\n+/).should.deep.equal(expectedCmd)
cmdSubHelp.logs.join('\n').split(/\n+/).should.deep.equal(expectedSub)
})
})
// see: https://github.com/yargs/yargs/pull/553
it('preserves top-level envPrefix', () => {
process.env.FUN_DIP_STICK = 'yummy'
process.env.FUN_DIP_POWDER = 'true'
yargs('eat')
.env('FUN_DIP')
.global('stick') // this does not actually need to be global
.command('eat', 'Adult supervision recommended', yargs => yargs.boolean('powder').exitProcess(false), (argv) => {
argv.powder.should.equal(true)
argv.stick.should.equal('yummy')
})
.exitProcess(false)
.parse()
})
// addresses https://github.com/yargs/yargs/issues/514.
it('respects order of positional arguments when matching commands', () => {
const output = []
yargs('bar foo')
.command('foo', 'foo command', (yargs) => {
output.push('foo')
})
.command('bar', 'bar command', (yargs) => {
output.push('bar')
})
.parse()
output.should.include('bar')
output.should.not.include('foo')
})
// addresses https://github.com/yargs/yargs/issues/558
it('handles positional arguments if command is invoked using .parse()', () => {
const y = yargs([])
.command('foo <second>', 'the foo command', {})
const argv = y.parse(['foo', 'bar'])
argv.second.should.equal('bar')
})
// addresses https://github.com/yargs/yargs/issues/710
it('invokes command handler repeatedly if parse() is called multiple times', () => {
let counter = 0
const y = yargs([])
.command('foo', 'the foo command', {}, (argv) => {
counter++
})
y.parse(['foo'])
y.parse(['foo'])
counter.should.equal(2)
})
// addresses: https://github.com/yargs/yargs/issues/776
it('allows command handler to be invoked repeatedly when help is enabled', (done) => {
let counter = 0
const y = yargs([])
.command('foo', 'the foo command', {}, (argv) => {
counter++
})
y.parse(['foo'], noop)
y.parse(['foo'], () => {
counter.should.equal(2)
return done()
})
})
// addresses https://github.com/yargs/yargs/issues/522
it('does not require builder function to return', () => {
const argv = yargs('yo')
.command('yo [someone]', 'Send someone a yo', (yargs) => {
yargs.default('someone', 'Pat')
}, (argv) => {
argv.should.have.property('someone').and.equal('Pat')
})
.parse()
argv.should.have.property('someone').and.equal('Pat')
})
it('allows builder function to parse argv without returning', () => {
const argv = yargs('yo Jude')
.command('yo <someone>', 'Send someone a yo', (yargs) => {
yargs.parse()
}, (argv) => {
argv.should.have.property('someone').and.equal('Jude')
}).parse()
argv.should.have.property('someone').and.equal('Jude')
})
it('allows builder function to return parsed argv', () => {
const argv = yargs('yo Leslie')
.command('yo <someone>', 'Send someone a yo', yargs => yargs.parse(), (argv) => {
argv.should.have.property('someone').and.equal('Leslie')
})
.parse()
argv.should.have.property('someone').and.equal('Leslie')
})
// addresses https://github.com/yargs/yargs/issues/540
it('ignores extra spaces in command string', () => {
const y = yargs([])
.command('foo [awesome]', 'my awesome command', yargs => yargs)
const command = y.getCommandInstance()
const handlers = command.getCommandHandlers()
handlers.foo.demanded.should.not.include({
cmd: '',
variadic: false
})
handlers.foo.demanded.should.have.lengthOf(0)
})
it('executes a command via alias', () => {
let commandCalled = false
const argv = yargs('hi world')
.command(['hello <someone>', 'hi'], 'Say hello', {}, (argv) => {
commandCalled = true
argv.should.have.property('someone').and.equal('world')
})
.parse()
argv.should.have.property('someone').and.equal('world')
commandCalled.should.equal(true)
})
describe('positional aliases', () => {
it('allows an alias to be defined for a required positional argument', () => {
const argv = yargs('yo bcoe 113993')
.command('yo <user | email> [ssn]', 'Send someone a yo')
.parse()
argv.user.should.equal('bcoe')
argv.email.should.equal('bcoe')
argv.ssn.should.equal(113993)
})
it('allows an alias to be defined for an optional positional argument', () => {
let argv
yargs('yo 113993')
.command('yo [ssn|sin]', 'Send someone a yo', {}, (_argv) => {
argv = _argv
})
.parse()
argv.ssn.should.equal(113993)
argv.sin.should.equal(113993)
})
it('allows variadic and positional arguments to be combined', () => {
const parser = yargs
.command('yo <user|email> [ ssns | sins... ]', 'Send someone a yo')
const argv = parser.parse('yo bcoe 113993 112888')
argv.user.should.equal('bcoe')
argv.email.should.equal('bcoe')
argv.ssns.should.deep.equal([113993, 112888])
argv.sins.should.deep.equal([113993, 112888])
})
})
describe('global parsing hints', () => {
describe('config', () => {
it('does not load config for command if global is false', (done) => {
yargs('command --foo ./package.json')
.command('command', 'a command', {}, (argv) => {
expect(argv.license).to.equal(undefined)
return done()
})
.config('foo')
.global('foo', false)
.parse()
})
it('loads config for command by default', (done) => {
yargs('command --foo ./package.json')
.command('command', 'a command', {}, (argv) => {
argv.license.should.equal('MIT')
return done()
})
.config('foo')
.parse()
})
})
describe('validation', () => {
it('resets implies logic for command if global is false', (done) => {
yargs('command --foo 99')
.command('command', 'a command', {}, (argv) => {
argv.foo.should.equal(99)
return done()
})
.implies('foo', 'bar')
.global('foo', false)
.parse()
})
it('applies conflicts logic for command by default', (done) => {
yargs('command --foo --bar')
.command('command', 'a command', {}, (argv) => {})
.fail((msg) => {
msg.should.match(/mutually exclusive/)
return done()
})
.conflicts('foo', 'bar')
.parse()
})
it('resets conflicts logic for command if global is false', (done) => {
yargs('command --foo --bar')
.command('command', 'a command', {}, (argv) => {
argv.foo.should.equal(true)
argv.bar.should.equal(true)
return done()
})
.conflicts('foo', 'bar')
.global('foo', false)
.parse()
})
it('applies custom checks globally by default', (done) => {
yargs('command blerg --foo')
.command('command <snuh>', 'a command')
.check((argv) => {
argv.snuh.should.equal('blerg')
argv.foo.should.equal(true)
argv._.should.include('command')
done()
return true
})
.parse()
})
it('resets custom check if global is false', () => {
let checkCalled = false
yargs('command blerg --foo')
.command('command <snuh>', 'a command')
.check((argv) => {
checkCalled = true
return true
}, false)
.parse()
checkCalled.should.equal(false)
})
it('applies demandOption globally', (done) => {
yargs('command blerg --foo')
.command('command <snuh>', 'a command')
.fail((msg) => {
msg.should.match(/Missing required argument: bar/)
return done()
})
.demandOption('bar')
.parse()
})
})
describe('strict', () => {
it('defaults to false when not called', () => {
let commandCalled = false
yargs('hi')
.command('hi', 'The hi command', (innerYargs) => {
commandCalled = true
innerYargs.getStrict().should.equal(false)
})
yargs.getStrict().should.equal(false)
yargs.parse() // parse and run command
commandCalled.should.equal(true)
})
it('can be enabled just for a command', () => {
let commandCalled = false