/** * POSIX-like shell word splitting that respects quoted substrings. * Handles: simple words, "double quoted", 'single quoted', and * --flag="value with spaces" correctly. */ export function splitCommand(command: string): string[] { const tokens: string[] = []; let current = ""; let inSingleQuote = false; let inDoubleQuote = false; for (let i = 0; i < command.length; i++) { const ch = command[i]; if (inSingleQuote) { if (ch === "'") { inSingleQuote = false; } else { current += ch; } continue; } if (inDoubleQuote) { if (ch === '"') { inDoubleQuote = false; } else { current += ch; } continue; } if (ch === "'") { inSingleQuote = true; continue; } if (ch === '"') { inDoubleQuote = true; continue; } if (ch === " " || ch === "\t") { if (current.length > 0) { tokens.push(current); current = ""; } continue; } current += ch; } if (current.length > 0) { tokens.push(current); } return tokens; }