Skip to content

Commit 60aad9f

Browse files
0disoftlidge-jun
authored andcommitted
guard subcommand help before side effects
1 parent 9190d59 commit 60aad9f

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

‎src/cli.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,69 @@ Examples:
4141
ocx sync Sync available models to Codex`);
4242
}
4343

44+
function hasHelpFlag(values: string[]): boolean {
45+
return values.some(value => value === "--help" || value === "-h" || value === "help");
46+
}
47+
48+
function printSubcommandUsage(name: string | undefined): void {
49+
switch (name) {
50+
case "init":
51+
console.log("Usage: ocx init\n\nInteractive setup for providers and Codex config injection.");
52+
break;
53+
case "start":
54+
console.log("Usage: ocx start [--port <port>]\n\nStart the proxy server and sync models to Codex.");
55+
break;
56+
case "stop":
57+
console.log("Usage: ocx stop\n\nStop the proxy and restore native Codex config.");
58+
break;
59+
case "restore":
60+
case "eject":
61+
console.log(`Usage: ocx ${name}\n\nRestore native Codex config without stopping the proxy.`);
62+
break;
63+
case "uninstall":
64+
case "remove":
65+
console.log(`Usage: ocx ${name}\n\nRemove service/shim/config and restore native Codex.`);
66+
break;
67+
case "service":
68+
console.log("Usage: ocx service <install|start|stop|status|uninstall>");
69+
break;
70+
case "codex-shim":
71+
console.log("Usage: ocx codex-shim <install|status|uninstall>");
72+
break;
73+
case "ensure":
74+
console.log("Usage: ocx ensure\n\nEnsure the proxy is running and Codex config/cache are current.");
75+
break;
76+
case "sync":
77+
console.log("Usage: ocx sync\n\nFetch provider models and inject them into Codex config.");
78+
break;
79+
case "sync-cache":
80+
console.log("Usage: ocx sync-cache\n\nRefresh Codex's model cache from the active catalog.");
81+
break;
82+
case "status":
83+
console.log("Usage: ocx status\n\nCheck proxy server status.");
84+
break;
85+
case "login":
86+
console.log("Usage: ocx login <provider>\n\nOAuth or API-key login for a provider.");
87+
break;
88+
case "logout":
89+
console.log("Usage: ocx logout <provider>\n\nRemove a stored provider login.");
90+
break;
91+
case "gui":
92+
console.log("Usage: ocx gui\n\nOpen the opencodex dashboard.");
93+
break;
94+
case "update":
95+
console.log("Usage: ocx update\n\nUpdate opencodex to the latest published version.");
96+
break;
97+
default:
98+
printUsage();
99+
}
100+
}
101+
102+
if (command !== undefined && command !== "help" && hasHelpFlag(args.slice(1))) {
103+
printSubcommandUsage(command);
104+
process.exit(0);
105+
}
106+
44107
async function syncModelsToCodex(port?: number) {
45108
const config = loadConfig();
46109
const p = port ?? config.port ?? 10100;

‎tests/cli-help.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { spawnSync } from "node:child_process";
3+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
4+
import { tmpdir } from "node:os";
5+
import { dirname, join } from "node:path";
6+
import { fileURLToPath } from "node:url";
7+
8+
const repoRoot = dirname(fileURLToPath(new URL("../package.json", import.meta.url)));
9+
const cliPath = join(repoRoot, "src", "cli.ts");
10+
11+
describe("CLI subcommand help", () => {
12+
test("restore --help prints usage without mutating Codex config", () => {
13+
const codexHome = mkdtempSync(join(tmpdir(), "ocx-help-"));
14+
try {
15+
const configPath = join(codexHome, "config.toml");
16+
const before = [
17+
'model_provider = "opencodex"',
18+
"",
19+
"[model_providers.opencodex]",
20+
'base_url = "http://localhost:10100/v1"',
21+
'wire_api = "responses"',
22+
"",
23+
].join("\n");
24+
writeFileSync(configPath, before, "utf8");
25+
26+
const result = spawnSync(process.execPath, [cliPath, "restore", "--help"], {
27+
cwd: repoRoot,
28+
env: { ...process.env, CODEX_HOME: codexHome },
29+
encoding: "utf8",
30+
});
31+
32+
expect(result.status).toBe(0);
33+
expect(result.stdout).toContain("Usage: ocx restore");
34+
expect(result.stdout).not.toContain("Plain `codex` now runs natively");
35+
expect(readFileSync(configPath, "utf8")).toBe(before);
36+
} finally {
37+
rmSync(codexHome, { recursive: true, force: true });
38+
}
39+
});
40+
});

0 commit comments

Comments
 (0)