fix: --help and --version redirect
This commit is contained in:
@@ -112,6 +112,10 @@ function toPrintOutputMode(appMode: AppMode): Exclude<Mode, "rpc"> {
|
|||||||
return appMode === "json" ? "json" : "text";
|
return appMode === "json" ? "json" : "text";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isPlainRuntimeMetadataCommand(parsed: Args): boolean {
|
||||||
|
return !parsed.print && parsed.mode === undefined && (parsed.help === true || parsed.listModels !== undefined);
|
||||||
|
}
|
||||||
|
|
||||||
async function prepareInitialMessage(
|
async function prepareInitialMessage(
|
||||||
parsed: Args,
|
parsed: Args,
|
||||||
autoResizeImages: boolean,
|
autoResizeImages: boolean,
|
||||||
@@ -480,11 +484,6 @@ export async function main(args: string[], options?: MainOptions) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
time("parseArgs");
|
time("parseArgs");
|
||||||
let appMode = resolveAppMode(parsed, process.stdin.isTTY, process.stdout.isTTY);
|
|
||||||
const shouldTakeOverStdout = appMode !== "interactive";
|
|
||||||
if (shouldTakeOverStdout) {
|
|
||||||
takeOverStdout();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parsed.version) {
|
if (parsed.version) {
|
||||||
console.log(VERSION);
|
console.log(VERSION);
|
||||||
@@ -505,6 +504,12 @@ export async function main(args: string[], options?: MainOptions) {
|
|||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let appMode = resolveAppMode(parsed, process.stdin.isTTY, process.stdout.isTTY);
|
||||||
|
const shouldTakeOverStdout = appMode !== "interactive" && !isPlainRuntimeMetadataCommand(parsed);
|
||||||
|
if (shouldTakeOverStdout) {
|
||||||
|
takeOverStdout();
|
||||||
|
}
|
||||||
|
|
||||||
if (parsed.mode === "rpc" && parsed.fileArgs.length > 0) {
|
if (parsed.mode === "rpc" && parsed.fileArgs.length > 0) {
|
||||||
console.error(chalk.red("Error: @file arguments are not supported in RPC mode"));
|
console.error(chalk.red("Error: @file arguments are not supported in RPC mode"));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|||||||
@@ -80,6 +80,22 @@ async function runCli(args: string[]): Promise<{ stdout: string; stderr: string;
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("stdout cleanliness in non-interactive modes", () => {
|
describe("stdout cleanliness in non-interactive modes", () => {
|
||||||
|
it("prints --version to stdout when stdout is redirected", async () => {
|
||||||
|
const result = await runCli(["--version"]);
|
||||||
|
|
||||||
|
expect(result.code).toBe(0);
|
||||||
|
expect(result.stdout.trim()).toMatch(/^\d+\.\d+\.\d+/);
|
||||||
|
expect(result.stderr).toBe("");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("prints plain --help to stdout when stdout is redirected", async () => {
|
||||||
|
const result = await runCli(["--help"]);
|
||||||
|
|
||||||
|
expect(result.code).toBe(0);
|
||||||
|
expect(result.stdout).toContain("Usage:");
|
||||||
|
expect(result.stderr).not.toContain("Usage:");
|
||||||
|
});
|
||||||
|
|
||||||
it("keeps stdout empty for --mode json --help while routing trusted startup chatter to stderr", async () => {
|
it("keeps stdout empty for --mode json --help while routing trusted startup chatter to stderr", async () => {
|
||||||
const result = await runCli(["--mode", "json", "--help", "--approve"]);
|
const result = await runCli(["--mode", "json", "--help", "--approve"]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user