Files
orca/src/cli/flags.ts
T
Max Hsu 4eaa12fef2 fix(cli): let worktree set --comment "" clear the comment (#37515)
`worktree set` read --comment through getOptionalStringFlag, which turns
an empty value into undefined, so `--comment ""` and `--comment=` sent no
comment field and the stored comment survived while the command reported
ok:true. Keep an explicit empty value for this flag only; the shared
helper and the RPC schema are unchanged.

Fixes #37464
2026-11-02 20:49:15 +00:00

183 lines
5.4 KiB
TypeScript

import { RuntimeClientError } from './runtime/types'
import { REPEATED_FLAG_SEPARATOR } from './args'
import { describeQuoteStrippedJsonFlag } from './quote-stripped-json-flag'
import type { RepoKind } from '../shared/repo-types'
export function getOptionalRepoKind(flags: Map<string, string | boolean>): RepoKind | undefined {
const kind = getOptionalStringFlag(flags, 'kind')
if (kind === undefined || kind === 'git' || kind === 'folder') {
return kind
}
throw new RuntimeClientError('invalid_argument', '--kind must be git or folder')
}
export function getRequiredStringFlag(flags: Map<string, string | boolean>, name: string): string {
const value = flags.get(name)
rejectValuelessFlag(value, name)
if (typeof value === 'string' && value.length > 0) {
return value
}
throw new RuntimeClientError('invalid_argument', `Missing required --${name}`)
}
export function getRequiredStringFlagAllowingEmpty(
flags: Map<string, string | boolean>,
name: string
): string {
const value = flags.get(name)
rejectValuelessFlag(value, name)
if (typeof value === 'string') {
return value
}
throw new RuntimeClientError('invalid_argument', `Missing required --${name}`)
}
export function getOptionalStringFlag(
flags: Map<string, string | boolean>,
name: string
): string | undefined {
const value = flags.get(name)
rejectValuelessFlag(value, name)
return typeof value === 'string' && value.length > 0 ? value : undefined
}
// Clearing flags (e.g. `worktree set --comment ""`) must keep the empty value instead of dropping it.
export function getOptionalStringFlagAllowingEmpty(
flags: Map<string, string | boolean>,
name: string
): string | undefined {
const value = flags.get(name)
rejectValuelessFlag(value, name)
return typeof value === 'string' ? value : undefined
}
export function getPresentStringFlag(
flags: Map<string, string | boolean>,
name: string
): string | undefined {
if (!flags.has(name)) {
return undefined
}
const value = flags.get(name)
if (typeof value === 'string' && value.length > 0) {
return value
}
throw new RuntimeClientError('invalid_argument', `Missing value for --${name}`)
}
// A missing valued flag must fail: silently regenerating mutation IDs can submit prompts twice.
export function rejectValuelessFlag(value: string | boolean | undefined, name: string): void {
if (value === true) {
throw new RuntimeClientError(
'invalid_argument',
`--${name} requires a value; it was passed with none.`
)
}
}
/** Detect shell-stripped JSON quotes before downstream parsing so the error names the argv boundary. */
export function getOptionalJsonFlag(
flags: Map<string, string | boolean>,
name: string
): string | undefined {
const value = getOptionalStringFlag(flags, name)
if (value === undefined) {
return undefined
}
const mangled = describeQuoteStrippedJsonFlag(name, value)
if (mangled) {
throw new RuntimeClientError('invalid_argument', mangled)
}
return value
}
export function getRepeatedStringFlag(
flags: Map<string, string | boolean>,
name: string
): string[] {
const value = getOptionalStringFlag(flags, name)
return value === undefined
? []
: value.split(REPEATED_FLAG_SEPARATOR).filter((entry) => entry.length > 0)
}
export function getOptionalNumberFlag(
flags: Map<string, string | boolean>,
name: string
): number | undefined {
const value = flags.get(name)
rejectValuelessFlag(value, name)
if (typeof value !== 'string' || value.length === 0) {
return undefined
}
const parsed = Number(value)
if (!Number.isFinite(parsed)) {
throw new RuntimeClientError('invalid_argument', `Invalid numeric value for --${name}`)
}
return parsed
}
export function getOptionalPositiveIntegerFlag(
flags: Map<string, string | boolean>,
name: string
): number | undefined {
const value = getOptionalNumberFlag(flags, name)
if (value === undefined) {
return undefined
}
if (!Number.isInteger(value) || value <= 0) {
throw new RuntimeClientError('invalid_argument', `Invalid positive integer for --${name}`)
}
return value
}
export function getOptionalNonNegativeIntegerFlag(
flags: Map<string, string | boolean>,
name: string
): number | undefined {
const value = getOptionalNumberFlag(flags, name)
if (value === undefined) {
return undefined
}
if (!Number.isInteger(value) || value < 0) {
throw new RuntimeClientError('invalid_argument', `Invalid non-negative integer for --${name}`)
}
return value
}
export function getRequiredPositiveNumber(
flags: Map<string, string | boolean>,
name: string
): number {
const raw = getRequiredStringFlag(flags, name)
const value = Number(raw)
if (!Number.isFinite(value) || value <= 0) {
throw new RuntimeClientError('invalid_argument', `--${name} must be a positive number`)
}
return value
}
export function getRequiredFiniteNumber(
flags: Map<string, string | boolean>,
name: string
): number {
const raw = getRequiredStringFlag(flags, name)
const value = Number(raw)
if (!Number.isFinite(value)) {
throw new RuntimeClientError('invalid_argument', `--${name} must be a valid number`)
}
return value
}
export function getOptionalNullableNumberFlag(
flags: Map<string, string | boolean>,
name: string
): number | null | undefined {
const value = flags.get(name)
rejectValuelessFlag(value, name)
if (value === 'null') {
return null
}
return getOptionalNumberFlag(flags, name)
}