Skip to content

Commit

Permalink
Improve '--' handling.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
m1kc authored and James Halliday committed Sep 19, 2013
1 parent 545b9a6 commit b11822c
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions index.js
Expand Up @@ -28,6 +28,13 @@ module.exports = function (args, opts) {
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) {
var value = !flags.strings[key] && isNumber(val)
? Number(val) : val
Expand All @@ -42,11 +49,7 @@ module.exports = function (args, opts) {
for (var i = 0; i < args.length; i++) {
var arg = args[i];

if (arg === '--') {
argv._.push.apply(argv._, args.slice(i + 1));
break;
}
else if (arg.match(/^--.+=/)) {
if (arg.match(/^--.+=/)) {
// Using [\s\S] instead of . because js doesn't support the
// 'dotall' regex modifier. See:
// http://stackoverflow.com/a/1068308/13216
Expand Down Expand Up @@ -105,10 +108,7 @@ module.exports = function (args, opts) {

var key = arg.slice(-1)[0];
if (!broken && key !== '-') {
if (args[i+1] === '--') {
setArg(key, true);
}
else if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
&& !flags.bools[key]
&& (aliases[key] ? !flags.bools[aliases[key]] : true)) {
setArg(key, args[i+1]);
Expand Down Expand Up @@ -140,6 +140,10 @@ module.exports = function (args, opts) {
}
});

notFlags.forEach(function(key) {
argv._.push(key);
});

return argv;
};

Expand Down

0 comments on commit b11822c

Please sign in to comment.