// Argument parsing for `ahub task handoff` (#604, agent-hub#1030). Pure: argv in, one request or a usage line out, so main.ts stays a thin dispatcher and every form is testable without a daemon. import { CLASSES } from "../hub/board.ts"; import { HANDOFF_KEY } from "../hub/handoff.ts"; export const HANDOFF_USAGE = "usage: ahub task handoff --list | --sync | --project [] [--path

]... [--issue ] [--task ] [--detail ] [--duplicate-ok ] [--key ] [--json] [--color=auto|always|never]"; /** `--list` and `--sync` are the person's views of the handoffs this project made (#734): the list shows each one's age and whether its target is reachable, the sync asks the targets now. */ export type HandoffCliView = { view: "list" | "sync" }; export interface HandoffCliRequest { project: string; title: string; class?: string; detail?: string; paths?: string[]; issue?: number; task?: number; duplicate_ok?: string; handoffKey?: string } /** `operands` is argv after `task handoff`, presentation flags already removed. A first word that is a class name is the class, said out loud by the caller (the documented short form). */ export function parseHandoffArgs(operands: readonly string[]): HandoffCliRequest | HandoffCliView | { usage: string } { if (operands.includes("--list") || operands.includes("--sync")) { // A view takes no other operand: `--list --project x` would read as filing a handoff and listing at once, which is neither. const views = operands.filter(arg => arg === "--list" || arg === "--sync"); if (operands.length !== views.length || new Set(views).size !== 1) return { usage: HANDOFF_USAGE }; return { view: views[0] === "--list" ? "list" : "sync" }; } const one: Record = {}, paths: string[] = [], rest: string[] = []; for (let i = 0; i < operands.length; i++) { const arg = operands[i]!; if (arg === "--project" || arg === "--detail" || arg === "--issue" || arg === "--task" || arg === "--path" || arg === "--duplicate-ok" || arg === "--key") { const value = operands[++i]; if (value === undefined || value.startsWith("--")) return { usage: HANDOFF_USAGE }; if (arg === "--path") paths.push(value); else { if (Object.hasOwn(one, arg)) return { usage: HANDOFF_USAGE }; one[arg] = value; } } else if (arg.startsWith("--") || arg === "-h") return { usage: `unknown option ${JSON.stringify(arg)}; nothing was filed\n${HANDOFF_USAGE}` }; else rest.push(arg); } const project = one["--project"]; const classed = rest[0] !== undefined && (CLASSES as readonly string[]).includes(rest[0]) && rest.length > 1; const title = (classed ? rest.slice(1) : rest).join(" ").trim(); if (!project || !title) return { usage: HANDOFF_USAGE }; if (one["--issue"] !== undefined && !/^[1-9]\d{0,9}$/.test(one["--issue"])) return { usage: `${HANDOFF_USAGE}\n--issue is the target repository's issue number` }; if (one["--task"] !== undefined && !/^[1-9]\d{0,9}$/.test(one["--task"])) return { usage: `${HANDOFF_USAGE}\n--task is this project's own task id` }; if (one["--key"] !== undefined && !HANDOFF_KEY.test(one["--key"])) return { usage: `${HANDOFF_USAGE}\n--key is the key a handoff that was not confirmed named (a lowercase UUID v4): the same request with it is filed once` }; return { project, title, ...(classed ? { class: rest[0]! } : {}), ...(one["--detail"] !== undefined ? { detail: one["--detail"] } : {}), ...(paths.length ? { paths } : {}), ...(one["--issue"] !== undefined ? { issue: Number(one["--issue"]) } : {}), ...(one["--task"] !== undefined ? { task: Number(one["--task"]) } : {}), ...(one["--duplicate-ok"] !== undefined ? { duplicate_ok: one["--duplicate-ok"] } : {}), ...(one["--key"] !== undefined ? { handoffKey: one["--key"] } : {}) }; }