MCPcopy Create free account
hub / github.com/anus-dev/ANUS / parseArgs

Method parseArgs

packages/cli/src/services/McpPromptLoader.ts:173–231  ·  view source on GitHub ↗
(
    userArgs: string,
    promptArgs: PromptArgument[] | undefined,
  )

Source from the content-addressed store, hash-verified

171 }
172
173 private parseArgs(
174 userArgs: string,
175 promptArgs: PromptArgument[] | undefined,
176 ): Record<string, unknown> | Error {
177 const argValues: { [key: string]: string } = {};
178 const promptInputs: Record<string, unknown> = {};
179
180 // arg parsing: --key="value" or --key=value
181 const namedArgRegex = /--([^=]+)=(?:"((?:\\.|[^"\\])*)"|([^ ]*))/g;
182 let match;
183 const remainingArgs: string[] = [];
184 let lastIndex = 0;
185
186 while ((match = namedArgRegex.exec(userArgs)) !== null) {
187 const key = match[1];
188 const value = match[2] ?? match[3]; // Quoted or unquoted value
189 argValues[key] = value;
190 // Capture text between matches as potential positional args
191 if (match.index > lastIndex) {
192 remainingArgs.push(userArgs.substring(lastIndex, match.index).trim());
193 }
194 lastIndex = namedArgRegex.lastIndex;
195 }
196
197 // Capture any remaining text after the last named arg
198 if (lastIndex < userArgs.length) {
199 remainingArgs.push(userArgs.substring(lastIndex).trim());
200 }
201
202 const positionalArgs = remainingArgs.join(' ').split(/ +/);
203
204 if (!promptArgs) {
205 return promptInputs;
206 }
207 for (const arg of promptArgs) {
208 if (argValues[arg.name]) {
209 promptInputs[arg.name] = argValues[arg.name];
210 }
211 }
212
213 const unfilledArgs = promptArgs.filter(
214 (arg) => arg.required && !promptInputs[arg.name],
215 );
216
217 const missingArgs: string[] = [];
218 for (let i = 0; i < unfilledArgs.length; i++) {
219 if (positionalArgs.length > i && positionalArgs[i]) {
220 promptInputs[unfilledArgs[i].name] = positionalArgs[i];
221 } else {
222 missingArgs.push(unfilledArgs[i].name);
223 }
224 }
225
226 if (missingArgs.length > 0) {
227 const missingArgNames = missingArgs.map((name) => `--${name}`).join(', ');
228 return new Error(`Missing required argument(s): ${missingArgNames}`);
229 }
230 return promptInputs;

Callers 1

loadCommandsMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected