fix(cli): resolve --url option collision in browser cookies set

When addGatewayClientOptions registers --url on the parent browser
command, Commander.js captures it before the cookies set subcommand
can receive it. Switch from requiredOption to option and resolve
via inheritOptionFromParent, matching the existing pattern used
for --target-id.

Fixes #24811

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
(cherry picked from commit 96fcb963ec)
This commit is contained in:
HCL
2026-02-24 12:04:06 +08:00
committed by Peter Steinberger
parent d51a4695f0
commit 24e52f53e4
2 changed files with 47 additions and 3 deletions

View File

@@ -4,6 +4,17 @@ import { defaultRuntime } from "../runtime.js";
import { callBrowserRequest, type BrowserParentOpts } from "./browser-cli-shared.js";
import { inheritOptionFromParent } from "./command-options.js";
function resolveUrl(opts: { url?: string }, command: Command): string | undefined {
if (typeof opts.url === "string" && opts.url.trim()) {
return opts.url.trim();
}
const inherited = inheritOptionFromParent<string>(command, "url");
if (typeof inherited === "string" && inherited.trim()) {
return inherited.trim();
}
return undefined;
}
function resolveTargetId(rawTargetId: unknown, command: Command): string | undefined {
const local = typeof rawTargetId === "string" ? rawTargetId.trim() : "";
if (local) {
@@ -58,12 +69,18 @@ export function registerBrowserCookiesAndStorageCommands(
.description("Set a cookie (requires --url or domain+path)")
.argument("<name>", "Cookie name")
.argument("<value>", "Cookie value")
.requiredOption("--url <url>", "Cookie URL scope (recommended)")
.option("--url <url>", "Cookie URL scope (recommended)")
.option("--target-id <id>", "CDP target id (or unique prefix)")
.action(async (name: string, value: string, opts, cmd) => {
const parent = parentOpts(cmd);
const profile = parent?.browserProfile;
const targetId = resolveTargetId(opts.targetId, cmd);
const url = resolveUrl(opts, cmd);
if (!url) {
defaultRuntime.error(danger("Missing required --url option for cookies set"));
defaultRuntime.exit(1);
return;
}
try {
const result = await callBrowserRequest(
parent,
@@ -73,7 +90,7 @@ export function registerBrowserCookiesAndStorageCommands(
query: profile ? { profile } : undefined,
body: {
targetId,
cookie: { name, value, url: opts.url },
cookie: { name, value, url },
},
},
{ timeoutMs: 20000 },