From 4fbd7374027e64498427e298a61faa2ca788e538 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Sat, 5 Sep 2026 22:13:57 +0800 Subject: [PATCH] fix(cli): support version and help aliases (v, V, -V, h, -h) --- src/cli/parser.ts | 3 ++- src/cli/runner.test.ts | 47 ++++++++++++++++++++++++++++++++++++++++++ src/cli/runner.ts | 18 ++++++++++++++-- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/cli/runner.test.ts diff --git a/src/cli/parser.ts b/src/cli/parser.ts index 05fefd9..e6f0e11 100644 --- a/src/cli/parser.ts +++ b/src/cli/parser.ts @@ -8,6 +8,7 @@ const KNOWN_BOOLEAN_FLAGS = new Set([ "h", "version", "v", + "V", "force", "f", "stateless", @@ -67,7 +68,7 @@ export function parseArgs(rawArgs: string[]): CliParsedArgs { const flag = arg.slice(1); if (flag === "h") options.help = true; else if (flag === "j") options.json = true; - else if (flag === "v") options.version = true; + else if (flag === "v" || flag === "V") options.version = true; else if (flag === "o") { const next = rawArgs[i + 1]; if (next && !next.startsWith("-")) { diff --git a/src/cli/runner.test.ts b/src/cli/runner.test.ts new file mode 100644 index 0000000..85c3d16 --- /dev/null +++ b/src/cli/runner.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from "bun:test"; +import { runCli } from "./runner.js"; +import { VERSION } from "../version.js"; + +describe("CLI runner - version and help aliases", () => { + const versionVariants = [ + ["version"], + ["v"], + ["V"], + ["-v"], + ["-V"], + ["--version"], + ]; + + for (const args of versionVariants) { + it(`correctly outputs version for "${args.join(" ")}"`, async () => { + const result = await runCli(args); + expect(result.exitCode).toBe(0); + expect(result.stderr).toBe(""); + expect(result.stdout).toBe(`hose-use v${VERSION}\n`); + }); + } + + const helpVariants = [ + [], + ["help"], + ["h"], + ["-h"], + ["--help"], + ]; + + for (const args of helpVariants) { + it(`correctly outputs help for "${args.join(" ") || ""}"`, async () => { + const result = await runCli(args); + expect(result.exitCode).toBe(0); + expect(result.stderr).toBe(""); + expect(result.stdout).toContain(`合思/易快报企业级 CLI 工具 (hose-use) v${VERSION}`); + expect(result.stdout).toContain("用法:"); + }); + } + + it("handles unknown commands gracefully", async () => { + const result = await runCli(["unknown-command-xyz"]); + expect(result.exitCode).toBe(1); + expect(result.stderr).toContain("未知命令域: unknown-command-xyz"); + }); +}); diff --git a/src/cli/runner.ts b/src/cli/runner.ts index 31c3260..6db9d26 100644 --- a/src/cli/runner.ts +++ b/src/cli/runner.ts @@ -88,11 +88,25 @@ async function dispatchCli( ): Promise { const parsed = parseArgs(rawArgs); - if (booleanOption(parsed.options, ["help", "h"]) || parsed.area === "help") { + if ( + booleanOption(parsed.options, ["help", "h"]) || + parsed.area === "help" || + parsed.area === "h" || + parsed.area === "-h" || + parsed.area === "--help" + ) { return { stdout: printHelp() + "\n", stderr: "", exitCode: 0 }; } - if (booleanOption(parsed.options, ["version", "v"]) || parsed.area === "version") { + if ( + booleanOption(parsed.options, ["version", "v", "V"]) || + parsed.area === "version" || + parsed.area === "v" || + parsed.area === "V" || + parsed.area === "-v" || + parsed.area === "-V" || + parsed.area === "--version" + ) { return { stdout: `hose-use v${VERSION}\n`, stderr: "", exitCode: 0 }; }