Files
orca/src/shared/command-option-occurrence.ts
T
Jinjing 9eb73a608f Replace agentArgsOverride with unified removeAgentArgs approach (#25091)
Consolidate override detection and removal into removeAgentArgs. Previously
catalogs used agentArgsOverride to detect conflicts and removeAgentArgs to
strip them; now removeAgentArgs handles both by returning stripped tokens.
This eliminates redundancy and makes the intent clearer.

Enhance removeAgentArgOption with optional value filtering to support
selective removal for complex cases like Codex config overrides.
2026-10-03 15:25:43 -07:00

39 lines
1.2 KiB
TypeScript

function matchesOption(token: string, aliases: readonly string[]): boolean {
return aliases.some(
(alias) =>
token === alias ||
token.startsWith(`${alias}=`) ||
(alias.startsWith('-') &&
!alias.startsWith('--') &&
token.startsWith(alias) &&
token.length > alias.length)
)
}
export function findOptionOccurrence(
tokens: readonly string[],
aliases: readonly string[],
stopAtTerminator: boolean
): { index: number; consumed: number; value?: string } | null {
for (let index = 0; index < tokens.length; index += 1) {
const token = tokens[index]
if (stopAtTerminator && token === '--') {
break
}
if (!matchesOption(token, aliases)) {
continue
}
const nextToken = tokens[index + 1]
const consumesNext =
aliases.includes(token) && nextToken !== undefined && !nextToken.startsWith('-')
const alias = aliases.find((name) => matchesOption(token, [name]))
const value = consumesNext
? nextToken
: alias && token !== alias
? token.slice(alias.length + (token[alias.length] === '=' ? 1 : 0))
: undefined
return { index, consumed: consumesNext ? 2 : 1, value }
}
return null
}