Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { installProxyIfConfigured } from "../net/proxy.js";
import { escalationContract } from "../prompt-fragments.js";
import { startCpuProfile, stopAndSaveCpuProfile } from "./cpu-prof.js";
import { resolveDashboardHost, resolveDashboardToken } from "./dashboard-options.js";
import { parseNonNegativeIntegerOption, parsePositiveIntegerOption } from "./number-options.js";
import { resolveBareCommandMode, resolveContinueFlag, resolveDefaults } from "./resolve.js";
import { markPhase } from "./startup-profile.js";

Expand Down Expand Up @@ -390,7 +391,7 @@ program
program
.command("prune-sessions")
.description(t("cli.pruneSessions"))
.option("--days <n>", t("ui.pruneDaysHint"), (v) => Number.parseInt(v, 10))
.option("--days <n>", t("ui.pruneDaysHint"), parsePositiveIntegerOption)
.option("--dry-run", t("ui.pruneDryRunHint"))
.action(async (opts) => {
const { pruneSessionsCommand } = await import("./commands/prune-sessions.js");
Expand All @@ -401,8 +402,8 @@ program
.command("events <name>")
.description(t("cli.events"))
.option("--type <type>", t("ui.eventTypeHint"))
.option("--since <id>", t("ui.eventSinceHint"), (v) => Number.parseInt(v, 10))
.option("--tail <n>", t("ui.eventTailHint"), (v) => Number.parseInt(v, 10))
.option("--since <id>", t("ui.eventSinceHint"), parseNonNegativeIntegerOption)
.option("--tail <n>", t("ui.eventTailHint"), parsePositiveIntegerOption)
.option("--json", t("ui.jsonHint"))
.option("--projection", t("ui.projectionHint"))
.action(async (name: string, opts) => {
Expand All @@ -421,8 +422,8 @@ program
.command("replay <transcript>")
.description(t("cli.replay"))
.option("--print", t("ui.printHint"))
.option("--head <n>", t("ui.headHint"), (v) => Number.parseInt(v, 10))
.option("--tail <n>", t("ui.tailHint"), (v) => Number.parseInt(v, 10))
.option("--head <n>", t("ui.headHint"), parsePositiveIntegerOption)
.option("--tail <n>", t("ui.tailHint"), parsePositiveIntegerOption)
.action(async (transcript: string, opts) => {
const { replayCommand } = await import("./commands/replay.js");
await replayCommand({
Expand Down Expand Up @@ -462,8 +463,8 @@ mcp
.option("--json", t("ui.jsonHintCatalog"))
.option("--local", t("ui.mcpLocalHint"))
.option("--refresh", t("ui.mcpRefreshHint"))
.option("--limit <n>", t("ui.mcpLimitHint"), (v) => Number.parseInt(v, 10))
.option("--pages <n>", t("ui.mcpPagesHint"), (v) => Number.parseInt(v, 10))
.option("--limit <n>", t("ui.mcpLimitHint"), parsePositiveIntegerOption)
.option("--pages <n>", t("ui.mcpPagesHint"), parsePositiveIntegerOption)
.option("--all", t("ui.mcpAllHint"))
.action(async (opts) => {
try {
Expand All @@ -487,8 +488,8 @@ mcp
.description(t("ui.mcpSearchDescription"))
.option("--json", t("ui.jsonHintCatalog"))
.option("--refresh", t("ui.mcpRefreshHint"))
.option("--limit <n>", t("ui.mcpLimitHint"), (v) => Number.parseInt(v, 10))
.option("--max-pages <n>", t("ui.mcpMaxPagesHint"), (v) => Number.parseInt(v, 10))
.option("--limit <n>", t("ui.mcpLimitHint"), parsePositiveIntegerOption)
.option("--max-pages <n>", t("ui.mcpMaxPagesHint"), parsePositiveIntegerOption)
.action(async (query: string, opts) => {
try {
const { mcpSearchCommand } = await import("./commands/mcp.js");
Expand All @@ -509,7 +510,7 @@ mcp
.command("install <name>")
.description(t("ui.mcpInstallDescription"))
.option("--refresh", t("ui.mcpRefreshHint"))
.option("--max-pages <n>", t("ui.mcpMaxPagesHint"), (v) => Number.parseInt(v, 10))
.option("--max-pages <n>", t("ui.mcpMaxPagesHint"), parsePositiveIntegerOption)
.action(async (name: string, opts) => {
try {
const { mcpInstallCommand } = await import("./commands/mcp.js");
Expand Down
22 changes: 22 additions & 0 deletions src/cli/number-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { InvalidArgumentError } from "commander";

export function parsePositiveIntegerOption(raw: string): number {
return parseIntegerOption(raw, { min: 1 });
}

export function parseNonNegativeIntegerOption(raw: string): number {
return parseIntegerOption(raw, { min: 0 });
}

function parseIntegerOption(raw: string, opts: { min: number }): number {
if (!/^\d+$/.test(raw)) {
throw new InvalidArgumentError("must be an integer");
}
const parsed = Number(raw);
if (!Number.isSafeInteger(parsed) || parsed < opts.min) {
throw new InvalidArgumentError(
opts.min === 0 ? "must be a non-negative integer" : "must be a positive integer",
);
}
return parsed;
}
25 changes: 25 additions & 0 deletions tests/cli-number-options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { InvalidArgumentError } from "commander";
import { describe, expect, it } from "vitest";
import {
parseNonNegativeIntegerOption,
parsePositiveIntegerOption,
} from "../src/cli/number-options.js";

describe("CLI number option parsers", () => {
it("accepts positive integer strings", () => {
expect(parsePositiveIntegerOption("1")).toBe(1);
expect(parsePositiveIntegerOption("42")).toBe(42);
});

it("rejects fractional, suffixed, and zero positive-integer values", () => {
for (const raw of ["1.5", "10abc", "0", "-1", ""]) {
expect(() => parsePositiveIntegerOption(raw)).toThrow(InvalidArgumentError);
}
});

it("allows zero only for non-negative integer options", () => {
expect(parseNonNegativeIntegerOption("0")).toBe(0);
expect(parseNonNegativeIntegerOption("12")).toBe(12);
expect(() => parseNonNegativeIntegerOption("-1")).toThrow(InvalidArgumentError);
});
});
Loading