# CLI argument validation ## Status Accepted, 2026-05-18. ## Context Optique was adopted as the CLI argument parser before this document existed. The choice is recorded informally in `PHILOSOPHY.tool-choices.md:26` (`@optique/core`, `@optique/run`: npm `dev` tag for 1. x) but never had a dedicated decision doc covering value-level validation. Current state when this decision was made: - Three CLIs use Optique: `package/cli/fy/`, `package/cli/mvm/`, `package/cli/vmsync/`. All three rely on `@optique/core/valueparser`'s built-in primitives (`string({metavar})`, `integer()`) and `@optique/core/constructs`' `map()` for shape transforms. - Three CLIs are not on Optique: `package/git-policy/cli/`, `package/cli/rgffplay/`, `package/cli/terminal-exec/`. They parse `process.argv` directly. The reasons they are not on Optique are project-specific and not recorded here; this decision does not speak to whether they should migrate. - No CLI currently performs shape-level validation beyond "is it a string" or "is it an integer". Memory strings like `2g`/`512m`, CPU ranges, URL formats, picklist enums, and file-path existence checks are all unchecked at parse time. - Valibot is already in the workspace catalog (`PHILOSOPHY.tool-choices.md:27`: `valibot@>=1.4.0`, Standard Schema-compatible). It is used in `package/pi-plugin/advisor/` and `package/ssg/aquati.cat/`, but never in a CLI package. `@optique/valibot` is Optique's adapter that lets a Valibot schema act as an Optique `ValueParser`. Adopting it gives the CLIs access to the Valibot rule surface (`v.url`, `v.email`, `v.regex`, `v.minValue`, `v.maxValue`, `v.picklist`, custom `v.transform`) at parse time, with errors surfaced through Optique's standard error-rendering path. ## Decision Use `@optique/valibot` **selectively**: reach for it only when a value parser needs validation beyond the type built-in primitives already provide. Default to the plain `string()` and `integer()` primitives from `@optique/core/valueparser` for parsers that accept any well-typed value (names, freeform paths, generic identifiers). Decision rule for new value parsers: - The parser is "any string" or "any integer": use `string()` / `integer()`. No Valibot. - The parser has a shape constraint (regex, range, picklist, URL, email, custom `transform` into a non-string type): use `valibot(v.pipe(v.string(), ...))` from `@optique/valibot`. - The parser needs a non-string output type (number, Date, parsed config): use `valibot(v.pipe(v.string(), v.transform(...), ...))`. CLI arguments are always strings on entry; `v.transform()` is the bridge. Errors should use the `errors.valibotError` option to keep messages consistent with the surrounding CLI tone. Async Valibot pipes (`v.pipeAsync`, `v.checkAsync`) are not supported by Optique's synchronous `ValueParser.parse()`. Async validation belongs after argument parsing, not inside it. Out-of-scope for this decision: - Migrating `cli-git`, `cli-rgffplay`, `cli-terminal-exec` to Optique. Their direct-`argv` approach is a separate decision; the reasons live outside this document. - Rewriting existing `string()` and `integer()` parsers in `cli-fy`, `cli-mvm`, `cli-vmsync` to use `valibot(v.string())`. The plain primitives stay; only new shape-constrained parsers reach for `@optique/valibot`. ## Rejected alternatives ### Broad adoption: rewrite every value parser as a Valibot pipe Rejected. The bulk of current parsers accept "any string" (VM names, generic file paths, freeform identifiers). Wrapping them in `v.pipe(v.string())` adds three import lines and a wrapper call without introducing any new check the type system was not already enforcing. The churn touches three working CLIs for no behavioural payoff and risks regressions in the parser-combinator types (`Parser` is invariant in state; replacing the parser source changes inferred types across the discriminated union). Specific gate: violates `AGENTS.md` "Don't add features beyond what the task requires"; the wrapper is overhead the parser does not need. ### Status quo: keep ad-hoc `map()` validation when shape checks are needed Rejected. Hand-rolled validation inside `map()` callbacks duplicates work the workspace's chosen validation library (Valibot) already does, and forfeits Standard Schema interop. The repo picked Valibot specifically for Standard Schema compatibility (`PHILOSOPHY.tool-choices.md:27`); writing per-parser validation in `map()` blocks defeats that choice for CLI inputs while keeping it for every other validation surface. Specific gate: would require maintaining two parallel validation idioms (Valibot everywhere else, hand-rolled in CLIs) for the same constraint shapes. ### Use `zod` instead of Valibot for the same role Rejected without separate vetting. The workspace has already chosen Valibot at the catalog level. Introducing a second validation library for one subsystem creates a long-term split where contributors must remember which library applies in which package. The `@optique/zod` adapter exists, but the case for swapping the workspace's validation default is out of scope for this decision; it would require its own decision doc against `PHILOSOPHY.tool-choices.md:27`. ## Vendor and dependency notes ### `@optique/valibot` audit Verified 2026-05-18 via `gh api repos/dahlia/optique`, `https://registry.npmjs.org/@optique/valibot/latest`, and `https://api.npmjs.org/downloads/point/last-week/@optique/valibot`: - License: MIT. - Maintainer: Hong Minhee (`dahlia`); single-maintainer (3315 contributions vs second contributor's 2). Same author as LogTape and Fedify; bus factor identical to `@optique/core` (already accepted). - Release: 1.1.0 at HEAD, 1.0.0 released 2026-04-14, first npm publish 2025-08-19. - Weekly downloads: 3 078 (`@optique/valibot`) vs 15 832 (`@optique/core`), 16 398 (`@optique/run`). Adapter usage is roughly 1:5 against the core packages. - Unpacked size: 49 267 bytes. Subpath-only `valibot` import surface; `sideEffects: false` allows tree-shaking. - Peer dep: `valibot ^1.2.0` per the published `package.json`. The README's "0.42.0 and above" claim is stale; the published `peerDependencies` field is authoritative. The workspace catalog pins `valibot@>=1.4.0`; the constraint is satisfied. - Open security advisories: none (`gh api .../security-advisories` returned `[]`). - Open bug-labeled issues: zero (11 closed). ### Compatibility with the Node CLI baseline All Optique CLIs use `#!/usr/bin/env node`. `@optique/valibot` declares engines `node>=20.0.0`, `bun>=1.2.0`, `deno>=2.3.0`. The workspace's Node version satisfies this. The adapter ships dual ESM/CJS exports; the Node ESM resolver picks the ESM entry. ### Catalog management Add `@optique/valibot` to `pnpm-workspace.yaml`'s `catalog:` block alongside the existing `@optique/core` and `@optique/run` entries when the first CLI adopts it. Track its version on the same `dev` tag as the sibling Optique packages until they all move to a stable major. ## When to revisit - If Hong Minhee's maintainership lapses (no commits for > 6 months), re-audit the alternatives. The realistic swap target inside the same Valibot-compatible space is `@effect/cli` plus Effect's own validation layer; the cost is migrating away from Optique entirely, not just the adapter. - If a non-CLI subsystem proposes a Standard Schema-compatible validation library other than Valibot, this decision needs renewal alongside `PHILOSOPHY.tool-choices.md:27`. - If async value parsing becomes a hard requirement for a CLI input (e.g. resolving DNS or checking a remote registry at parse time), Optique cannot deliver it synchronously; the validation must move out of the parser into a post-parse step.