MCPcopy Create free account
hub / github.com/OpenHands/OpenHands / parseCommand

Function parseCommand

src/utils/acp-command.ts:65–148  ·  view source on GitHub ↗
(value: string)

Source from the content-addressed store, hash-verified

63 * either way.
64 */
65export function parseCommand(value: string): string[] {
66 const tokens: string[] = [];
67 let i = 0;
68 let current = "";
69 let inToken = false;
70
71 while (i < value.length) {
72 const ch = value[i];
73
74 if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") {
75 if (inToken) {
76 tokens.push(current);
77 current = "";
78 inToken = false;
79 }
80 i += 1;
81 continue;
82 }
83
84 if (ch === "'") {
85 // Single-quoted segment: literal until the next single quote.
86 // No escapes inside (POSIX shell semantics).
87 inToken = true;
88 i += 1;
89 while (i < value.length && value[i] !== "'") {
90 current += value[i];
91 i += 1;
92 }
93 // Skip the closing quote if present. Unterminated → EOF closes.
94 if (i < value.length) i += 1;
95 continue;
96 }
97
98 if (ch === '"') {
99 // Double-quoted segment: literal with backslash escapes for
100 // ``\\"`` and ``\\\\``. We intentionally do NOT expand $VAR
101 // (so a user typing ``"--key=$X"`` keeps it literal — the
102 // agent-server doesn't run a shell anyway). Other backslash
103 // sequences pass through verbatim (matches what most users
104 // expect when copying paths with backslashes from Windows
105 // examples; corner-case differences from POSIX aren't worth
106 // the complexity here).
107 inToken = true;
108 i += 1;
109 while (i < value.length && value[i] !== '"') {
110 if (value[i] === "\\" && i + 1 < value.length) {
111 const next = value[i + 1];
112 if (next === '"' || next === "\\") {
113 current += next;
114 i += 2;
115 continue;
116 }
117 }
118 current += value[i];
119 i += 1;
120 }
121 if (i < value.length) i += 1;
122 continue;

Callers 4

toAgentSettingsOverrideFunction · 0.90
detectPresetFunction · 0.90
AgentSettingsScreenFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected