Skip to content

Commit 0b572d8

Browse files
author
James Halliday
committed
passes the recursive case
1 parent e40b050 commit 0b572d8

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

index.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
var minimist = require('minimist');
22

33
module.exports = function parse (args, opts) {
4-
var inside = false, index;
4+
var level = 0, index;
5+
var args_ = [];
6+
57
for (var i = 0; i < args.length; i++) {
6-
if (/^\[/.test(args[i])) {
7-
inside = true;
8-
index = i;
8+
if (typeof args[i] === 'string' && /^\[/.test(args[i])) {
9+
if (level ++ === 0) {
10+
index = i;
11+
}
912
}
10-
if (inside && /\]$/.test(args[i])) {
13+
if (typeof args[i] === 'string' && /\]$/.test(args[i])) {
14+
if (-- level > 0) continue;
15+
1116
var sub = args.slice(index, i + 1);
12-
sub[0] = sub[0].replace(/^\[/, '');
17+
if (typeof sub[0] === 'string') {
18+
sub[0] = sub[0].replace(/^\[/, '');
19+
}
1320
if (sub[0] === '') sub.shift();
1421

15-
sub[sub.length-1] = sub[sub.length-1].replace(/\]$/, '');
16-
if (sub[sub.length-1] === '') sub.pop();
22+
var n = sub.length - 1;
23+
if (typeof sub[n] === 'string') {
24+
sub[n] = sub[n].replace(/\]$/, '');
25+
}
26+
if (sub[n] === '') sub.pop();
1727

18-
args.splice(index, i - 1, parse(sub));
19-
inside = false;
28+
args_.push(parse(sub));
2029
}
30+
else if (level === 0) args_.push(args[i]);
2131
}
2232

23-
var argv = minimist(args, opts);
33+
var argv = minimist(args_, opts);
2434
return argv;
2535
};

0 commit comments

Comments
 (0)