1 |
3a515b92
|
cagy
|
// take an un-split argv string and tokenize it.
|
2 |
|
|
module.exports = function (argString) {
|
3 |
|
|
if (Array.isArray(argString)) return argString
|
4 |
|
|
|
5 |
|
|
argString = argString.trim()
|
6 |
|
|
|
7 |
|
|
var i = 0
|
8 |
|
|
var prevC = null
|
9 |
|
|
var c = null
|
10 |
|
|
var opening = null
|
11 |
|
|
var args = []
|
12 |
|
|
|
13 |
|
|
for (var ii = 0; ii < argString.length; ii++) {
|
14 |
|
|
prevC = c
|
15 |
|
|
c = argString.charAt(ii)
|
16 |
|
|
|
17 |
|
|
// split on spaces unless we're in quotes.
|
18 |
|
|
if (c === ' ' && !opening) {
|
19 |
|
|
if (!(prevC === ' ')) {
|
20 |
|
|
i++
|
21 |
|
|
}
|
22 |
|
|
continue
|
23 |
|
|
}
|
24 |
|
|
|
25 |
|
|
// don't split the string if we're in matching
|
26 |
|
|
// opening or closing single and double quotes.
|
27 |
|
|
if (c === opening) {
|
28 |
|
|
if (!args[i]) args[i] = ''
|
29 |
|
|
opening = null
|
30 |
|
|
continue
|
31 |
|
|
} else if ((c === "'" || c === '"') && !opening) {
|
32 |
|
|
opening = c
|
33 |
|
|
continue
|
34 |
|
|
}
|
35 |
|
|
|
36 |
|
|
if (!args[i]) args[i] = ''
|
37 |
|
|
args[i] += c
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
return args
|
41 |
|
|
}
|