fix(cli): support version and help aliases (v, V, -V, h, -h)
This commit is contained in:
1 parent
40b3b29ca5
commit
4fbd737402
3 files changed
+65
-3
No files matched your search
+2
-1
@@ -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("-")) {
|
||||
|
||||
@@ -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(" ") || "<empty>"}"`, 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");
|
||||
});
|
||||
});
|
||||
+16
-2
@@ -88,11 +88,25 @@ async function dispatchCli(
|
||||
): Promise<CliResult> {
|
||||
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 };
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user