Build InitOptions from raw Commander opts + globals.
( cmdOpts: SetupCmdOpts, command: Command, defaultAgent: AgentTarget, )
| 452 | |
| 453 | /** Build {@link InitOptions} from raw Commander opts + globals. */ |
| 454 | function buildSetupOptions( |
| 455 | cmdOpts: SetupCmdOpts, |
| 456 | command: Command, |
| 457 | defaultAgent: AgentTarget, |
| 458 | ): InitOptions { |
| 459 | const common = resolveCommonOptions(command); |
| 460 | |
| 461 | // Commander sets `agent: false` (boolean) when `--no-agent` is passed, |
| 462 | // because `--no-agent` is the negation of `--agent <target>`. Guard against |
| 463 | // this: if agent is falsy (false or empty string) default to the CLI default |
| 464 | // so runInstall never receives a non-string target. |
| 465 | const resolvedAgent = |
| 466 | cmdOpts.agent && typeof cmdOpts.agent === 'string' ? cmdOpts.agent : defaultAgent; |
| 467 | const isNoAgent = cmdOpts.noAgent === true || cmdOpts.agent === false; |
| 468 | |
| 469 | // Detect conflict when both --no-agent and --agent <target> appear in the raw |
| 470 | // args. Commander only populates `rawArgs` on the ROOT command passed to |
| 471 | // parseAsync; subcommands have an empty array. Walk up to the root so we |
| 472 | // always inspect the full argv. |
| 473 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 474 | let root: any = command; |
| 475 | while (root.parent) root = root.parent; |
| 476 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 477 | const rawArgs: string[] = (root as any).rawArgs ?? process.argv; |
| 478 | const rawArgConflict = |
| 479 | rawArgs.some((a: string) => a === '--no-agent') && |
| 480 | rawArgs.some((a: string) => a === '--agent' || a.startsWith('--agent=')); |
| 481 | |
| 482 | return { |
| 483 | ...common, |
| 484 | apiKey: cmdOpts.apiKey, |
| 485 | fromEnv: Boolean(cmdOpts.fromEnv), |
| 486 | agent: resolvedAgent, |
| 487 | noAgent: isNoAgent, |
| 488 | force: Boolean(cmdOpts.force), |
| 489 | dir: cmdOpts.dir, |
| 490 | yes: Boolean(cmdOpts.yes), |
| 491 | rawArgConflict, |
| 492 | }; |
| 493 | } |
| 494 | |
| 495 | /** Shared action for `setup` and the deprecated `init` alias. */ |
| 496 | async function runSetupAction( |
no test coverage detected