runtime-shell
Version:
Some useful shell commands for runtime.js
30 lines (28 loc) • 750 B
JavaScript
;
module.exports.parse = (args) => {
const split = args.split(' ');
const newArgs = [];
let curr_quote = '';
let in_quote = false;
for (const i of split) {
if (i === '') continue;
if (i.substr(0, 1) === '"') {
in_quote = true;
}
if (in_quote) {
if (i.substr(0, 1) === '"') {
curr_quote = i.substr(1);
} else if (i.substr(i.length-1) === '"') {
curr_quote = (i.substr(0, 1) === '"') ? curr_quote.substr(0, curr_quote.length-1) : curr_quote + ' ' + i.substr(0, i.length-1);
newArgs.push(curr_quote);
in_quote = false;
curr_quote = '';
} else {
curr_quote += ' ' + i;
}
} else {
newArgs.push(i);
}
}
return newArgs;
}