deno.land / x / yargs@v17.6.0-deno / test / command.cjs

نووسراو ببینە
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
/* global describe, it, beforeEach *//* eslint-disable no-unused-vars */'use strict';const assert = require('assert');const yargs = require('../index.cjs');const expect = require('chai').expect;const checkOutput = require('./helpers/utils.cjs').checkOutput;
require('chai').should();const noop = () => {};async function wait() { return new Promise(resolve => { setTimeout(resolve, 10); });}
describe('Command', () => { beforeEach(() => { yargs.getInternalMethods().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.getInternalMethods().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 when unknown-options-as-args is not set', () => { const argv = yargs('foo hello world') .command('foo <bar> [awesome]') .parse();
argv._.should.include('foo'); argv.should.have.property('bar', 'hello'); argv.should.have.property('awesome', 'world'); });
it('populates outer argv with positional arguments when unknown-options-as-args is set', () => { const argv = yargs('foo hello world') .command('foo <bar> [awesome]') .parserConfiguration({'unknown-options-as-args': true}) .parse();
argv._.should.include('foo'); argv.should.have.property('bar', 'hello'); argv.should.have.property('awesome', '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.getInternalMethods().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); });
// see: https://github.com/yargs/yargs/issues/1457 it('handles -- in conjunction with positional arguments', () => { let called = false; const argv = yargs('foo hello world series -- apple banana') .command( 'foo <bar> [awesome...]', 'my awesome command', noop, argv2 => { argv2.bar.should.eql('hello'); argv2.awesome.should.eql(['world', 'series']); argv2._.should.eql(['foo', 'apple', 'banana']); called = true; } ) .parse(); argv.bar.should.eql('hello'); argv.awesome.should.eql(['world', 'series']); argv._.should.eql(['foo', 'apple', 'banana']); called.should.equal(true); });
// see: https://github.com/yargs/yargs/issues/1457 it('continues to support populate-- for commands, post #1457', () => { let called = false; const argv = yargs('foo hello world series -- apple banana') .command( 'foo <bar> [awesome...]', 'my awesome command', noop, argv2 => { argv2.bar.should.eql('hello'); argv2.awesome.should.eql(['world', 'series']); argv2._.should.eql(['foo']); argv2['--'].should.eql(['apple', 'banana']); called = true; } ) .parserConfiguration({ 'populate--': true, }) .parse(); argv.bar.should.eql('hello'); argv.awesome.should.eql(['world', 'series']); argv._.should.eql(['foo']); argv['--'].should.eql(['apple', 'banana']); called.should.equal(true); });
// Addresses: https://github.com/yargs/yargs/issues/1637 it('does not overwrite options in argv if variadic', () => { yargs .command({ command: 'cmd [foods..]', desc: 'cmd desc', builder: yargs => yargs.positional('foods', { desc: 'foods desc', type: 'string', }), handler: argv => { argv.foods.should.deep.equal(['apples', 'cherries', 'grapes']); }, }) .parse('cmd --foods apples cherries grapes'); });
it('does not overwrite options in argv if variadic and when using default command', () => { yargs .command({ command: '$0 [foods..]', desc: 'default desc', builder: yargs => yargs.positional('foods', { desc: 'foods desc', type: 'string', }), handler: argv => { argv.foods.should.deep.equal(['apples', 'cherries', 'grapes']); }, }) .parse('--foods apples cherries grapes'); });
it('does not combine positional default and provided values', () => { yargs() .command({ command: 'cmd [foods..]', desc: 'cmd desc', builder: yargs => yargs.positional('foods', { desc: 'foods desc', type: 'string', default: ['pizza', 'wings'], }), handler: argv => { argv.foods.should.deep.equal(['apples', 'cherries', 'grapes']); argv.foods.should.not.include('pizza'); }, }) .parse('cmd apples cherries grapes'); });
it('does not combine config values and provided values', () => { yargs('foo bar baz qux') .command({ command: '$0 <arg-1> [arg-2] [arg-3..]', desc: 'default description', builder: yargs => yargs .option('arg-1', {type: 'string'}) .option('arg-2', {type: 'string'}) .option('arg-3', {type: 'string'}) .config({ arg2: 'bar', arg3: ['baz', 'qux'], }), handler: argv => { argv.arg1.should.equal('foo'); argv.arg2.should.equal('bar'); argv.arg3.should.deep.equal(['baz', 'qux']); argv['arg-3'].should.deep.equal(['baz', 'qux']); }, }) .strict() .parse(); });
it('does not overwrite options in argv if variadic and preserves falsy values', () => { yargs .command({ command: '$0 [numbers..]', desc: 'default desc', builder: yargs => yargs.positional('numbers', { desc: 'numbers desc', type: 'number', }), handler: argv => { argv.numbers.should.deep.equal([0, 1, 2]); }, }) .parse('--numbers 0 1 2'); }); });
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 deprecated = false;
const y = yargs([]).command(cmd, desc); const commands = y.getInternalMethods().getUsageInstance().getCommands(); commands[0].should.deep.equal([ cmd, desc, isDefault, aliases, deprecated, ]); });
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 deprecated = false;
const y = yargs([]).command([cmd].concat(aliases), desc); const usageCommands = y .getInternalMethods() .getUsageInstance() .getCommands(); usageCommands[0].should.deep.equal([ cmd, desc, isDefault, aliases, deprecated, ]); const cmdCommands = y .getInternalMethods() .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.getInternalMethods().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 .getInternalMethods() .getUsageInstance() .getCommands(); usageCommands.should.deep.equal([]); const cmdCommands = y .getInternalMethods() .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 .getInternalMethods() .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 .getInternalMethods() .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 .getInternalMethods() .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 .getInternalMethods() .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 deprecated = false;
const y = yargs([]).command(module); const handlers = y .getInternalMethods() .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.getInternalMethods().getUsageInstance().getCommands(); commands[0].should.deep.equal([ module.command, module.describe, isDefault, aliases, deprecated, ]); });
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 deprecated = false;
const y = yargs([]).command(module); const handlers = y .getInternalMethods() .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.getInternalMethods().getUsageInstance().getCommands(); commands[0].should.deep.equal([ module.command, module.description, isDefault, aliases, deprecated, ]); });
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 deprecated = false;
const y = yargs([]).command(module); const handlers = y .getInternalMethods() .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.getInternalMethods().getUsageInstance().getCommands(); commands[0].should.deep.equal([ module.command, module.desc, isDefault, aliases, deprecated, ]); });
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 .getInternalMethods() .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.getInternalMethods().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 .getInternalMethods() .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.getInternalMethods().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 deprecated = false;
const y = yargs([]).command(module); const handlers = y .getInternalMethods() .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.getInternalMethods().getUsageInstance().getCommands(); commands[0].should.deep.equal([ module.command, module.describe, isDefault, aliases, deprecated, ]); });
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 deprecated = false;
const y = yargs([]).command(module); const handlers = y .getInternalMethods() .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.getInternalMethods().getUsageInstance().getCommands(); commands[0].should.deep.equal([ module.command, module.describe, isDefault, aliases, deprecated, ]); });
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 deprecated = false;
const y = yargs([]).command(module); const handlers = y .getInternalMethods() .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 .getInternalMethods() .getUsageInstance() .getCommands(); usageCommands[0].should.deep.equal([ module.command[0], module.describe, isDefault, ['bar', 'baz'], deprecated, ]); const cmdCommands = y .getInternalMethods() .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 deprecated = false;
const y = yargs([]).command(module); const handlers = y .getInternalMethods() .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 .getInternalMethods() .getUsageInstance() .getCommands(); usageCommands[0].should.deep.equal([ module.command, module.describe, isDefault, module.aliases, deprecated, ]); const cmdCommands = y .getInternalMethods() .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 deprecated = false;
const y = yargs([]).command(module); const handlers = y .getInternalMethods() .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 .getInternalMethods() .getUsageInstance() .getCommands(); usageCommands[0].should.deep.equal([ module.command[0], module.describe, isDefault, ['bar', 'baz', 'nat'], deprecated, ]); const cmdCommands = y .getInternalMethods() .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 deprecated = false;
const y = yargs([]).command(module); const handlers = y .getInternalMethods() .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 .getInternalMethods() .getUsageInstance() .getCommands(); usageCommands[0].should.deep.equal([ module.command, module.describe, isDefault, ['bar'], deprecated, ]); const cmdCommands = y .getInternalMethods() .getCommandInstance() .getCommands(); cmdCommands.should.deep.equal(['foo', 'bar']); });
it('accepts deprecated as 5th argument', () => { const command = 'command'; const description = 'description'; const isDefault = false; const aliases = []; const deprecated = false; const y = yargs([]).command( command, description, {}, () => {}, [], deprecated ); const usageCommands = y .getInternalMethods() .getUsageInstance() .getCommands(); usageCommands[0].should.deep.equal([ command, description, isDefault, aliases, deprecated, ]); }); });
describe('commandDir', () => { it('supports relative dirs', () => { const r = checkOutput(() => yargs('--help').wrap(null).commandDir('fixtures/cmddir').parse() ); r.exit.should.equal(true); r.exitCode.should.equal(0); 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.getInternalMethods().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 several aliases to be defined for a required positional argument', () => { const argv = yargs('yo bcoe 113993') .command('yo <user | email | id> [ssn]', 'Send someone a yo', yargs => yargs.alias('user', 'somethingElse') ) .parse(); argv.user.should.equal('bcoe'); argv.email.should.equal('bcoe'); argv.id.should.equal('bcoe'); argv.somethingElse.should.equal('bcoe'); argv.ssn.should.equal(113993); });
it('allows several aliases to be defined for an optional positional argument', () => { let argv; yargs('yo 113993') .command( 'yo [ssn|sin|id]', 'Send someone a yo', yargs => yargs.alias('ssn', 'somethingElse'), _argv => { argv = _argv; } ) .parse(); argv.ssn.should.equal(113993); argv.sin.should.equal(113993); argv.id.should.equal(113993); argv.somethingElse.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('allows each builder to specify own middleware', () => { let checkCalled1 = 0; let checkCalled2 = 0; const y = yargs() .command('command <snuh>', 'a command', () => { yargs.check(argv => { checkCalled1++; return true; }); }) .command('command2 <snuh>', 'a second command', yargs => { yargs.check(argv => { checkCalled2++; return true; }); }); y.parse('command blerg --foo'); y.parse('command2 blerg --foo'); y.parse('command blerg --foo'); checkCalled1.should.equal(2); checkCalled2.should.equal(1); });
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; yargs('hi').command('hi', 'The hi command', innerYargs => { commandCalled = true; innerYargs.strict().getStrict().should.equal(true); }); yargs.getStrict().should.equal(false); yargs.parse(); // parse and run command commandCalled.should.equal(true); });
it('applies strict globally by default', () => { let commandCalled = false; yargs('hi') .strict() .command('hi', 'The hi command', innerYargs => { commandCalled = true; innerYargs.getStrict().should.equal(true); }); yargs.getStrict().should.equal(true); yargs.parse(); // parse and run command commandCalled.should.equal(true); });
// address regression introduced in #766, thanks @nexdrew! it('does not fail strict check due to postional command arguments', done => { yargs() .strict() .command('hi <name>', 'The hi command') .parse('hi ben', (err, argv, output) => { expect(err).to.equal(null); return done(); }); });
// address https://github.com/yargs/yargs/issues/795 it('does not fail strict check due to postional command arguments in nested commands', done => { yargs() .strict() .command('hi', 'The hi command', yargs => { yargs.command('ben <age>', 'ben command', noop, noop); }) .parse('hi ben 99', (err, argv, output) => { expect(err).to.equal(null); return done(); }); });
it('allows a command to override global`', () => { let commandCalled = false; yargs('hi') .strict() .command('hi', 'The hi command', innerYargs => { commandCalled = true; innerYargs.strict(false).getStrict().should.equal(false); }); yargs.getStrict().should.equal(true); yargs.parse(); // parse and run command commandCalled.should.equal(true); });
it('does not fire command if validation fails', done => { let commandRun = false; yargs() .strict() .command('hi <name>', 'The hi command', noop, argv => { commandRun = true; }) .parse('hi ben --hello=world', (err, argv, output) => { commandRun.should.equal(false); err.message.should.equal('Unknown argument: hello'); return done(); }); }); });
describe('types', () => { it('applies array type globally', () => { const argv = yargs('command --foo 1 2') .command('command', 'a command') .array('foo') .parse(); argv.foo.should.eql([1, 2]); });
it('allows global setting to be disabled for array type', () => { const argv = yargs('command --foo 1 2') .command('command', 'a command') .array('foo') .global('foo', false) .parse(); argv.foo.should.eql(1); });
it('applies choices type globally', done => { yargs('command --foo 99') .command('command', 'a command') .choices('foo', [33, 88]) .fail(msg => { msg.should.match(/Choices: 33, 88/); return done(); }) .parse(); }); });
describe('aliases', () => { it('defaults to applying aliases globally', done => { yargs('command blerg --foo 22') .command('command <snuh>', 'a command', {}, argv => { argv.foo.should.equal(22); argv.bar.should.equal(22); argv.snuh.should.equal('blerg'); return done(); }) .alias('foo', 'bar') .parse(); });
it('allows global application of alias to be disabled', done => { yargs('command blerg --foo 22') .command('command <snuh>', 'a command', {}, argv => { argv.foo.should.equal(22); expect(argv.bar).to.equal(undefined); argv.snuh.should.equal('blerg'); return done(); }) .option('foo', { alias: 'bar', global: false, }) .parse(); }); });
describe('coerce', () => { it('defaults to applying coerce rules globally', done => { yargs('command blerg --foo 22') .command('command <snuh>', 'a command', {}, argv => { argv.foo.should.equal(44); argv.snuh.should.equal('blerg'); return done(); }) .coerce('foo', arg => arg * 2) .parse(); });
// addresses https://github.com/yargs/yargs/issues/794 it('should bubble errors thrown by coerce function inside commands', done => { yargs .command('foo', 'the foo command', yargs => { yargs.coerce('x', arg => { throw Error('yikes an error'); }); }) .parse('foo -x 99', err => { err.message.should.match(/yikes an error/); return done(); }); });
// addresses https://github.com/yargs/yargs/issues/1966 it('should not be applied multiple times for nested commands', () => { let coerceExecutionCount = 0;
const argv = yargs('cmd1 cmd2 foo bar baz') .command('cmd1', 'cmd1 desc', yargs => yargs.command('cmd2 <positional1> <rest...>', 'cmd2 desc', yargs => yargs .positional('rest', { type: 'string', coerce: arg => { if (coerceExecutionCount) { throw Error('coerce applied multiple times'); } coerceExecutionCount++; return arg.join(' '); }, }) .fail(() => { expect.fail(); }) ) ) .parse();
argv.rest.should.equal('bar baz'); coerceExecutionCount.should.equal(1); });
// Addresses: https://github.com/yargs/yargs/issues/2130 it('should not run or set new properties on argv when related argument is not passed', () => { yargs('cmd1') .command( 'cmd1', 'cmd1 desc', yargs => yargs .option('foo', {alias: 'f', type: 'string'}) .option('bar', { alias: 'b', type: 'string', implies: 'f', coerce: () => expect.fail(), // Should not be called }) .fail(() => { expect.fail(); // Should not fail because of implies }), argv => { // eslint-disable-next-line no-prototype-builtins if (Object.prototype.hasOwnProperty(argv, 'b')) { expect.fail(); // 'b' was not provided, coerce should not set it } } ) .strict() .parse(); });
// Addresses: https://github.com/yargs/yargs/issues/2159 it('should not add aliases to argv if strip-aliased config is true', () => { yargs('cmd1 -f hello -b world') .parserConfiguration({'strip-aliased': true}) .command( 'cmd1', 'cmd1 desc', yargs => yargs.option('foo', {alias: 'f', type: 'string'}).option('bar', { type: 'string', alias: 'b', coerce: val => val, }), argv => { // eslint-disable-next-line no-prototype-builtins if (Object.prototype.hasOwnProperty(argv, 'b')) { expect.fail(); // 'b' is an alias, it should not be in argv } } ) .strict() .parse(); }); });
describe('defaults', () => { it('applies defaults globally', done => { yargs('command --foo 22') .command('command [snuh]', 'a command', {}, argv => { argv.foo.should.equal(22); argv.snuh.should.equal(55); return done(); }) .default('snuh', 55) .parse(); }); });
describe('describe', () => { it('flags an option as global if a description is set', done => { yargs() .command('command [snuh]', 'a command') .describe('foo', 'an awesome argument') .parse('command --help', (err, argv, output) => { if (err) return done(err); output.should.not.match(/Commands:/); output.should.match(/an awesome argument/); return done(); }); }); });
describe('help', () => { it('applies help globally', done => { yargs() .command('command [snuh]', 'a command') .describe('foo', 'an awesome argument') .help('hellllllp') .parse('command --hellllllp', (err, argv, output) => { if (err) return done(err); output.should.match(/--hellllllp {2}Show help/); return done(); }); }); });
describe('version', () => { it('applies version globally', done => { yargs() .command('command [snuh]', 'a command') .describe('foo', 'an awesome argument') .version('ver', 'show version', '9.9.9') .parse('command --ver', (err, argv, output) => { if (err) return done(err); output.should.equal('9.9.9'); return done(); }); }); });
describe('groups', () => { it('should apply custom option groups globally', done => { yargs() .command('command [snuh]', 'a command') .group('foo', 'Bad Variable Names:') .group('snuh', 'Bad Variable Names:') .describe('foo', 'foo option') .describe('snuh', 'snuh positional') .parse('command --help', (err, argv, output) => { if (err) return done(err); output.should.match(/Bad Variable Names:\W*--foo/); return done(); }); }); }); });
describe('default commands', () => { it('executes default command if no positional arguments given', done => { yargs('--foo bar') .command('*', 'default command', noop, argv => { argv.foo.should.equal('bar'); return done(); }) .parse(); });
it('executes default command if undefined positional arguments and only command', done => { yargs('baz --foo bar') .command('*', 'default command', noop, argv => { argv.foo.should.equal('bar'); argv._.should.contain('baz'); return done(); }) .parse(); });
it('executes default command if defined positional arguments and only command', done => { yargs('baz --foo bar') .command('* <target>', 'default command', noop, argv => { argv.foo.should.equal('bar'); argv.target.should.equal('baz'); return done(); }) .parse(); });
it('allows $0 as an alias for a default command', done => { yargs('9999') .command('$0 [port]', 'default command', noop, argv => { argv.port.should.equal(9999); return done(); }) .parse(); });
it('does not execute default command if another command is provided', done => { yargs('run bcoe --foo bar') .command('*', 'default command', noop, argv => {}) .command('run <name>', 'run command', noop, argv => { argv.name.should.equal('bcoe'); argv.foo.should.equal('bar'); return done(); }) .parse(); });
it('allows default command to be set as alias', done => { yargs('bcoe --foo bar') .command(['start <name>', '*'], 'start command', noop, argv => { argv._.should.eql([]); argv.name.should.equal('bcoe'); argv.foo.should.equal('bar'); return done(); }) .parse(); });
it('allows command to be run when alias is default command', done => { yargs('start bcoe --foo bar') .command(['start <name>', '*'], 'start command', noop, argv => { argv._.should.eql(['start']); argv.name.should.equal('bcoe'); argv.foo.should.equal('bar'); return done(); }) .parse(); });
it('the last default command set should take precedence', done => { yargs('bcoe --foo bar') .command(['first', '*'], 'override me', noop, noop) .command(['second <name>', '*'], 'start command', noop, argv => { argv._.should.eql([]); argv.name.should.equal('bcoe'); argv.foo.should.equal('bar'); return done(); }) .parse(); });
describe('strict', () => { it('executes default command when strict mode is enabled', done => { yargs('--foo bar') .command('*', 'default command', noop, argv => { argv.foo.should.equal('bar'); return done(); }) .option('foo', { describe: 'a foo command', }) .strict() .parse(); });
it('allows default command aliases, when strict mode is enabled', done => { yargs('bcoe --foo bar') .command(['start <name>', '*'], 'start command', noop, argv => { argv._.should.eql([]); argv.name.should.equal('bcoe'); argv.foo.should.equal('bar'); return done(); }) .strict() .option('foo', { describe: 'a foo command', }) .parse(); }); }); });
describe('deprecated command', () => { describe('using arg', () => { it('shows deprecated notice with boolean', () => { const command = 'command'; const description = 'description'; const deprecated = true; const r = checkOutput(() => { yargs('--help') .command(command, description, {}, () => {}, [], deprecated) .parse(); }); r.logs.should.match(/\[deprecated\]/); }); it('shows deprecated notice with string', () => { const command = 'command'; const description = 'description'; const deprecated = 'deprecated'; const r = checkOutput(() => { yargs('--help') .command(command, description, {}, () => {}, [], deprecated) .parse(); }); r.logs.should.match(/\[deprecated: deprecated\]/); }); }); describe('using module', () => { it('shows deprecated notice with boolean', () => { const command = 'command'; const description = 'description'; const deprecated = true; const r = checkOutput(() => { yargs('--help').command({command, description, deprecated}).parse(); }); r.logs.should.match(/\[deprecated\]/); }); it('shows deprecated notice with string', () => { const command = 'command'; const description = 'description'; const deprecated = 'deprecated'; const r = checkOutput(() => { yargs('--help').command({command, description, deprecated}).parse(); }); r.logs.should.match(/\[deprecated: deprecated\]/); }); }); });
// addresses: https://github.com/yargs/yargs/issues/819 it('should kick along [demand] configuration to commands', () => { let called = false; const r = checkOutput(() => { yargs('foo') .command('foo', 'foo command', noop, argv => { called = true; }) .option('bar', { describe: 'a foo command', demand: true, }) .parse(); }); called.should.equal(false); r.exitCode.should.equal(1); r.errors.should.match(/Missing required argument/); });
it('should support numeric commands', () => { const output = []; yargs('1') .command('1', 'numeric command', yargs => { output.push('1'); }) .parse(); output.should.include('1'); });
// see: https://github.com/yargs/yargs/issues/853 it('should not execute command if it is preceded by another positional argument', () => { let commandCalled = false; yargs() .command('foo', 'foo command', noop, () => { commandCalled = true; }) .parse('bar foo', (err, argv) => { expect(err).to.equal(null); commandCalled.should.equal(false); argv._.should.eql(['bar', 'foo']); }); });
// see: https://github.com/yargs/yargs/issues/861 phew! that's an edge-case. it('should allow positional arguments for inner commands in strict mode, when no handler is provided', () => { yargs() .command('foo', 'outer command', yargs => { yargs.command('bar [optional]', 'inner command'); }) .strict() .parse('foo bar 33', (err, argv) => { expect(err).to.equal(null); argv.optional.should.equal(33); argv._.should.eql(['foo', 'bar']); }); });
describe('usage', () => { it('allows you to configure a default command', () => { yargs() .usage('$0 <port>', 'default command', yargs => { yargs.positional('port', { type: 'string', }); }) .parse('33', (err, argv) => { expect(err).to.equal(null); argv.port.should.equal('33'); }); });
it('throws exception if default command does not have leading $0', () => { expect(() => { yargs().usage('<port>', 'default command', yargs => { yargs.positional('port', { type: 'string', }); }); }).to.throw(/.*\.usage\(\) description must start with \$0.*/); }); });
describe('async', () => { // addresses https://github.com/yargs/yargs/issues/510 it('fails when the promise returned by the command handler rejects', done => { const error = new Error(); yargs('foo') .command('foo', 'foo command', noop, yargs => Promise.reject(error)) .fail((msg, err) => { expect(msg).to.equal(null); expect(err).to.equal(error); done(); }) .parse(); });
it('returns promise that resolves arguments once handler succeeds', async () => { let complete = false; const handler = new Promise((resolve, reject) => { setTimeout(() => { complete = true; return resolve(); }, 10); }); const parsedPromise = yargs('foo hello') .command( 'foo <pos>', 'foo command', () => {}, () => handler ) .parse(); complete.should.equal(false); const parsed = await parsedPromise; complete.should.equal(true); parsed.pos.should.equal('hello'); });
it('returns promise that can be caught, when fail(false)', async () => { let complete = false; const handler = new Promise((resolve, reject) => { setTimeout(() => { complete = true; return reject(Error('error from handler')); }, 10); }); const parsedPromise = yargs('foo hello') .command( 'foo <pos>', 'foo command', () => {}, () => handler ) .fail(false) .parse(); try { complete.should.equal(false); await parsedPromise; throw Error('unreachable'); } catch (err) { err.message.should.match(/error from handler/); complete.should.equal(true); } });
// see: https://github.com/yargs/yargs/issues/1144 it('displays error and appropriate help message when handler fails', async () => { // the bug reported in #1144 only happens when // usage.help() is called, this does not occur when // console output is suppressed. tldr; we capture // the log output: const r = await checkOutput(async () => { return yargs('foo') .command( 'foo', 'foo command', yargs => { yargs.option('bar', { describe: 'bar option', }); }, argv => { return Promise.reject(Error('foo error')); } ) .exitProcess(false) .parse(); }); const errorLog = r.errors.join('\n'); // ensure the appropriate help is displayed: errorLog.should.include('bar option'); // ensure error was displayed: errorLog.should.include('foo error'); }); });
// see: https://github.com/yargs/yargs/issues/1099 it('does not coerce number from positional with leading "+"', () => { const argv = yargs.command('$0 <phone>', '', yargs => {}).parse('+5550100'); argv.phone.should.equal('+5550100'); });
it('allows an array of commands to be provided', () => { const innerCommands = [ { command: 'c <x> <y>', describe: 'add x to y', builder: () => {}, handler: argv => { argv.output.value = argv.x + argv.y; }, }, ]; const cmds = [ { command: 'a', describe: 'numeric comamand', builder: yargs => { yargs.command(innerCommands); }, handler: () => {}, }, ]; const context = { output: {}, }; yargs().command(cmds).parse('a c 10 5', context); context.output.value.should.equal(15); });
it('allows async exception in handler to be caught', async () => { await assert.rejects( yargs(['mw']) .fail(false) .command( 'mw', 'adds func to middleware', () => {}, async () => { throw Error('not cool'); } ) .parse(), /not cool/ ); });
it('reports error if error occurs parsing positional', () => { try { yargs(['cmd', 'neat']) .fail(false) .command('cmd <foo>', 'run a foo command', yargs => { yargs.option('foo', { nargs: 3, }); }) .parse(); throw Error('unreachable'); } catch (err) { err.message.should.match(/Not enough arguments/); } });
describe('async builder', async () => { it('allows positionals to be configured asynchronously', async () => { const argvPromise = yargs(['cmd', '999']) .command('cmd <foo>', 'a test command', async yargs => { await wait(); yargs.positional('foo', { type: 'string', }); }) .parse(); (typeof argvPromise.then).should.equal('function'); const argv = await argvPromise; (typeof argv.foo).should.equal('string'); }); describe('helpOrVersionSet', () => { it('--help', async () => { let set = false; await yargs() .command('cmd <foo>', 'a test command', (yargs, helpOrVersionSet) => { set = helpOrVersionSet; if (!helpOrVersionSet) { return wait(); } }) .parse('cmd --help', () => {}); assert.strictEqual(set, true); }); }); // Refs: https://github.com/yargs/yargs/issues/1894 it('does not print to stdout when parse callback is provided', async () => { await yargs() .command('cmd <foo>', 'a test command', async () => { await wait(); }) .parse('cmd --help', (_err, argv, output) => { output.should.include('a test command'); argv.help.should.equal(true); }); }); // Refs: https://github.com/yargs/yargs/issues/1917 it('allows command to be defined in async builder', async () => { let invoked = false; await yargs('alpha beta') .strict() .command({ command: 'alpha', describe: 'A', builder: async yargs => { await wait(); yargs .command({ command: 'beta', describe: 'B', handler: () => { invoked = true; }, }) .demandCommand(1); }, }) .demandCommand(1) .parse(); assert.strictEqual(invoked, true); }); it('allows deeply nested command to be defined in async builder', async () => { let invoked = false; await yargs('alpha beta gamma') .strict() .command('alpha', 'A', async yargs => { await wait(); yargs .command({ command: 'beta', describe: 'B', builder: async yargs => { await wait(); return yargs.command( 'gamma', 'C', async () => { await wait(); }, async () => { await wait(); invoked = true; } ); }, }) .demandCommand(1); }) .demandCommand(1) .parse(); assert.strictEqual(invoked, true); }); });
describe('builder', () => { // Refs: https://github.com/yargs/yargs/issues/1042 describe('helpOrVersionSet', () => { it('--version', () => { let set = false; yargs() .command('cmd <foo>', 'a test command', (yargs, helpOrVersionSet) => { set = helpOrVersionSet; }) .parse('cmd --version', () => {}); assert.strictEqual(set, true); }); it('--help', () => { let set = false; yargs() .command('cmd <foo>', 'a test command', (yargs, helpOrVersionSet) => { set = helpOrVersionSet; }) .parse('cmd --help', () => {}); assert.strictEqual(set, true); }); it('help', () => { let set = false; yargs() .command('cmd <foo>', 'a test command', (yargs, helpOrVersionSet) => { set = helpOrVersionSet; }) .parse('cmd help', () => {}); assert.strictEqual(set, true); }); it('cmd', () => { let set = false; const argv = yargs() .command('cmd <foo>', 'a test command', (yargs, helpOrVersionSet) => { set = helpOrVersionSet; }) .parse('cmd bar', () => {}); assert.strictEqual(set, false); assert.strictEqual(argv.foo, 'bar'); }); }); });});
yargs

Version Info

Tagged at
a year ago