From 802c32c6bd0a9c0c334ca029300f479b8f97d772 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Wed, 29 Jul 2026 22:28:38 +0800 Subject: [PATCH] Refine CLI version flag alias parsing and test expectations --- src/cli.test.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/cli.ts | 26 ++++++++++++++++---------- 2 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 src/cli.test.ts diff --git a/src/cli.test.ts b/src/cli.test.ts new file mode 100644 index 0000000..bbb5090 --- /dev/null +++ b/src/cli.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, test } from "bun:test"; +import { spawnSync } from "node:child_process"; +import { join } from "node:path"; +import { VERSION } from "./version.js"; + +const CLI_PATH = join(import.meta.dirname, "cli.ts"); + +function runCli(args: string[]) { + return spawnSync("bun", [CLI_PATH, ...args], { + encoding: "utf-8" + }); +} + +describe("CLI version & help handling", () => { + test.each([ + ["version"], + ["v"], + ["-v"], + ["-version"], + ["--version"], + ["-V"], + ["--v"], + ["--V"] + ])("version command alias '%s' returns version JSON", (flag) => { + const result = runCli([flag]); + expect(result.status).toBe(0); + const parsed = JSON.parse(result.stdout.trim()); + expect(parsed.name).toBe("seal-home"); + expect(parsed.version).toBe(VERSION); + }); + + test("help command prints standard help banner", () => { + const result = runCli(["help"]); + expect(result.status).toBe(0); + expect(result.stdout).toContain("seal-home CLI"); + expect(result.stdout).toContain("Usage:"); + }); + + test("invalid command displays unknown command error with help hint", () => { + const result = runCli(["invalid-command"]); + expect(result.status).toBe(1); + expect(result.stderr).toContain("Unknown command: invalid-command. Run 'seal-home help' for available commands."); + }); +}); diff --git a/src/cli.ts b/src/cli.ts index efb7ce3..6c7b3a6 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -40,12 +40,13 @@ async function main() { const args = parseArgs(Bun.argv.slice(2)); const [area, action, maybeName] = args.command; - if (!area || area === "help" || args.options.help) { - printHelp(); - return; - } - - if (area === "version") { + if ( + area === "version" || + area === "v" || + Boolean(args.options.version) || + Boolean(args.options.v) || + Boolean(args.options.V) + ) { printJson({ name: "seal-home", version: VERSION @@ -53,6 +54,11 @@ async function main() { return; } + if (!area || area === "help" || args.options.help) { + printHelp(); + return; + } + if (area === "config" && action === "paths") { printJson({ candidates: getEnterprisesDirCandidates() @@ -475,7 +481,7 @@ async function main() { return; } - throw new Error(`Unknown command: ${args.command.join(" ")}`); + throw new Error(`Unknown command: ${args.command.join(" ")}. Run 'seal-home help' for available commands.`); } async function runServiceCommand(action?: string) { @@ -1241,10 +1247,10 @@ function parseArgs(argv: string[]): ParsedArgs { continue; } - if (arg.startsWith("--")) { - const key = arg.slice(2); + if (arg.startsWith("-")) { + const key = arg.replace(/^-+/, ""); const next = argv[index + 1]; - if (!next || next.startsWith("--")) { + if (!next || next.startsWith("-")) { options[key] = true; } else { options[key] = next;