fix(cli): correct --verbose / -v option syntax in acp commands (#21303)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 20d058dcf524765f53681b601f605b0d4b3129f3
Co-authored-by: adhitShet <131381638+adhitShet@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
adhitShet
2026-02-20 06:04:22 +04:00
committed by GitHub
parent 9264a8e21a
commit 164d478652
8 changed files with 120 additions and 8 deletions

View File

@@ -0,0 +1,39 @@
import process from "node:process";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const { buildProgram } = await import("./build-program.js");
describe("buildProgram version alias handling", () => {
let originalArgv: string[];
beforeEach(() => {
originalArgv = [...process.argv];
});
afterEach(() => {
process.argv = originalArgv;
vi.restoreAllMocks();
});
it("exits with version output for root -v", () => {
process.argv = ["node", "openclaw", "-v"];
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`process.exit:${String(code)}`);
}) as typeof process.exit);
expect(() => buildProgram()).toThrow("process.exit:0");
expect(logSpy).toHaveBeenCalledTimes(1);
expect(exitSpy).toHaveBeenCalledWith(0);
});
it("does not treat subcommand -v as root version alias", () => {
process.argv = ["node", "openclaw", "acp", "-v"];
const exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`unexpected process.exit:${String(code)}`);
}) as typeof process.exit);
expect(() => buildProgram()).not.toThrow();
expect(exitSpy).not.toHaveBeenCalled();
});
});

View File

@@ -2,6 +2,7 @@ import type { Command } from "commander";
import { formatDocsLink } from "../../terminal/links.js";
import { isRich, theme } from "../../terminal/theme.js";
import { escapeRegExp } from "../../utils.js";
import { hasFlag, hasRootVersionAlias } from "../argv.js";
import { formatCliBannerLine, hasEmittedCliBanner } from "../banner.js";
import { replaceCliName, resolveCliName } from "../cli-name.js";
import { getCoreCliCommandsWithSubcommands } from "./command-registry.js";
@@ -98,9 +99,9 @@ export function configureProgramHelp(program: Command, ctx: ProgramContext) {
});
if (
process.argv.includes("-V") ||
process.argv.includes("--version") ||
process.argv.includes("-v")
hasFlag(process.argv, "-V") ||
hasFlag(process.argv, "--version") ||
hasRootVersionAlias(process.argv)
) {
console.log(ctx.programVersion);
process.exit(0);