Comparing version 3.3.1 to 3.4.0
15
index.js
@@ -54,2 +54,3 @@ var path = require('path'); | ||
string: [], | ||
narg: {}, | ||
key: {}, | ||
@@ -88,2 +89,13 @@ alias: {}, | ||
self.nargs = function (key, n) { | ||
if (typeof key === 'object') { | ||
Object.keys(key).forEach(function(k) { | ||
self.nargs(k, key[k]); | ||
}); | ||
} else { | ||
options.narg[key] = n; | ||
} | ||
return self; | ||
} | ||
self.normalize = function (strings) { | ||
@@ -239,2 +251,5 @@ options.normalize.push.apply(options.normalize, [].concat(strings)); | ||
} | ||
if ('nargs' in opt) { | ||
self.nargs(key, opt.nargs); | ||
} | ||
if (opt.boolean || opt.type === 'boolean') { | ||
@@ -241,0 +256,0 @@ self.boolean(key); |
@@ -9,3 +9,2 @@ // fancy-pants parsing of argv, originally forked | ||
if (!opts) opts = {}; | ||
var flags = { arrays: {}, bools : {}, strings : {}, counts: {}, normalize: {}, configs: {} }; | ||
@@ -92,15 +91,21 @@ | ||
var key = arg.match(/^--(.+)/)[1]; | ||
var next = args[i + 1]; | ||
if (next !== undefined && !next.match(/^-/) | ||
&& !checkAllAliases(key, flags.bools)) { | ||
setArg(key, next); | ||
i++; | ||
if (checkAllAliases(key, opts.narg)) { | ||
i = eatNargs(i, key, args); | ||
} else { | ||
var next = args[i + 1]; | ||
if (next !== undefined && !next.match(/^-/) | ||
&& !checkAllAliases(key, flags.bools)) { | ||
setArg(key, next); | ||
i++; | ||
} | ||
else if (/^(true|false)$/.test(next)) { | ||
setArg(key, next); | ||
i++; | ||
} | ||
else { | ||
setArg(key, defaultForType(guessType(key, flags))); | ||
} | ||
} | ||
else if (/^(true|false)$/.test(next)) { | ||
setArg(key, next); | ||
i++; | ||
} | ||
else { | ||
setArg(key, defaultForType(guessType(key, flags))); | ||
} | ||
} | ||
@@ -142,3 +147,2 @@ // dot-notation flag seperated by '='. | ||
} | ||
if (/[A-Za-z]/.test(letters[j]) | ||
@@ -150,3 +154,2 @@ && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) { | ||
} | ||
if (letters[j+1] && letters[j+1].match(/\W/)) { | ||
@@ -163,15 +166,20 @@ setArg(letters[j], arg.slice(j+2)); | ||
var key = arg.slice(-1)[0]; | ||
if (!broken && key !== '-') { | ||
if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1]) | ||
&& !checkAllAliases(key, flags.bools)) { | ||
setArg(key, args[i+1]); | ||
i++; | ||
if (checkAllAliases(key, opts.narg)) { | ||
i = eatNargs(i, key, args); | ||
} else { | ||
if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1]) | ||
&& !checkAllAliases(key, flags.bools)) { | ||
setArg(key, args[i+1]); | ||
i++; | ||
} | ||
else if (args[i+1] && /true|false/.test(args[i+1])) { | ||
setArg(key, args[i+1]); | ||
i++; | ||
} | ||
else { | ||
setArg(key, defaultForType(guessType(key, flags))); | ||
} | ||
} | ||
else if (args[i+1] && /true|false/.test(args[i+1])) { | ||
setArg(key, args[i+1]); | ||
i++; | ||
} | ||
else { | ||
setArg(key, defaultForType(guessType(key, flags))); | ||
} | ||
} | ||
@@ -197,2 +205,16 @@ } | ||
// how many arguments should we consume, based | ||
// on the nargs option? | ||
function eatNargs (i, key, args) { | ||
var toEat = checkAllAliases(key, opts.narg); | ||
if (args.length - (i + 1) < toEat) throw Error('not enough arguments following: ' + key); | ||
for (var ii = i + 1; ii < (toEat + i + 1); ii++) { | ||
setArg(key, args[ii]); | ||
} | ||
return (i + toEat); | ||
} | ||
function setArg (key, val) { | ||
@@ -353,3 +375,3 @@ // handle parsing boolean arguments --foo=true --bar false. | ||
toCheck.forEach(function(key) { | ||
if (flag[key]) isSet = true; | ||
if (flag[key]) isSet = flag[key]; | ||
}); | ||
@@ -356,0 +378,0 @@ |
{ | ||
"name": "yargs", | ||
"version": "3.3.1", | ||
"version": "3.4.0", | ||
"description": "Light-weight option parsing with an argv hash. No optstrings attached.", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -576,2 +576,20 @@ yargs | ||
.nargs(key, count) | ||
----------- | ||
The number of arguments that should be consumed after a key. This can be a | ||
useful hint to prevent parsing ambiguity: | ||
```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. | ||
.config(key) | ||
@@ -578,0 +596,0 @@ ------------ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62705
1164
842