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
|
yargs
========
Yargs be a node.js library fer hearties tryin' ter parse optstrings.
With yargs, ye be havin' a map that leads straight to yer treasure! Treasure of course, being a simple option hash.
[![Build Status][travis-image]][travis-url]
[![Coverage Status][coveralls-image]][coveralls-url]
[![NPM version][npm-image]][npm-url]
[![Windows Tests][windows-image]][windows-url]
[![js-standard-style][standard-image]][standard-url]
[![standard-version][standard-version-image]][standard-version-url]
[![Gitter][gitter-image]][gitter-url]
> Yargs is the official successor to optimist. Please feel free to submit issues and pull requests. If you'd like to contribute and don't know where to start, have a look at [the issue list](https://github.com/yargs/yargs/issues) :)
examples
========
With yargs, the options be just a hash!
-------------------------------------------------------------------
plunder.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs').argv;
if (argv.ships > 3 && argv.distance < 53.5) {
console.log('Plunder more riffiwobbles!');
} else {
console.log('Retreat from the xupptumblers!');
}
````
***
$ ./plunder.js --ships=4 --distance=22
Plunder more riffiwobbles!
$ ./plunder.js --ships 12 --distance 98.7
Retreat from the xupptumblers!
![Joe was one optimistic pirate.](http://i.imgur.com/4WFGVJ9.png)
But don't walk the plank just yet! There be more! You can do short options:
-------------------------------------------------
short.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs').argv;
console.log('(%d,%d)', argv.x, argv.y);
````
***
$ ./short.js -x 10 -y 21
(10,21)
And booleans, both long, short, and even grouped:
----------------------------------
bool.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs').argv;
if (argv.s) {
process.stdout.write(argv.fr ? 'Le perroquet dit: ' : 'The parrot says: ');
}
console.log(
(argv.fr ? 'couac' : 'squawk') + (argv.p ? '!' : '')
);
````
***
$ ./bool.js -s
The parrot says: squawk
$ ./bool.js -sp
The parrot says: squawk!
$ ./bool.js -sp --fr
Le perroquet dit: couac!
And non-hyphenated options too! Just use `argv._`!
-------------------------------------------------
nonopt.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs').argv;
console.log('(%d,%d)', argv.x, argv.y);
console.log(argv._);
````
***
$ ./nonopt.js -x 6.82 -y 3.35 rum
(6.82,3.35)
[ 'rum' ]
$ ./nonopt.js "me hearties" -x 0.54 yo -y 1.12 ho
(0.54,1.12)
[ 'me hearties', 'yo', 'ho' ]
Yargs even counts your booleans!
----------------------------------------------------------------------
count.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.count('verbose')
.alias('v', 'verbose')
.argv;
VERBOSE_LEVEL = argv.verbose;
function WARN() { VERBOSE_LEVEL >= 0 && console.log.apply(console, arguments); }
function INFO() { VERBOSE_LEVEL >= 1 && console.log.apply(console, arguments); }
function DEBUG() { VERBOSE_LEVEL >= 2 && console.log.apply(console, arguments); }
WARN("Showing only important stuff");
INFO("Showing semi-important stuff too");
DEBUG("Extra chatty mode");
````
***
$ node count.js
Showing only important stuff
$ node count.js -v
Showing only important stuff
Showing semi-important stuff too
$ node count.js -vv
Showing only important stuff
Showing semi-important stuff too
Extra chatty mode
$ node count.js -v --verbose
Showing only important stuff
Showing semi-important stuff too
Extra chatty mode
Tell users how to use yer options and make demands.
-------------------------------------------------
area.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.usage('Usage: $0 -w [num] -h [num]')
.demand(['w','h'])
.argv;
console.log("The area is:", argv.w * argv.h);
````
***
$ ./area.js -w 55 -h 11
The area is: 605
$ node ./area.js -w 4.91 -w 2.51
Usage: area.js -w [num] -h [num]
Options:
-w [required]
-h [required]
Missing required arguments: h
After yer demands have been met, demand more! Ask for non-hyphenated arguments!
-----------------------------------------
demand_count.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.demand(2)
.argv;
console.dir(argv);
````
***
$ ./demand_count.js a
Not enough non-option arguments: got 1, need at least 2
$ ./demand_count.js a b
{ _: [ 'a', 'b' ], '$0': 'demand_count.js' }
$ ./demand_count.js a b c
{ _: [ 'a', 'b', 'c' ], '$0': 'demand_count.js' }
EVEN MORE SHIVER ME TIMBERS!
------------------
default_singles.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.default('x', 10)
.default('y', 10)
.argv
;
console.log(argv.x + argv.y);
````
***
$ ./default_singles.js -x 5
15
default_hash.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.default({ x : 10, y : 10 })
.argv
;
console.log(argv.x + argv.y);
````
***
$ ./default_hash.js -y 7
17
And if you really want to get all descriptive about it...
---------------------------------------------------------
boolean_single.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.boolean('v')
.argv
;
console.dir(argv.v);
console.dir(argv._);
````
***
$ ./boolean_single.js -v "me hearties" yo ho
true
[ 'me hearties', 'yo', 'ho' ]
boolean_double.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.boolean(['x','y','z'])
.argv
;
console.dir([ argv.x, argv.y, argv.z ]);
console.dir(argv._);
````
***
$ ./boolean_double.js -x -z one two three
[ true, false, true ]
[ 'one', 'two', 'three' ]
Yargs is here to help you...
---------------------------
Ye can describe parameters fer help messages and set aliases. Yargs figures
out how ter format a handy help string automatically.
line_count.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.usage('Usage: $0 <command> [options]')
.command('count', 'Count the lines in a file')
.example('$0 count -f foo.js', 'count the lines in the given file')
.alias('f', 'file')
.nargs('f', 1)
.describe('f', 'Load a file')
.demand(1, ['f'])
.help('h')
.alias('h', 'help')
.epilog('copyright 2015')
.argv;
var fs = require('fs');
var s = fs.createReadStream(argv.file);
var lines = 0;
s.on('data', function (buf) {
lines += buf.toString().match(/\n/g).length;
});
s.on('end', function () {
console.log(lines);
});
````
***
$ node line_count.js count
Usage: line_count.js <command> [options]
Commands:
count Count the lines in a file
Options:
-f, --file Load a file [required]
-h, --help Show help [boolean]
Examples:
line_count.js count -f foo.js count the lines in the given file
copyright 2015
Missing required arguments: f
$ node line_count.js count --file line_count.js
26
$ node line_count.js count -f line_count.js
26
methods
=======
By itself,
````javascript
require('yargs').argv
````
will use the `process.argv` array to construct the `argv` object.
You can pass in the `process.argv` yourself:
````javascript
require('yargs')([ '-x', '1', '-y', '2' ]).argv
````
or use `.parse()` to do the same thing:
````javascript
require('yargs').parse([ '-x', '1', '-y', '2' ])
````
The rest of these methods below come in just before the terminating `.argv`.
<a name="alias"></a>.alias(key, alias)
------------------
Set key names as equivalent such that updates to a key will propagate to aliases
and vice-versa.
Optionally `.alias()` can take an object that maps keys to aliases.
Each key of this object should be the canonical version of the option, and each
value should be a string or an array of strings.
.argv
-----
Get the arguments as a plain old object.
Arguments without a corresponding flag show up in the `argv._` array.
The script name or node command is available at `argv.$0` similarly to how `$0`
works in bash or perl.
If `yargs` is executed in an environment that embeds node and there's no script name (e.g.
[Electron](http://electron.atom.io/) or [nw.js](http://nwjs.io/)), it will ignore the first parameter since it
expects it to be the script name. In order to override this behavior, use `.parse(process.argv.slice(1))`
instead of `.argv` and the first parameter won't be ignored.
<a name="array"></a>.array(key)
----------
Tell the parser to interpret `key` as an array. If `.array('foo')` is set,
`--foo foo bar` will be parsed as `['foo', 'bar']` rather than as `'foo'`.
<a name="boolean"></a>.boolean(key)
-------------
Interpret `key` as a boolean. If a non-flag option follows `key` in
`process.argv`, that string won't get set as the value of `key`.
`key` will default to `false`, unless a `default(key, undefined)` is
explicitly set.
If `key` is an array, interpret all the elements as booleans.
.check(fn)
----------
Check that certain conditions are met in the provided arguments.
`fn` is called with two arguments, the parsed `argv` hash and an array of options and their aliases.
If `fn` throws or returns a non-truthy value, show the thrown error, usage information, and
exit.
<a name="choices"></a>.choices(key, choices)
----------------------
Limit valid values for `key` to a predefined set of `choices`, given as an array
or as an individual value.
```js
var argv = require('yargs')
.alias('i', 'ingredient')
.describe('i', 'choose your sandwich ingredients')
.choices('i', ['peanut-butter', 'jelly', 'banana', 'pickles'])
.help('help')
.argv
```
If this method is called multiple times, all enumerated values will be merged
together. Choices are generally strings or numbers, and value matching is
case-sensitive.
Optionally `.choices()` can take an object that maps multiple keys to their
choices.
Choices can also be specified as `choices` in the object given to `option()`.
```js
var argv = require('yargs')
.option('size', {
alias: 's',
describe: 'choose a size',
choices: ['xs', 's', 'm', 'l', 'xl']
})
.argv
```
<a name="coerce"></a>.coerce(key, fn)
----------------
Provide a synchronous function to coerce or transform the value(s) given on the
command line for `key`.
The coercion function should accept one argument, representing the parsed value
from the command line, and should return a new value or throw an error. The
returned value will be used as the value for `key` (or one of its aliases) in
`argv`.
If the function throws, the error will be treated as a validation
failure, delegating to either a custom [`.fail()`](#fail) handler or printing
the error message in the console.
Coercion will be applied to a value after
all other modifications, such as [`.normalize()`](#normalize).
_Examples:_
```js
var argv = require('yargs')
.coerce('file', function (arg) {
return require('fs').readFileSync(arg, 'utf8')
})
.argv
```
Optionally `.coerce()` can take an object that maps several keys to their
respective coercion function.
```js
var argv = require('yargs')
.coerce({
date: Date.parse,
json: JSON.parse
})
.argv
```
You can also map the same function to several keys at one time. Just pass an
array of keys as the first argument to `.coerce()`:
```js
var path = require('path')
var argv = require('yargs')
.coerce(['src', 'dest'], path.resolve)
.argv
```
If you are using dot-notion or arrays, .e.g., `user.email` and `user.password`,
coercion will be applied to the final object that has been parsed:
```js
// --user.name Batman --user.password 123
// gives us: {name: 'batman', password: '[SECRET]'}
var argv = require('yargs')
.option('user')
.coerce('user', opt => {
opt.name = opt.name.toLowerCase()
opt.password = '[SECRET]'
return opt
})
.argv
```
.command(cmd, desc, [builder], [handler])
-----------------------------------------
.command(cmd, desc, [module])
-----------------------------
.command(module)
----------------
Define the commands exposed by your application.
`cmd` should be a string representing the command or an array of strings
representing the command and its aliases. Read more about command aliases in the
subsection below.
Use `desc` to provide a description for each command your application accepts (the
values stored in `argv._`). Set `desc` to `false` to create a hidden command.
Hidden commands don't show up in the help output and aren't available for
completion.
Optionally, you can provide a `builder` object to give hints about the
options that your command accepts:
```js
yargs
.command('get', 'make a get HTTP request', {
url: {
alias: 'u',
default: 'http://yargs.js.org/'
}
})
.help()
.argv
```
Note that commands will not automatically inherit configuration _or_ options
of their parent context. This means you'll have to re-apply configuration
if necessary, and make options global manually using the [global](#global) method.
Additionally, the [`help`](#help) and [`version`](#version)
options (if used) **always** apply globally, just like the
[`.wrap()`](#wrap) configuration.
`builder` can also be a function. This function is executed
with a `yargs` instance, and can be used to provide _advanced_ command specific help:
```js
yargs
.command('get', 'make a get HTTP request', function (yargs) {
return yargs.option('url', {
alias: 'u',
default: 'http://yargs.js.org/'
})
})
.help()
.argv
```
You can also provide a handler function, which will be executed with the
parsed `argv` object:
```js
yargs
.command(
'get',
'make a get HTTP request',
function (yargs) {
return yargs.option('u', {
alias: 'url',
describe: 'the URL to make an HTTP request to'
})
},
function (argv) {
console.log(argv.url)
}
)
.help()
.argv
```
### Positional Arguments
Commands can accept _optional_ and _required_ positional arguments. Required
positional arguments take the form `<foo>`, and optional arguments
take the form `[bar]`. The parsed positional arguments will be populated in
`argv`:
```js
yargs.command('get <source> [proxy]', 'make a get HTTP request')
.help()
.argv
```
#### Variadic Positional Arguments
The last positional argument can optionally accept an array of
values, by using the `..` operator:
```js
yargs.command('download <url> [files..]', 'download several files')
.help()
.argv
```
### Command Execution
When a command is given on the command line, yargs will execute the following:
1. push the command into the current context
2. reset non-global configuration
3. apply command configuration via the `builder`, if given
4. parse and validate args from the command line, including positional args
5. if validation succeeds, run the `handler` function, if given
6. pop the command from the current context
### Command Aliases
You can define aliases for a command by putting the command and all of its
aliases into an array.
Alternatively, a command module may specify an `aliases` property, which may be
a string or an array of strings. All aliases defined via the `command` property
and the `aliases` property will be concatenated together.
The first element in the array is considered the canonical command, which may
define positional arguments, and the remaining elements in the array are
considered aliases. Aliases inherit positional args from the canonical command,
and thus any positional args defined in the aliases themselves are ignored.
If either the canonical command or any of its aliases are given on the command
line, the command will be executed.
```js
#!/usr/bin/env node
require('yargs')
.command(['start [app]', 'run', 'up'], 'Start up an app', {}, (argv) => {
console.log('starting up the', argv.app || 'default', 'app')
})
.command({
command: 'configure <key> [value]',
aliases: ['config', 'cfg'],
desc: 'Set a config variable',
builder: (yargs) => yargs.default('value', 'true'),
handler: (argv) => {
console.log(`setting ${argv.key} to ${argv.value}`)
}
})
.demand(1)
.help()
.wrap(72)
.argv
```
```
$ ./svc.js help
Commands:
start [app] Start up an app [aliases: run, up]
configure <key> [value] Set a config variable [aliases: config, cfg]
Options:
--help Show help [boolean]
$ ./svc.js cfg concurrency 4
setting concurrency to 4
$ ./svc.js run web
starting up the web app
```
### Providing a Command Module
For complicated commands you can pull the logic into a module. A module
simply needs to export:
* `exports.command`: string (or array of strings) that executes this command when given on the command line, first string may contain positional args
* `exports.aliases`: array of strings (or a single string) representing aliases of `exports.command`, positional args defined in an alias are ignored
* `exports.describe`: string used as the description for the command in help text, use `false` for a hidden command
* `exports.builder`: object declaring the options the command accepts, or a function accepting and returning a yargs instance
* `exports.handler`: a function which will be passed the parsed argv.
```js
// my-module.js
exports.command = 'get <source> [proxy]'
exports.describe = 'make a get HTTP request'
exports.builder = {
banana: {
default: 'cool'
},
batman: {
default: 'sad'
}
}
exports.handler = function (argv) {
// do something with argv.
}
```
You then register the module like so:
```js
yargs.command(require('my-module'))
.help()
.argv
```
Or if the module does not export `command` and `describe` (or if you just want to override them):
```js
yargs.command('get <source> [proxy]', 'make a get HTTP request', require('my-module'))
.help()
.argv
```
.commandDir(directory, [opts])
------------------------------
Apply command modules from a directory relative to the module calling this method.
This allows you to organize multiple commands into their own modules under a
single directory and apply all of them at once instead of calling
`.command(require('./dir/module'))` multiple times.
By default, it ignores subdirectories. This is so you can use a directory
structure to represent your command hierarchy, where each command applies its
subcommands using this method in its builder function. See the example below.
Note that yargs assumes all modules in the given directory are command modules
and will error if non-command modules are encountered. In this scenario, you
can either move your module to a different directory or use the `exclude` or
`visit` option to manually filter it out. More on that below.
`directory` is a relative directory path as a string (required).
`opts` is an options object (optional). The following options are valid:
- `recurse`: boolean, default `false`
Look for command modules in all subdirectories and apply them as a flattened
(non-hierarchical) list.
- `extensions`: array of strings, default `['js']`
The types of files to look for when requiring command modules.
- `visit`: function
A synchronous function called for each command module encountered. Accepts
`commandObject`, `pathToFile`, and `filename` as arguments. Returns
`commandObject` to include the command; any falsy value to exclude/skip it.
- `include`: RegExp or function
Whitelist certain modules. See [`require-directory` whitelisting](https://www.npmjs.com/package/require-directory#whitelisting) for details.
- `exclude`: RegExp or function
Blacklist certain modules. See [`require-directory` blacklisting](https://www.npmjs.com/package/require-directory#blacklisting) for details.
### Example command hierarchy using `.commandDir()`
Desired CLI:
```sh
$ myapp --help
$ myapp init
$ myapp remote --help
$ myapp remote add base http://yargs.js.org
$ myapp remote prune base
$ myapp remote prune base fork whatever
```
Directory structure:
```
myapp/
├─ cli.js
└─ cmds/
├─ init.js
├─ remote.js
└─ remote_cmds/
├─ add.js
└─ prune.js
```
cli.js:
```js
#!/usr/bin/env node
require('yargs')
.commandDir('cmds')
.demand(1)
.help()
.argv
```
cmds/init.js:
```js
exports.command = 'init [dir]'
exports.desc = 'Create an empty repo'
exports.builder = {
dir: {
default: '.'
}
}
exports.handler = function (argv) {
console.log('init called for dir', argv.dir)
}
```
cmds/remote.js:
```js
exports.command = 'remote <command>'
exports.desc = 'Manage set of tracked repos'
exports.builder = function (yargs) {
return yargs.commandDir('remote_cmds')
}
exports.handler = function (argv) {}
```
cmds/remote_cmds/add.js:
```js
exports.command = 'add <name> <url>'
exports.desc = 'Add remote named <name> for repo at url <url>'
exports.builder = {}
exports.handler = function (argv) {
console.log('adding remote %s at url %s', argv.name, argv.url)
}
```
cmds/remote_cmds/prune.js:
```js
exports.command = 'prune <name> [names..]'
exports.desc = 'Delete tracked branches gone stale for remotes'
exports.builder = {}
exports.handler = function (argv) {
console.log('pruning remotes %s', [].concat(argv.name).concat(argv.names).join(', '))
}
```
.completion([cmd], [description], [fn])
---------------------------------------
Enable bash-completion shortcuts for commands and options.
`cmd`: When present in `argv._`, will result in the `.bashrc` completion script
being outputted. To enable bash completions, concat the generated script to your
`.bashrc` or `.bash_profile`.
`description`: Provide a description in your usage instructions for the command
that generates bash completion scripts.
`fn`: Rather than relying on yargs' default completion functionality, which
shiver me timbers is pretty awesome, you can provide your own completion
method.
If invoked without parameters, `.completion()` will make `completion` the command to output
the completion script.
```js
var argv = require('yargs')
.completion('completion', function(current, argv) {
// 'current' is the current command being completed.
// 'argv' is the parsed arguments so far.
// simply return an array of completions.
return [
'foo',
'bar'
];
})
.argv;
```
You can also provide asynchronous completions.
```js
var argv = require('yargs')
.completion('completion', function(current, argv, done) {
setTimeout(function() {
done([
'apple',
'banana'
]);
}, 500);
})
.argv;
```
But wait, there's more! You can return an asynchronous promise.
```js
var argv = require('yargs')
.completion('completion', function(current, argv, done) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(['apple', 'banana'])
}, 10)
})
})
.argv;
```
<a name="config"></a>.config([key], [description], [parseFn])
-------------------------------------------------------------
.config(object)
---------------
Tells the parser that if the option specified by `key` is passed in, it
should be interpreted as a path to a JSON config file. The file is loaded
and parsed, and its properties are set as arguments. Because the file is
loaded using Node's require(), the filename MUST end in `.json` to be
interpreted correctly.
If invoked without parameters, `.config()` will make `--config` the option to pass the JSON config file.
An optional `description` can be provided to customize the config (`key`) option
in the usage string.
An optional `parseFn` can be used to provide a custom parser. The parsing
function must be synchronous, and should return an object containing
key value pairs or an error.
```js
var argv = require('yargs')
.config('settings', function (configPath) {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'))
})
.argv
```
You can also pass an explicit configuration `object`, it will be parsed
and its properties will be set as arguments.
```js
var argv = require('yargs')
.config({foo: 1, bar: 2})
.argv
console.log(argv)
```
```
$ node test.js
{ _: [],
foo: 1,
bar: 2,
'$0': 'test.js' }
```
<a name="count"></a>.count(key)
------------
Interpret `key` as a boolean flag, but set its parsed value to the number of
flag occurrences rather than `true` or `false`. Default value is thus `0`.
<a name="default"></a>.default(key, value, [description])
---------------------------------------------------------
.defaults(key, value, [description])
------------------------------------
**Note:** The `.defaults()` alias is deprecated. It will be
removed in the next major version.
Set `argv[key]` to `value` if no option was specified in `process.argv`.
Optionally `.default()` can take an object that maps keys to default values.
But wait, there's more! The default value can be a `function` which returns
a value. The name of the function will be used in the usage string:
```js
var argv = require('yargs')
.default('random', function randomValue() {
return Math.random() * 256;
}).argv;
```
Optionally, `description` can also be provided and will take precedence over
displaying the value in the usage instructions:
```js
.default('timeout', 60000, '(one-minute)')
```
<a name="demand"></a>.demand(key, [msg | boolean])
------------------------------
.demand(count, [max], [msg])
------------------------------
If `key` is a string, show the usage information and exit if `key` wasn't
specified in `process.argv`.
If `key` is a number, demand at least as many non-option arguments, which show
up in `argv._`. A second number can also optionally be provided, which indicates
the maximum number of non-option arguments.
If `key` is an array, demand each element.
If a `msg` string is given, it will be printed when the argument is missing,
instead of the standard error message. This is especially helpful for the non-option arguments in `argv._`.
If a `boolean` value is given, it controls whether the option is demanded;
this is useful when using `.options()` to specify command line parameters.
A combination of `.demand(1)` and `.strict()` will allow you to require a user to pass at least one command:
```js
var argv = require('yargs')
.command('install', 'tis a mighty fine package to install')
.demand(1)
.strict()
.argv
```
Similarly, you can require a command and arguments at the same time:
```js
var argv = require('yargs')
.command('install', 'tis a mighty fine package to install')
.demand(1, ['w', 'm'])
.strict()
.argv
```
<a name="describe"></a>.describe(key, desc)
--------------------
Describe a `key` for the generated usage information.
Optionally `.describe()` can take an object that maps keys to descriptions.
.detectLocale(boolean)
-----------
Should yargs attempt to detect the os' locale? Defaults to `true`.
.env([prefix])
--------------
Tell yargs to parse environment variables matching the given prefix and apply
them to argv as though they were command line arguments.
Use the "__" separator in the environment variable to indicate nested options.
(e.g. prefix_nested__foo => nested.foo)
If this method is called with no argument or with an empty string or with `true`,
then all env vars will be applied to argv.
Program arguments are defined in this order of precedence:
1. Command line args
2. Config file
3. Env var
4. Configured defaults
```js
var argv = require('yargs')
.env('MY_PROGRAM')
.option('f', {
alias: 'fruit-thing',
default: 'apple'
})
.argv
console.log(argv)
```
```
$ node fruity.js
{ _: [],
f: 'apple',
'fruit-thing': 'apple',
fruitThing: 'apple',
'$0': 'fruity.js' }
```
```
$ MY_PROGRAM_FRUIT_THING=banana node fruity.js
{ _: [],
fruitThing: 'banana',
f: 'banana',
'fruit-thing': 'banana',
'$0': 'fruity.js' }
```
```
$ MY_PROGRAM_FRUIT_THING=banana node fruity.js -f cat
{ _: [],
f: 'cat',
'fruit-thing': 'cat',
fruitThing: 'cat',
'$0': 'fruity.js' }
```
Env var parsing is disabled by default, but you can also explicitly disable it
by calling `.env(false)`, e.g. if you need to undo previous configuration.
.epilog(str)
------------
.epilogue(str)
--------------
A message to print at the end of the usage instructions, e.g.
```js
var argv = require('yargs')
.epilogue('for more information, find our manual at http://example.com');
```
.example(cmd, desc)
-------------------
Give some example invocations of your program. Inside `cmd`, the string
`$0` will get interpolated to the current script name or node command for the
present script similar to how `$0` works in bash or perl.
Examples will be printed out as part of the help message.
<a name="exitprocess"></a>.exitProcess(enable)
----------------------------------
By default, yargs exits the process when the user passes a help flag, uses the
`.version` functionality, or when validation fails. Calling
`.exitProcess(false)` disables this behavior, enabling further actions after
yargs have been validated.
<a name="fail"></a>.fail(fn)
---------
Method to execute when a failure occurs, rather than printing the failure message.
`fn` is called with the failure message that would have been printed, the
`Error` instance originally thrown and yargs state when the failure
occured.
```js
var argv = require('yargs')
.fail(function (msg, err, yargs) {
if (err) throw err // preserve stack
console.error('You broke it!')
console.error(msg)
console.error('You should be doing', yargs.help())
process.exit(1)
})
.argv
```
.getCompletion(args, done);
---------------------------
Allows to programmatically get completion choices for any line.
`args`: An array of the words in the command line to complete.
`done`: The callback to be called with the resulting completions.
For example:
```js
require('yargs')
.option('foobar')
.option('foobaz')
.completion()
.getCompletion(['./test.js', '--foo'], function (completions) {
console.log(completions)
})
```
Outputs the same completion choices as `./test.js --foo`<kbd>TAB</kbd>: `--foobar` and `--foobaz`
<a name="global"></a>.global(globals)
------------
Indicate that an option (or group of options) should not be reset when a command
is executed, as an example:
```js
var argv = require('yargs')
.option('a', {
alias: 'all',
default: true
})
.option('n', {
alias: 'none',
default: true
})
.command('foo', 'foo command', function (yargs) {
return yargs.option('b', {
alias: 'bar'
})
})
.help('help')
.global('a')
.argv
```
If the `foo` command is executed the `all` option will remain, but the `none`
option will have been eliminated.
`help`, `version`, and `completion` options default to being global.
<a name="group"></a>.group(key(s), groupName)
--------------------
Given a key, or an array of keys, places options under an alternative heading
when displaying usage instructions, e.g.,
```js
var yargs = require('yargs')(['--help'])
.help()
.group('batman', 'Heroes:')
.describe('batman', "world's greatest detective")
.wrap(null)
.argv
```
***
Heroes:
--batman world's greatest detective
Options:
--help Show help [boolean]
<a name="help"></a>.help()
-----------------------------------------
.help([option | boolean])
-----------------------------------------
.help([option, [description | boolean]])
-----------------------------------------
.help([option, [description, [boolean]]])
-----------------------------------------
Add an option (e.g. `--help`) and implicit command that displays the usage
string and exits the process.
If present, the `description` parameter customizes the description of
the help option in the usage string.
If a boolean argument is provided, it will enable or disable the use of an
implicit command. The implicit command is enabled by default, but it can be
disabled by passing `false`.
Note that any multi-char aliases (e.g. `help`) used for the help option will
also be used for the implicit command. If there are no multi-char aliases (e.g.
`h`), then all single-char aliases will be used for the command.
If invoked without parameters, `.help()` will use `--help` as the option and
`help` as the implicit command to trigger help output.
Example:
```js
var yargs = require("yargs")(['--help'])
.usage("$0 -operand1 number -operand2 number -operation [add|subtract]")
.help()
.argv
```
Later on, `argv` can be retrieved with `yargs.argv`.
.implies(x, y)
--------------
Given the key `x` is set, it is required that the key `y` is set.
Optionally `.implies()` can accept an object specifying multiple implications.
.locale()
---------
Return the locale that yargs is currently using.
By default, yargs will auto-detect the operating system's locale so that
yargs-generated help content will display in the user's language.
To override this behavior with a static locale, pass the desired locale as a
string to this method (see below).
.locale(locale)
---------------
Override the auto-detected locale from the user's operating system with a static
locale. Note that the OS locale can be modified by setting/exporting the `LC_ALL`
environment variable.
```js
var argv = require('yargs')
.usage('./$0 - follow ye instructions true')
.option('option', {
alias: 'o',
describe: "'tis a mighty fine option",
demand: true
})
.command('run', "Arrr, ya best be knowin' what yer doin'")
.example('$0 run foo', "shiver me timbers, here's an example for ye")
.help('help')
.wrap(70)
.locale('pirate')
.argv
```
***
```shell
./test.js - follow ye instructions true
Choose yer command:
run Arrr, ya best be knowin' what yer doin'
Options for me hearties!
--option, -o 'tis a mighty fine option [requi-yar-ed]
--help Parlay this here code of conduct [boolean]
Ex. marks the spot:
test.js run foo shiver me timbers, here's an example for ye
Ye be havin' to set the followin' argument land lubber: option
```
Locales currently supported:
* **de:** German.
* **en:** American English.
* **es:** Spanish.
* **fr:** French.
* **hi:** Hindi.
* **hu:** Hungarian.
* **id:** Indonesian.
* **it:** Italian.
* **ja:** Japanese.
* **ko:** Korean.
* **nb:** Norwegian Bokmål.
* **pirate:** American Pirate.
* **pl:** Polish.
* **pt:** Portuguese.
* **pt_BR:** Brazilian Portuguese.
* **ru:** Russian.
* **th:** Thai.
* **tr:** Turkish.
* **zh_CN:** Chinese.
To submit a new translation for yargs:
1. use `./locales/en.json` as a starting point.
2. submit a pull request with the new locale file.
*The [Microsoft Terminology Search](http://www.microsoft.com/Language/en-US/Search.aspx) can be useful for finding the correct terminology in your locale.*
<a name="nargs"></a>.nargs(key, count)
-----------
The number of arguments that should be consumed after a key. This can be a
useful hint to prevent parsing ambiguity. For example:
```js
var argv = require('yargs')
.nargs('token', 1)
.parse(['--token', '-my-token']);
```
parses as:
`{ _: [], token: '-my-token', '$0': 'node test' }`
Optionally `.nargs()` can take an object of `key`/`narg` pairs.
<a name="normalize"></a>.normalize(key)
---------------
The key provided represents a path and should have `path.normalize()` applied.
<a name="number"></a>.number(key)
------------
Tell the parser to always interpret `key` as a number.
If `key` is an array, all elements will be parsed as numbers.
If the option is given on the command line without a value, `argv` will be
populated with `undefined`.
If the value given on the command line cannot be parsed as a number, `argv` will
be populated with `NaN`.
Note that decimals, hexadecimals, and scientific notation are all accepted.
```js
var argv = require('yargs')
.number('n')
.number(['width', 'height'])
.argv
```
.option(key, [opt])
-----------------
.options(key, [opt])
------------------
This method can be used to make yargs aware of options that _could_
exist. You can also pass an `opt` object which can hold further
customization, like `.alias()`, `.demand()` etc. for that option.
For example:
````javascript
var argv = require('yargs')
.option('f', {
alias: 'file',
demand: true,
default: '/etc/passwd',
describe: 'x marks the spot',
type: 'string'
})
.argv
;
````
is the same as
````javascript
var argv = require('yargs')
.alias('f', 'file')
.demand('f')
.default('f', '/etc/passwd')
.describe('f', 'x marks the spot')
.string('f')
.argv
;
````
Optionally `.options()` can take an object that maps keys to `opt` parameters.
````javascript
var argv = require('yargs')
.options({
'f': {
alias: 'file',
demand: true,
default: '/etc/passwd',
describe: 'x marks the spot',
type: 'string'
}
})
.argv
;
````
Valid `opt` keys include:
- `alias`: string or array of strings, alias(es) for the canonical option key, see [`alias()`](#alias)
- `array`: boolean, interpret option as an array, see [`array()`](#array)
- `boolean`: boolean, interpret option as a boolean flag, see [`boolean()`](#boolean)
- `choices`: value or array of values, limit valid option arguments to a predefined set, see [`choices()`](#choices)
- `coerce`: function, coerce or transform parsed command line values into another value, see [`coerce()`](#coerce)
- `config`: boolean, interpret option as a path to a JSON config file, see [`config()`](#config)
- `configParser`: function, provide a custom config parsing function, see [`config()`](#config)
- `count`: boolean, interpret option as a count of boolean flags, see [`count()`](#count)
- `default`: value, set a default value for the option, see [`default()`](#default)
- `defaultDescription`: string, use this description for the default value in help content, see [`default()`](#default)
- `demand`/`require`/`required`: boolean or string, demand the option be given, with optional error message, see [`demand()`](#demand)
- `desc`/`describe`/`description`: string, the option description for help content, see [`describe()`](#describe)
- `global`: boolean, indicate that this key should not be [reset](#reset) when a command is invoked, see [`global()`](#global)
- `group`: string, when displaying usage instructions place the option under an alternative group heading, see [`group()`](#group)
- `nargs`: number, specify how many arguments should be consumed for the option, see [`nargs()`](#nargs)
- `normalize`: boolean, apply `path.normalize()` to the option, see [`normalize()`](#normalize)
- `number`: boolean, interpret option as a number, [`number()`](#number)
- `requiresArg`: boolean, require the option be specified with a value, see [`requiresArg()`](#requiresArg)
- `skipValidation`: boolean, skips validation if the option is present, see [`skipValidation()`](#skipValidation)
- `string`: boolean, interpret option as a string, see [`string()`](#string)
- `type`: one of the following strings
- `'array'`: synonymous for `array: true`, see [`array()`](#array)
- `'boolean'`: synonymous for `boolean: true`, see [`boolean()`](#boolean)
- `'count'`: synonymous for `count: true`, see [`count()`](#count)
- `'number'`: synonymous for `number: true`, see [`number()`](#number)
- `'string'`: synonymous for `string: true`, see [`string()`](#string)
.parse(args, [context], [parseCallback])
------------
Parse `args` instead of `process.argv`. Returns the `argv` object.
`args` may either be a pre-processed argv array, or a raw argument string.
A `context` object can optionally be given as the second argument to `parse()`, providing a
useful mechanism for passing state information to commands:
```js
const parser = yargs
.command('lunch-train <restaurant>', 'start lunch train', function () {}, function (argv) {
console.log(argv.restaurant, argv.time)
})
.parse("lunch-train rudy's", {time: '12:15'})
```
A `parseCallback` can also be provided to `.parse()`. If a callback is given, it will be invoked with three arguments:
1. `err`: populated if any validation errors raised while parsing.
2. `argv`: the parsed argv object.
3. `output`: any text that would have been output to the terminal, had a
callback not been provided.
```js
// providing the `fn` argument to `parse()` runs yargs in headless mode, this
// makes it easy to use yargs in contexts other than the CLI, e.g., writing
// a chat-bot.
const parser = yargs
.command('lunch-train <restaurant> <time>', 'start lunch train', function () {}, function (argv) {
api.scheduleLunch(argv.restaurant, moment(argv.time))
})
.help()
parser.parse(bot.userText, function (err, argv, output) {
if (output) bot.respond(output)
})
```
***Note:*** Providing a callback to `parse()` disables the [`exitProcess` setting](#exitprocess) until after the callback is invoked.
.pkgConf(key, [cwd])
------------
Similar to [`config()`](#config), indicates that yargs should interpret the object from the specified key in package.json
as a configuration object.
`cwd` can optionally be provided, the package.json will be read
from this location.
.recommendCommands()
---------------------------
Should yargs provide suggestions regarding similar commands if no matching
command is found?
.require(key, [msg | boolean])
------------------------------
.required(key, [msg | boolean])
------------------------------
An alias for [`demand()`](#demand). See docs there.
<a name="requiresArg"></a>.requiresArg(key)
-----------------
Specifies either a single option key (string), or an array of options that
must be followed by option values. If any option value is missing, show the
usage information and exit.
The default behavior is to set the value of any key not followed by an
option value to `true`.
<a name="reset"></a>.reset()
--------
Reset the argument object built up so far. This is useful for
creating nested command line interfaces. Use [global](#global)
to specify keys that should not be reset.
```js
var yargs = require('yargs')
.usage('$0 command')
.command('hello', 'hello command')
.command('world', 'world command')
.demand(1, 'must provide a valid command'),
argv = yargs.argv,
command = argv._[0];
if (command === 'hello') {
yargs.reset()
.usage('$0 hello')
.help('h')
.example('$0 hello', 'print the hello message!')
.argv
console.log('hello!');
} else if (command === 'world'){
yargs.reset()
.usage('$0 world')
.help('h')
.example('$0 world', 'print the world message!')
.argv
console.log('world!');
} else {
yargs.showHelp();
}
```
.showCompletionScript()
----------------------
Generate a bash completion script. Users of your application can install this
script in their `.bashrc`, and yargs will provide completion shortcuts for
commands and options.
.showHelp(consoleLevel='error')
---------------------------
Print the usage data using the [`console`](https://nodejs.org/api/console.html) function `consoleLevel` for printing.
Example:
```js
var yargs = require("yargs")
.usage("$0 -operand1 number -operand2 number -operation [add|subtract]");
yargs.showHelp(); //prints to stderr using console.error()
```
Or, to print the usage data to `stdout` instead, you can specify the use of `console.log`:
```js
yargs.showHelp("log"); //prints to stdout using console.log()
```
Later on, `argv` can be retrieved with `yargs.argv`.
.showHelpOnFail(enable, [message])
----------------------------------
By default, yargs outputs a usage string if any error is detected. Use the
`.showHelpOnFail()` method to customize this behavior. If `enable` is `false`,
the usage string is not output. If the `message` parameter is present, this
message is output after the error message.
line_count.js:
````javascript
#!/usr/bin/env node
var argv = require('yargs')
.usage('Count the lines in a file.\nUsage: $0 -f <file>')
.demand('f')
.alias('f', 'file')
.describe('f', 'Load a file')
.string('f')
.showHelpOnFail(false, 'Specify --help for available options')
.help('help')
.argv;
// etc.
````
***
```
$ node line_count.js
Missing argument value: f
Specify --help for available options
```
<a name="skipValidation"></a>.skipValidation(key)
-----------------
Specifies either a single option key (string), or an array of options.
If any of the options is present, yargs validation is skipped.
.strict()
---------
Any command-line argument given that is not demanded, or does not have a
corresponding description, will be reported as an error.
<a name="string"></a>.string(key)
------------
Tell the parser logic not to interpret `key` as a number or boolean.
This can be useful if you need to preserve leading zeros in an input.
If `key` is an array, interpret all the elements as strings.
`.string('_')` will result in non-hyphenated arguments being interpreted as strings,
regardless of whether they resemble numbers.
.updateLocale(obj)
------------------
.updateStrings(obj)
------------------
Override the default strings used by yargs with the key/value
pairs provided in `obj`:
```js
var argv = require('yargs')
.command('run', 'the run command')
.help('help')
.updateStrings({
'Commands:': 'My Commands -->\n'
})
.wrap(null)
.argv
```
***
```shell
My Commands -->
run the run command
Options:
--help Show help [boolean]
```
If you explicitly specify a `locale()`, you should do so *before* calling
`updateStrings()`.
.usage(message, [opts])
---------------------
Set a usage message to show which commands to use. Inside `message`, the string
`$0` will get interpolated to the current script name or node command for the
present script similar to how `$0` works in bash or perl.
`opts` is optional and acts like calling `.options(opts)`.
<a name="version"></a>.version([option], [description], [version])
----------------------------------------
Add an option (e.g. `--version`) that displays the version number (given by the
`version` parameter) and exits the process.
If no arguments are passed to `version` (`.version()`), yargs will parse the `package.json`
of your module and use its `version` value. The default value of `option` is `--version`.
You can provide a `function` for version, rather than a string.
This is useful if you want to use a version stored in a location other than package.json:
```js
var argv = require('yargs')
.version(function() {
return require('../lib/version').version;
})
.argv;
```
<a name="wrap"></a>.wrap(columns)
--------------
Format usage output to wrap at `columns` many columns.
By default wrap will be set to `Math.min(80, windowWidth)`. Use `.wrap(null)` to
specify no column limit (no right-align). Use `.wrap(yargs.terminalWidth())` to
maximize the width of yargs' usage instructions.
parsing tricks
==============
stop parsing
------------
Use `--` to stop parsing flags and stuff the remainder into `argv._`.
$ node examples/reflect.js -a 1 -b 2 -- -c 3 -d 4
{ _: [ '-c', '3', '-d', '4' ],
a: 1,
b: 2,
'$0': 'examples/reflect.js' }
negate fields
-------------
If you want to explicitly set a field to false instead of just leaving it
undefined or to override a default you can do `--no-key`.
$ node examples/reflect.js -a --no-b
{ _: [], a: true, b: false, '$0': 'examples/reflect.js' }
numbers
-------
Every argument that looks like a number (`!isNaN(Number(arg))`) is converted to
one. This way you can just `net.createConnection(argv.port)` and you can add
numbers out of `argv` with `+` without having that mean concatenation,
which is super frustrating.
duplicates
----------
If you specify a flag multiple times it will get turned into an array containing
all the values in order.
$ node examples/reflect.js -x 5 -x 8 -x 0
{ _: [], x: [ 5, 8, 0 ], '$0': 'examples/reflect.js' }
dot notation
------------
When you use dots (`.`s) in argument names, an implicit object path is assumed.
This lets you organize arguments into nested objects.
$ node examples/reflect.js --foo.bar.baz=33 --foo.quux=5
{ _: [],
foo: { bar: { baz: 33 }, quux: 5 },
'$0': 'examples/reflect.js' }
short numbers
-------------
Short numeric `-n5` style arguments work too:
$ node examples/reflect.js -n123 -m456
{ _: [], n: 123, m: 456, '$0': 'examples/reflect.js' }
installation
============
With [npm](https://github.com/npm/npm), just do:
npm install yargs
or clone this project on github:
git clone http://github.com/yargs/yargs.git
To run the tests with npm, just do:
npm test
configuration
=============
Using the `yargs` stanza in your `package.json` you can turn on and off
some of yargs' parsing features:
```json
{
"yargs": {
"short-option-groups": true,
"camel-case-expansion": true,
"dot-notation": true,
"parse-numbers": true,
"boolean-negation": true
}
}
```
See the [yargs-parser](https://github.com/yargs/yargs-parser#configuration) module
for detailed documentation of this feature.
inspired by
===========
This module is loosely inspired by Perl's
[Getopt::Casual](http://search.cpan.org/~photo/Getopt-Casual-0.13.1/Casual.pm).
[travis-url]: https://travis-ci.org/yargs/yargs
[travis-image]: https://img.shields.io/travis/yargs/yargs/master.svg
[coveralls-url]: https://coveralls.io/github/yargs/yargs
[coveralls-image]: https://img.shields.io/coveralls/yargs/yargs.svg
[npm-url]: https://www.npmjs.com/package/yargs
[npm-image]: https://img.shields.io/npm/v/yargs.svg
[windows-url]: https://ci.appveyor.com/project/bcoe/yargs-ljwvf
[windows-image]: https://img.shields.io/appveyor/ci/bcoe/yargs-ljwvf/master.svg?label=Windows%20Tests
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
[standard-url]: http://standardjs.com/
[standard-version-image]: https://img.shields.io/badge/release-standard%20version-brightgreen.svg
[standard-version-url]: https://github.com/conventional-changelog/standard-version
[gitter-image]: https://img.shields.io/gitter/room/nwjs/nw.js.svg?maxAge=2592000
[gitter-url]: https://gitter.im/yargs/Lobby?utm_source=share-link&utm_medium=link&utm_campaign=share-link
|