-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
35 lines (29 loc) · 1.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var minimist = require('minimist');
module.exports = function parse (args, opts) {
var level = 0, index;
var args_ = [];
for (var i = 0; i < args.length; i++) {
if (typeof args[i] === 'string' && /^\[/.test(args[i])) {
if (level ++ === 0) {
index = i;
}
}
if (typeof args[i] === 'string' && /\]$/.test(args[i])) {
if (-- level > 0) continue;
var sub = args.slice(index, i + 1);
if (typeof sub[0] === 'string') {
sub[0] = sub[0].replace(/^\[/, '');
}
if (sub[0] === '') sub.shift();
var n = sub.length - 1;
if (typeof sub[n] === 'string') {
sub[n] = sub[n].replace(/\]$/, '');
}
if (sub[n] === '') sub.pop();
args_.push(parse(sub));
}
else if (level === 0) args_.push(args[i]);
}
var argv = minimist(args_, opts);
return argv;
};