-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandline.js
43 lines (43 loc) · 1.16 KB
/
commandline.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
36
37
38
39
40
41
42
43
module.exports = {
parse: function(cmd, index) {
var currentArg;
var virtualCurrentArg;
var args = [];
var argsmap = [];
var quote = false;
var curr = 0, start = 0;
for (;curr < cmd.length; curr++) {
switch (cmd[curr]) {
case '"': quote = !quote; break;
case ' ': if (!quote) {
if (start != curr) {
if (index >= start && index <= curr) currentArg = args.length;
if (curr < index) virtualCurrentArg = args.length + 1;
args.push(cmd.slice(start, curr));
argsmap.push([start, curr]);
}
start = curr + 1;
}
}
}
if (start != curr) {
if (index >= start && index <= curr) currentArg = args.length;
args.push(cmd.slice(start, curr));
argsmap.push([start, curr]);
}
var arg0 = "", argr = "";
if (args.length > 0) {
arg0 = args[0];
argr = args.length == 2 ? args[1] : cmd.slice(arg0.length).trim();
}
return {
raw: cmd,
arg0: arg0,
argr: argr,
args: args,
argsmap: argsmap,
current: currentArg,
virtualCurrent: currentArg || virtualCurrentArg
};
}
};