From 563d4ff64be076031813e735feb5df2557674296 Mon Sep 17 00:00:00 2001 From: Trevin Chow Date: Wed, 27 May 2026 00:19:43 -0700 Subject: [PATCH] fix(cli): allow flag values that start with -- via --flag=value (#2892) --- src/cli/args.test.ts | 27 +++++++++++++++++++++++++++ src/cli/args.ts | 12 +++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/cli/args.test.ts b/src/cli/args.test.ts index 868e66516..a134a8a2a 100644 --- a/src/cli/args.test.ts +++ b/src/cli/args.test.ts @@ -10,4 +10,31 @@ describe('parseArgs', () => { expect(parsed.flags.get('value')).toBe('') expect(parsed.flags.get('json')).toBe(true) }) + + it('accepts a flag value that starts with -- via the = form', () => { + const parsed = parseArgs(['terminal', 'send', '--text=--help']) + + expect(parsed.commandPath).toEqual(['terminal', 'send']) + expect(parsed.flags.get('text')).toBe('--help') + }) + + it('splits --flag=value on the first = so values may contain =', () => { + const parsed = parseArgs(['set', 'cookie', '--value=a=b=c']) + + expect(parsed.flags.get('value')).toBe('a=b=c') + }) + + it('treats --flag= as an empty string value', () => { + const parsed = parseArgs(['--value=']) + + expect(parsed.flags.get('value')).toBe('') + }) + + it('still parses boolean flags and space-separated values', () => { + const parsed = parseArgs(['tab', 'create', '--json', '--url', 'https://example.com']) + + expect(parsed.commandPath).toEqual(['tab', 'create']) + expect(parsed.flags.get('json')).toBe(true) + expect(parsed.flags.get('url')).toBe('https://example.com') + }) }) diff --git a/src/cli/args.ts b/src/cli/args.ts index 19529b535..02992e3b3 100644 --- a/src/cli/args.ts +++ b/src/cli/args.ts @@ -29,7 +29,17 @@ export function parseArgs(argv: string[]): ParsedArgs { continue } - const flag = token.slice(2) + const assignment = token.slice(2) + // Why: `--flag=value` is the only unambiguous way to pass a value that + // itself starts with `--` (e.g. `--text=--help`); the space-separated form + // treats a `--`-leading next token as a new flag, so it can't express one. + const equalsIndex = assignment.indexOf('=') + if (equalsIndex !== -1) { + flags.set(assignment.slice(0, equalsIndex), assignment.slice(equalsIndex + 1)) + continue + } + + const flag = assignment const hasNext = i + 1 < argv.length const next = argv[i + 1] if (!hasNext || next.startsWith('--')) {