MCPcopy
hub / github.com/callstack/agent-device / runCli

Function runCli

src/cli.ts:82–449  ·  view source on GitHub ↗
(argv: string[], deps: CliDeps = DEFAULT_CLI_DEPS)

Source from the content-addressed store, hash-verified

80]);
81
82export async function runCli(argv: string[], deps: CliDeps = DEFAULT_CLI_DEPS): Promise<void> {
83 const requestId = createRequestId();
84 const version = readVersion();
85 const debugEnabled = isDebugRequested(argv);
86 const jsonRequested = argv.includes('--json');
87 // Best-effort session guess used only for pre-parse diagnostics scope.
88 // After parse succeeds, request dispatch uses parsed flags/session resolution.
89 const sessionGuess = guessSessionFromArgv(argv) ?? process.env.AGENT_DEVICE_SESSION ?? 'default';
90
91 await withDiagnosticsScope(
92 {
93 session: sessionGuess,
94 requestId,
95 command: argv[0],
96 debug: debugEnabled,
97 },
98 async () => {
99 let parsed: ReturnType<typeof resolveCliOptions>;
100 try {
101 parsed = resolveCliOptions(argv, { cwd: process.cwd(), env: process.env });
102 } catch (error) {
103 emitDiagnostic({
104 level: 'error',
105 phase: 'cli_parse_failed',
106 data: {
107 error: error instanceof Error ? error.message : String(error),
108 },
109 });
110 const normalized = normalizeError(error, {
111 diagnosticId: getDiagnosticsMeta().diagnosticId,
112 logPath: flushDiagnosticsToSessionFile({ force: true }) ?? undefined,
113 });
114 if (jsonRequested) {
115 printJson({ success: false, error: normalized });
116 } else {
117 printHumanError(normalized, { showDetails: debugEnabled });
118 }
119 process.exit(1);
120 return;
121 }
122
123 for (const warning of parsed.warnings) {
124 process.stderr.write(`Warning: ${warning}\n`);
125 }
126
127 if (parsed.flags.version) {
128 process.stdout.write(`${version}\n`);
129 process.exit(0);
130 }
131
132 const isHelpAlias = parsed.command === 'help';
133 const isHelpFlag = parsed.flags.help;
134 if (isHelpAlias || isHelpFlag) {
135 if (isHelpAlias && parsed.positionals.length > 1) {
136 printHumanError(new AppError('INVALID_ARGS', 'help accepts at most one command.'));
137 process.exit(1);
138 }
139 const helpTarget = isHelpAlias ? parsed.positionals[0] : parsed.command;

Callers 1

cli.tsFile · 0.70

Calls 15

createRequestIdFunction · 0.90
readVersionFunction · 0.90
withDiagnosticsScopeFunction · 0.90
resolveCliOptionsFunction · 0.90
emitDiagnosticFunction · 0.90
normalizeErrorFunction · 0.90
getDiagnosticsMetaFunction · 0.90
printJsonFunction · 0.90
printHumanErrorFunction · 0.90
usageFunction · 0.90
usageForCommandFunction · 0.90

Tested by

no test coverage detected