If there are a double dash in the `args`, we can just throw away
following arguments and push them directly to `argv._`. This allows us
to remove any checks for `--` from the following code therefore making
it more mistake-proof.

These arguments are stored in a temporary array and pushed after
processing the rest - to ensure argument order won't change.
This commit is contained in:
m1kc (Max Musatov) 2013-09-17 20:42:02 +04:00 коммит произвёл James Halliday
Родитель 545b9a6d6d
Коммит b11822c09c
1 изменённых файлов: 13 добавлений и 9 удалений

Просмотреть файл

@ -28,6 +28,13 @@ module.exports = function (args, opts) {
setArg(key, defaults[key] === undefined ? false : defaults[key]); setArg(key, defaults[key] === undefined ? false : defaults[key]);
}); });
var notFlags = [];
if (args.indexOf('--') !== -1) {
notFlags = args.slice(args.indexOf('--')+1);
args = args.slice(0, args.indexOf('--'));
}
function setArg (key, val) { function setArg (key, val) {
var value = !flags.strings[key] && isNumber(val) var value = !flags.strings[key] && isNumber(val)
? Number(val) : val ? Number(val) : val
@ -42,11 +49,7 @@ module.exports = function (args, opts) {
for (var i = 0; i < args.length; i++) { for (var i = 0; i < args.length; i++) {
var arg = args[i]; var arg = args[i];
if (arg === '--') { if (arg.match(/^--.+=/)) {
argv._.push.apply(argv._, args.slice(i + 1));
break;
}
else if (arg.match(/^--.+=/)) {
// Using [\s\S] instead of . because js doesn't support the // Using [\s\S] instead of . because js doesn't support the
// 'dotall' regex modifier. See: // 'dotall' regex modifier. See:
// http://stackoverflow.com/a/1068308/13216 // http://stackoverflow.com/a/1068308/13216
@ -105,10 +108,7 @@ module.exports = function (args, opts) {
var key = arg.slice(-1)[0]; var key = arg.slice(-1)[0];
if (!broken && key !== '-') { if (!broken && key !== '-') {
if (args[i+1] === '--') { if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
setArg(key, true);
}
else if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
&& !flags.bools[key] && !flags.bools[key]
&& (aliases[key] ? !flags.bools[aliases[key]] : true)) { && (aliases[key] ? !flags.bools[aliases[key]] : true)) {
setArg(key, args[i+1]); setArg(key, args[i+1]);
@ -140,6 +140,10 @@ module.exports = function (args, opts) {
} }
}); });
notFlags.forEach(function(key) {
argv._.push(key);
});
return argv; return argv;
}; };