fix(cli): support case-insensitive version and help flags and subcommands

This commit is contained in:
gitea committed 2026-09-05 22:16:56 +08:00
1 parent c99c5d98ec
commit 269b7e8f0f
3 files changed
+56 -22

No files matched your search

+29 -17
View File
@@ -21,7 +21,7 @@ import { handleMessageCommand } from "./commands/message.js";
import { handleDimensionCommand } from "./commands/dimension.js";
import { handleRoleCommand } from "./commands/role.js";
import { handlePermissionCommand } from "./commands/permission.js";
import type { CliResult, CliRunOptions } from "./types.js";
import type { CliParsedArgs, CliResult, CliRunOptions } from "./types.js";
const STANDING_COMMANDS = new Set(["http", "server", "web", "mcp"]);
@@ -82,31 +82,43 @@ async function runCliOnce(
}
}
function isHelpRequested(parsed: CliParsedArgs): boolean {
const normArea = (parsed.area || "").replace(/^-+/, "").toLowerCase();
if (normArea === "help" || normArea === "h" || normArea === "?") return true;
for (const [key, val] of Object.entries(parsed.options)) {
const normKey = key.replace(/^-+/, "").toLowerCase();
if (normKey === "help" || normKey === "h" || normKey === "?") {
if (val === true || val === "true" || val === "") return true;
}
}
return false;
}
function isVersionRequested(parsed: CliParsedArgs): boolean {
const normArea = (parsed.area || "").replace(/^-+/, "").toLowerCase();
if (normArea === "version" || normArea === "v") return true;
for (const [key, val] of Object.entries(parsed.options)) {
const normKey = key.replace(/^-+/, "").toLowerCase();
if (normKey === "version" || normKey === "v") {
if (val === true || val === "true" || val === "") return true;
}
}
return false;
}
async function dispatchCli(
rawArgs: string[],
options: CliRunOptions = {}
): Promise<CliResult> {
const parsed = parseArgs(rawArgs);
if (
booleanOption(parsed.options, ["help", "h"]) ||
parsed.area === "help" ||
parsed.area === "h" ||
parsed.area === "-h" ||
parsed.area === "--help"
) {
if (isHelpRequested(parsed)) {
return { stdout: printHelp() + "\n", stderr: "", exitCode: 0 };
}
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"
) {
if (isVersionRequested(parsed)) {
return { stdout: `hose-use v${VERSION}\n`, stderr: "", exitCode: 0 };
}