Support --version and strict CLI port validation

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-09 22:30:47 +00:00
1 parent b53656c055
commit b06d25073e
3 files changed
+45 -5

No files matched your search

+1
View File
@@ -26,6 +26,7 @@ CLI flags:
- `--backend-port <port>` — set WebSocket runtime port.
- `--web-port <port>` — set web UI port.
- `--cwd <path>` — choose launch project directory (defaults to current directory).
- `--version` — print CLI version.
- `--help` — print CLI usage.
Optional environment variables:
+11
View File
@@ -46,6 +46,17 @@ describe("parseCliOptions", () => {
expect(options.showHelp).toBe(true);
});
it("supports version flag", () => {
const options = parseCliOptions(["--version"], {}, "/workspace");
expect(options.showVersion).toBe(true);
});
it("throws for invalid explicit port values", () => {
expect(() => parseCliOptions(["--web-port", "nope"], {}, "/workspace")).toThrow(
"Invalid value for --web-port",
);
});
it("throws for unknown arguments", () => {
expect(() => parseCliOptions(["--wat"], {}, "/workspace")).toThrow(
"Unknown argument: --wat",
+33 -5
View File
@@ -23,12 +23,22 @@ function parsePort(value: string | undefined, fallback: number): number {
return parsed;
}
function parseExplicitPort(value: string, key: string): number {
const parsed = Number(value);
if (!Number.isInteger(parsed) || parsed <= 0) {
throw new Error(`Invalid value for ${key}: '${value}'. Expected a positive integer.`);
}
return parsed;
}
interface CliOptions {
backendPort: number;
webPort: number;
launchCwd: string;
noOpen: boolean;
showHelp: boolean;
showVersion: boolean;
}
function readArgValue(args: string[], index: number, key: string): string {
@@ -50,6 +60,7 @@ export function parseCliOptions(
let launchCwd = cwd;
let noOpen = env.T3_NO_OPEN === "1";
let showHelp = false;
let showVersion = false;
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
@@ -60,32 +71,37 @@ export function parseCliOptions(
continue;
}
if (arg === "--version" || arg === "-v") {
showVersion = true;
continue;
}
if (arg === "--no-open") {
noOpen = true;
continue;
}
if (arg.startsWith("--backend-port=")) {
backendPort = parsePort(arg.split("=")[1], DEFAULT_BACKEND_PORT);
backendPort = parseExplicitPort(arg.split("=")[1] ?? "", "--backend-port");
continue;
}
if (arg === "--backend-port") {
backendPort = parsePort(
backendPort = parseExplicitPort(
readArgValue(argv, index, "--backend-port"),
DEFAULT_BACKEND_PORT,
"--backend-port",
);
index += 1;
continue;
}
if (arg.startsWith("--web-port=")) {
webPort = parsePort(arg.split("=")[1], DEFAULT_WEB_PORT);
webPort = parseExplicitPort(arg.split("=")[1] ?? "", "--web-port");
continue;
}
if (arg === "--web-port") {
webPort = parsePort(readArgValue(argv, index, "--web-port"), DEFAULT_WEB_PORT);
webPort = parseExplicitPort(readArgValue(argv, index, "--web-port"), "--web-port");
index += 1;
continue;
}
@@ -110,6 +126,7 @@ export function parseCliOptions(
launchCwd,
noOpen,
showHelp,
showVersion,
};
}
@@ -123,6 +140,7 @@ function printHelp(): void {
" --backend-port <port> Override WebSocket API port (default: 4317)",
" --web-port <port> Override web UI port (default: 4318)",
" --cwd <path> Launch project directory (default: current directory)",
" -v, --version Print CLI version",
" -h, --help Show this help message",
"",
"Environment variables:",
@@ -153,6 +171,10 @@ function openBrowser(url: string, noOpen: boolean): void {
child.unref();
}
function readCliVersion(): string {
return process.env.npm_package_version ?? "0.1.0";
}
function ensureRendererBuild(rendererRoot: string): void {
const distPath = path.join(rendererRoot, "dist", "index.html");
if (fs.existsSync(distPath)) {
@@ -247,6 +269,12 @@ async function main() {
return;
}
if (options.showVersion) {
process.stdout.write(`${readCliVersion()}\n`);
process.exit(0);
return;
}
const runtimeServer = await startRuntimeApiServer({
port: options.backendPort,
launchCwd: options.launchCwd,