yargs 18 CLI options validated with Zod 4: working versions (tested 2026-10-15)
Build a yargs 18 command parser that funnels argv through Zod 4 safeParse, returning typed options or field errors without exiting the process in tests.
passed2 passed · 0 failed2026-10-15 16:06 UTC- Status
- passed
- Last passed
- 2026-10-15 16:06 UTC
- Tests
- 2 / 2
- Language
- TypeScript
- Runtime
- node v22.23.3
npm install --no-audit --no-fund --ignore-scripts=false npm test
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38764699834
The code
Exactly the files that passed. Your agent can pull them with get_recipe("yargs-zod-cli") (install), or see them on GitHub.
{
"name": "yargs-zod-cli",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"yargs": "18.2.0",
"zod": "4.6.7"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15",
"@types/yargs": "17.0.35"
}
}
import yargs from "yargs/yargs";
import { hideBin } from "yargs/helpers";
import { z } from "zod";
export const DeployOpts = z.object({
env: z.enum(["dev", "staging", "prod"]),
tag: z.string().regex(/^v\d+\.\d+\.\d+$/),
dryRun: z.boolean().default(false),
regions: z.array(z.string().min(2)).min(1),
});
export type DeployOpts = z.infer<typeof DeployOpts>;
export async function parseDeploy(argv: string[]) {
const raw = await yargs(argv)
.scriptName("deploy")
.option("env", { type: "string", demandOption: true })
.option("tag", { type: "string", demandOption: true })
.option("dry-run", { type: "boolean", default: false })
.option("regions", { type: "array", string: true, demandOption: true })
.exitProcess(false)
.parseAsync();
const parsed = DeployOpts.safeParse({
env: raw.env,
tag: raw.tag,
dryRun: raw.dryRun ?? raw["dry-run"],
regions: raw.regions,
});
if (!parsed.success) {
return { ok: false as const, issues: z.flattenError(parsed.error).fieldErrors };
}
return { ok: true as const, data: parsed.data };
}
export function fromProcessArgv(argv = process.argv) {
return parseDeploy(hideBin(argv));
}
import { test } from "node:test";
import assert from "node:assert/strict";
import { parseDeploy } from "../src/cli.ts";
test("parses valid deploy argv", async () => {
const r = await parseDeploy(["--env", "prod", "--tag", "v1.2.3", "--regions", "us", "eu", "--dry-run"]);
assert.equal(r.ok, true);
if (r.ok) {
assert.equal(r.data.env, "prod");
assert.equal(r.data.tag, "v1.2.3");
assert.equal(r.data.dryRun, true);
assert.deepEqual(r.data.regions, ["us", "eu"]);
}
});
test("rejects bad tag", async () => {
const r = await parseDeploy(["--env", "dev", "--tag", "nope", "--regions", "us"]);
assert.equal(r.ok, false);
if (!r.ok) assert.ok(r.issues.tag);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.
Related stacks
Anthropic TypeScript SDK tool use loop, tested with a mocked fetch
Run a Claude tool_use loop with the official Anthropic SDK and Zod-validated tool inputs, tested offline with an injected fetch.
better-sqlite3 transactions with Zod-validated inputs
A small ledger on better-sqlite3 using db.transaction for atomic transfers, nested transactions as savepoints for all-or-nothing batches, and Zod 4 to validate inputs and rows, tested on in-memory SQLite.
Cheerio HTML scraping with Zod-validated records and saved fixtures
Scrape a product listing and a spec table with Cheerio, normalise prices and relative URLs, and validate every record with Zod 4 so broken cards are reported, tested offline against saved HTML fixtures.
Commander CLI with Zod option validation, tested in-process
A Commander subcommand CLI with variadic arguments, repeatable options, choices and Zod-backed argument parsers, made testable with exitOverride and configureOutput so node:test runs it in-process without spawning.