58 lines
844 B
JavaScript
58 lines
844 B
JavaScript
function splitArgs(input) {
|
|
if (!input || typeof input !== 'string') {
|
|
return [];
|
|
}
|
|
|
|
const args = [];
|
|
let current = '';
|
|
let quote = null;
|
|
let escaping = false;
|
|
|
|
for (const ch of input) {
|
|
if (escaping) {
|
|
current += ch;
|
|
escaping = false;
|
|
continue;
|
|
}
|
|
|
|
if (ch === '\\') {
|
|
escaping = true;
|
|
continue;
|
|
}
|
|
|
|
if (quote) {
|
|
if (ch === quote) {
|
|
quote = null;
|
|
} else {
|
|
current += ch;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (ch === '"' || ch === "'") {
|
|
quote = ch;
|
|
continue;
|
|
}
|
|
|
|
if (/\s/.test(ch)) {
|
|
if (current.length > 0) {
|
|
args.push(current);
|
|
current = '';
|
|
}
|
|
continue;
|
|
}
|
|
|
|
current += ch;
|
|
}
|
|
|
|
if (current.length > 0) {
|
|
args.push(current);
|
|
}
|
|
|
|
return args;
|
|
}
|
|
|
|
module.exports = {
|
|
splitArgs
|
|
};
|