yargs 2.1.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,10 +8,9 @@ With yargs, ye be havin' a map that leads straight to yer treasure! Treasure of
8
8
  [![Build Status](https://travis-ci.org/chevex/yargs.png)](https://travis-ci.org/chevex/yargs)
9
9
  [![Dependency Status](https://gemnasium.com/chevex/yargs.png)](https://gemnasium.com/chevex/yargs)
10
10
  [![NPM version](https://badge.fury.io/js/yargs.png)](http://badge.fury.io/js/yargs)
11
+ [![Coverage Status](https://coveralls.io/repos/chevex/yargs/badge.svg?branch=)](https://coveralls.io/r/chevex/yargs?branch=)
11
12
 
12
- > ~~NOTE: Yargs is a fork of [optimist](https://github.com/substack/node-optimist) by [substack (James Halliday)](https://github.com/substack). It is obvious that substack is stretched pretty thin maintaining over 300 modules on npm at the time of this writing. So rather than complain in the project issue tracker I thought I'd just pick up the torch and maintain a proper fork. Currently the project is totally backward compatible with optimist but this may change in the future (if it does I will update this notice to inform you of this). For now though, enjoy optimist with about 5 months worth of fixes and updates rolled in, most of them pulled from optimist's own [stale pull requests](https://github.com/substack/node-optimist/pulls).~~
13
-
14
- > UPDATE: Yargs is now the official successor to optimist. Please feel free to submit issues and pull requests. While I personally don't have the time to pore over all the issues and fix all of them on a regular basis, I'm more than happy to look over pull requests, test them, and merge them in. If you'd like to contribute and don't know where to start, have a look at [the issue list](https://github.com/chevex/yargs/issues) :)
13
+ > 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/chevex/yargs/issues) :)
15
14
 
16
15
  examples
17
16
  ========
@@ -37,7 +36,7 @@ else {
37
36
 
38
37
  $ ./xup.js --rif=55 --xup=9.52
39
38
  Plunder more riffiwobbles!
40
-
39
+
41
40
  $ ./xup.js --rif 12 --xup 8.1
42
41
  Drop the xupptumblers!
43
42
 
@@ -45,7 +44,7 @@ else {
45
44
 
46
45
  But don't walk the plank just yet! There be more! You can do short options:
47
46
  -------------------------------------------------
48
-
47
+
49
48
  short.js:
50
49
 
51
50
  ````javascript
@@ -81,7 +80,7 @@ console.log(
81
80
 
82
81
  $ ./bool.js -s
83
82
  The parrot says: squawk
84
-
83
+
85
84
  $ ./bool.js -sp
86
85
  The parrot says: squawk!
87
86
 
@@ -90,7 +89,7 @@ console.log(
90
89
 
91
90
  And non-hyphenated options too! Just use `argv._`!
92
91
  -------------------------------------------------
93
-
92
+
94
93
  nonopt.js:
95
94
 
96
95
  ````javascript
@@ -105,7 +104,7 @@ console.log(argv._);
105
104
  $ ./nonopt.js -x 6.82 -y 3.35 rum
106
105
  (6.82,3.35)
107
106
  [ 'rum' ]
108
-
107
+
109
108
  $ ./nonopt.js "me hearties" -x 0.54 yo -y 1.12 ho
110
109
  (0.54,1.12)
111
110
  [ 'me hearties', 'yo', 'ho' ]
@@ -167,10 +166,10 @@ console.log(argv.x / argv.y);
167
166
  ````
168
167
 
169
168
  ***
170
-
169
+
171
170
  $ ./divide.js -x 55 -y 11
172
171
  5
173
-
172
+
174
173
  $ node ./divide.js -x 4.91 -z 2.51
175
174
  Usage: node ./divide.js -x [num] -y [num]
176
175
 
@@ -258,7 +257,7 @@ console.dir(argv._);
258
257
  $ ./boolean_single.js -v "me hearties" yo ho
259
258
  true
260
259
  [ 'me hearties', 'yo', 'ho' ]
261
-
260
+
262
261
 
263
262
  boolean_double.js
264
263
 
@@ -324,10 +323,10 @@ s.on('end', function () {
324
323
 
325
324
  Missing required arguments: f
326
325
 
327
- $ node line_count.js --file line_count.js
326
+ $ node line_count.js --file line_count.js
328
327
  20
329
-
330
- $ node line_count.js -f line_count.js
328
+
329
+ $ node line_count.js -f line_count.js
331
330
  20
332
331
 
333
332
  methods
@@ -403,6 +402,13 @@ usage information and exit.
403
402
  The default behaviour is to set the value of any key not followed by an
404
403
  option value to `true`.
405
404
 
405
+ .implies(x, y)
406
+ --------------
407
+
408
+ Given the key `x` is set, it is required that the key `y` is set.
409
+
410
+ implies can also accept an object specifying multiple implications.
411
+
406
412
  .describe(key, desc)
407
413
  --------------------
408
414
 
@@ -415,7 +421,7 @@ Optionally `.describe()` can take an object that maps keys to descriptions.
415
421
  .options(key, opt)
416
422
  ------------------
417
423
 
418
- Instead of chaining together `.alias().demand().default()`, you can specify
424
+ Instead of chaining together `.alias().demand().default().describe().string()`, you can specify
419
425
  keys in `opt` for each of the chainable methods.
420
426
 
421
427
  For example:
@@ -424,7 +430,10 @@ For example:
424
430
  var argv = require('yargs')
425
431
  .option('f', {
426
432
  alias : 'file',
427
- default : '/etc/passwd',
433
+ demand: true,
434
+ default: '/etc/passwd',
435
+ describe: 'x marks the spot',
436
+ type: 'string'
428
437
  })
429
438
  .argv
430
439
  ;
@@ -442,6 +451,21 @@ var argv = require('yargs')
442
451
 
443
452
  Optionally `.options()` can take an object that maps keys to `opt` parameters.
444
453
 
454
+ ````javascript
455
+ var argv = require('yargs')
456
+ .options({
457
+ 'f': {
458
+ alias: 'file',
459
+ demand: true,
460
+ default: '/etc/passwd',
461
+ describe: 'x marks the spot',
462
+ type: 'string'
463
+ }
464
+ })
465
+ .argv
466
+ ;
467
+ ````
468
+
445
469
  .usage(message, opts)
446
470
  ---------------------
447
471
 
@@ -469,14 +493,21 @@ Check that certain conditions are met in the provided arguments.
469
493
  If `fn` throws or returns `false`, show the thrown error, usage information, and
470
494
  exit.
471
495
 
496
+ .fail(fn)
497
+ ---------
498
+
499
+ Method to execute when a failure occurs, rather then printing the failure message.
500
+
501
+ `fn` is called with the failure message that would have been printed.
502
+
472
503
  .boolean(key)
473
504
  -------------
474
505
 
475
506
  Interpret `key` as a boolean. If a non-flag option follows `key` in
476
507
  `process.argv`, that string won't get set as the value of `key`.
477
508
 
478
- If `key` never shows up as a flag in `process.arguments`, `argv[key]` will be
479
- `false`.
509
+ `key` will default to `false`, unless an `default(key, undefined)` is
510
+ explicitly set.
480
511
 
481
512
  If `key` is an Array, interpret all the elements as booleans.
482
513
 
@@ -488,6 +519,9 @@ This can be useful if you need to preserve leading zeros in an input.
488
519
 
489
520
  If `key` is an Array, interpret all the elements as strings.
490
521
 
522
+ `.string('_')` will result in non-hyphenated arguments being interpreted as strings,
523
+ regardless of whether they resemble numbers.
524
+
491
525
  .config(key)
492
526
  ------------
493
527
 
@@ -671,17 +705,17 @@ installation
671
705
  With [npm](http://github.com/isaacs/npm), just do:
672
706
 
673
707
  npm install yargs
674
-
708
+
675
709
  or clone this project on github:
676
710
 
677
711
  git clone http://github.com/chevex/yargs.git
678
712
 
679
713
  To run the tests with [expresso](http://github.com/visionmedia/expresso),
680
714
  just do:
681
-
715
+
682
716
  expresso
683
717
 
684
- inspired By
718
+ inspired by
685
719
  ===========
686
720
 
687
721
  This module is loosely inspired by Perl's
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  var path = require('path');
2
- var minimist = require('./lib/minimist');
3
- var wordwrap = require('./lib/wordwrap');
2
+ var parser = require('./lib/parser');
3
+ var wordwrap = require('wordwrap');
4
4
 
5
5
  /* Hack an instance of Argv with process.argv into Argv
6
6
  so people can do
@@ -189,7 +189,7 @@ function Argv (processArgs, cwd) {
189
189
  if (exitProcess){
190
190
  process.exit(1);
191
191
  }else{
192
- throw new Error(msg);
192
+ throw new Error(msg);
193
193
  }
194
194
  }
195
195
  }
@@ -233,7 +233,7 @@ function Argv (processArgs, cwd) {
233
233
  self.demand(key, demand);
234
234
  }
235
235
 
236
- if (typeof opt.default !== 'undefined') {
236
+ if ('default' in opt) {
237
237
  self.default(key, opt.default);
238
238
  }
239
239
 
@@ -300,8 +300,8 @@ function Argv (processArgs, cwd) {
300
300
  var showHelpOnFail = true;
301
301
  self.showHelpOnFail = function (enabled, message) {
302
302
  if (typeof enabled === 'string') {
303
- enabled = true;
304
303
  message = enabled;
304
+ enabled = true;
305
305
  }
306
306
  else if (typeof enabled === 'undefined') {
307
307
  enabled = true;
@@ -440,13 +440,17 @@ function Argv (processArgs, cwd) {
440
440
  var dlen = dlines.slice(-1)[0].length
441
441
  + (dlines.length === 1 ? prelude.length : 0)
442
442
 
443
- body = desc + (dlen + extra.length > wrap - 2
444
- ? '\n'
445
- + new Array(wrap - extra.length + 1).join(' ')
446
- + extra
447
- : new Array(wrap - extra.length - dlen + 1).join(' ')
448
- + extra
449
- );
443
+ if (extra.length > wrap) {
444
+ body = desc + '\n' + wordwrap(switchlen + 4, wrap)(extra)
445
+ } else {
446
+ body = desc + (dlen + extra.length > wrap - 2
447
+ ? '\n'
448
+ + new Array(wrap - extra.length + 1).join(' ')
449
+ + extra
450
+ : new Array(wrap - extra.length - dlen + 1).join(' ')
451
+ + extra
452
+ );
453
+ }
450
454
  }
451
455
 
452
456
  help.push(prelude + body);
@@ -457,12 +461,22 @@ function Argv (processArgs, cwd) {
457
461
  };
458
462
 
459
463
  Object.defineProperty(self, 'argv', {
460
- get : function () { return parseArgs(processArgs) },
464
+ get : function () {
465
+ var args = null;
466
+
467
+ try {
468
+ args = parseArgs(processArgs);
469
+ } catch (err) {
470
+ fail(err.message);
471
+ }
472
+
473
+ return args;
474
+ },
461
475
  enumerable : true
462
476
  });
463
477
 
464
478
  function parseArgs (args) {
465
- var parsed = minimist(args, options),
479
+ var parsed = parser(args, options),
466
480
  argv = parsed.argv,
467
481
  aliases = parsed.aliases;
468
482
 
@@ -501,7 +515,7 @@ function Argv (processArgs, cwd) {
501
515
  options.requiresArg.forEach(function(key) {
502
516
  var value = argv[key];
503
517
 
504
- // minimist sets --foo value to true / --no-foo to false
518
+ // parser sets --foo value to true / --no-foo to false
505
519
  if (value === true || value === false) {
506
520
  missingRequiredArgs.push(key);
507
521
  }
@@ -643,14 +657,5 @@ function Argv (processArgs, cwd) {
643
657
  // exported for tests
644
658
  exports.rebase = rebase;
645
659
  function rebase (base, dir) {
646
- var ds = path.normalize(dir).split('/').slice(1);
647
- var bs = path.normalize(base).split('/').slice(1);
648
-
649
- for (var i = 0; ds[i] && ds[i] == bs[i]; i++);
650
- ds.splice(0, i); bs.splice(0, i);
651
-
652
- var p = path.normalize(
653
- bs.map(function () { return '..' }).concat(ds).join('/')
654
- ).replace(/\/$/,'').replace(/^$/, '.');
655
- return p.match(/^[.\/]/) ? p : './' + p;
660
+ return path.relative(base, dir);
656
661
  };
@@ -1,3 +1,5 @@
1
+ // fancy-pants parsing of argv, originally forked
2
+ // from minimist: https://www.npmjs.com/package/minimist
1
3
  var path = require('path');
2
4
  var fs = require('fs');
3
5
 
@@ -71,7 +73,7 @@ module.exports = function (args, opts) {
71
73
 
72
74
  var argv = { _ : [] };
73
75
  Object.keys(flags.bools).forEach(function (key) {
74
- setArg(key, defaults[key] === undefined ? false : defaults[key]);
76
+ setArg(key, !(key in defaults) ? false : defaults[key]);
75
77
  });
76
78
 
77
79
  var notFlags = [];
@@ -145,8 +147,7 @@ module.exports = function (args, opts) {
145
147
  }
146
148
  });
147
149
  } catch (ex) {
148
- console.error('Invalid JSON config file: ' + configPath);
149
- throw ex;
150
+ throw Error('invalid json config file: ' + configPath);
150
151
  }
151
152
  }
152
153
  });
@@ -328,7 +329,3 @@ function isNumber (x) {
328
329
  if (/^0x[0-9a-f]+$/i.test(x)) return true;
329
330
  return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
330
331
  }
331
-
332
- function longest (xs) {
333
- return Math.max.apply(null, xs.map(function (x) { return x.length }));
334
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yargs",
3
- "version": "2.1.1",
3
+ "version": "2.3.0",
4
4
  "description": "Light-weight option parsing with an argv hash. No optstrings attached.",
5
5
  "main": "./index.js",
6
6
  "files": [
@@ -8,12 +8,16 @@
8
8
  "lib",
9
9
  "LICENSE"
10
10
  ],
11
- "dependencies": {},
11
+ "dependencies": {
12
+ "wordwrap": "0.0.2"
13
+ },
12
14
  "devDependencies": {
13
15
  "blanket": "^1.1.6",
14
16
  "chai": "^1.10.0",
17
+ "coveralls": "^2.11.2",
15
18
  "hashish": "0.0.4",
16
19
  "mocha": "^2.1.0",
20
+ "mocha-lcov-reporter": "0.0.1",
17
21
  "mocoverage": "^1.0.0"
18
22
  },
19
23
  "scripts": {
package/lib/wordwrap.js DELETED
@@ -1,50 +0,0 @@
1
- // Simplified version of https://github.com/substack/node-wordwrap
2
-
3
- 'use strict';
4
-
5
- module.exports = function (start, stop) {
6
-
7
- if (!stop) {
8
- stop = start;
9
- start = 0;
10
- }
11
-
12
- var re = /(\S+\s+)/;
13
-
14
- return function (text) {
15
- var chunks = text.toString().split(re);
16
-
17
- return chunks.reduce(function (lines, rawChunk) {
18
- if (rawChunk === '') return lines;
19
-
20
- var chunk = rawChunk.replace(/\t/g, ' ');
21
-
22
- var i = lines.length - 1;
23
- if (lines[i].length + chunk.length > stop) {
24
- lines[i] = lines[i].replace(/\s+$/, '');
25
-
26
- chunk.split(/\n/).forEach(function (c) {
27
- lines.push(
28
- new Array(start + 1).join(' ')
29
- + c.replace(/^\s+/, '')
30
- );
31
- });
32
- }
33
- else if (chunk.match(/\n/)) {
34
- var xs = chunk.split(/\n/);
35
- lines[i] += xs.shift();
36
- xs.forEach(function (c) {
37
- lines.push(
38
- new Array(start + 1).join(' ')
39
- + c.replace(/^\s+/, '')
40
- );
41
- });
42
- }
43
- else {
44
- lines[i] += chunk;
45
- }
46
-
47
- return lines;
48
- }, [ new Array(start + 1).join(' ') ]).join('\n');
49
- };
50
- };