Clerc is a strongly-typed CLI framework for Node.js 24+, with built-in help, Cargo-style diagnostics, structured logging, shell completions, and MCP support.
Note
ESM-only, targeting Node.js >= 24.
- Signature-driven TypeScript types - Write
"deploy <env> [region] -- [extra...]"and getctx.argsinferred as{ env: string; region: string | undefined; extra: string[] }withoutas const. Typos inargsorflagsoption keys fail at compile time. - Standard Schema v1 everywhere - Pass Zod, Valibot, ArkType, or built-in
Types.Enum/Types.Integer/Types.Number/Types.String/Types.Regexdirectly totype. One schema drives runtime validation, TypeScript output types,--helpplaceholders, shell completion candidates, and MCP JSON Schemas. - Editorial
--help, typo hints, andctx.logbuilt in - Every CLI gets aligned--helppages (command/flag groups, examples, notes), Cargo-style error diagnostics with Levenshtein suggestions (buid->'build',--prt->'--port'), and a structured logger (step,box,spinner,task, streaming.line(), and JSON mode). - One API for apps and plugins -
clerc()andplugin()share.command(),.flags(),.groups(),.middleware(),.onError(), and.use(). Onion middlewares inject typedctx.statevianext({ db }), clean up intry / finally, and support{ enforce: "pre" | "post" }. - Shell completions and MCP server from one definition - Add
@clerc/plugin-completionsfor Bash, Zsh, Fish, and PowerShell tab-completion. Add@clerc/plugin-mcpand<cli> mcpexposes your commands as Model Context Protocol tools and help pages as resources for AI agents. - In-memory testing without
process.exit()-await cli.run(["deploy", "prod"], { terminal: createMemoryTerminal() })capturesstdout,stderr, andexitCodein memory without mutating globals or killing your test runner.
pnpm add clercimport { Types, clerc } from "clerc";
const cli = clerc({
name: "acme",
version: "2.0.0",
description: "Build and deploy TypeScript services",
});
cli.command("deploy <env> [region]", {
aliases: "d",
description: "Deploy service to target environment",
args: {
env: {
type: Types.Enum("staging", "prod"),
description: "Target environment",
},
region: {
default: "us-east-1",
description: "Cloud region",
},
},
flags: {
port: {
type: Types.Integer({ min: 1, max: 65_535 }),
short: "p",
default: 3000,
env: "PORT",
description: "Server port",
},
dryRun: {
type: Boolean,
short: "n",
description: "Preview without deploying",
},
},
run(ctx) {
// ctx.args.env -> "staging" | "prod"
// ctx.args.region -> string
// ctx.flags.port -> number
ctx.log.step(
`Deploying to ${ctx.args.env} (${ctx.args.region})`,
`port=${ctx.flags.port}`,
);
if (ctx.flags.dryRun) {
ctx.log.box.info("Dry run", "No remote resources were modified.");
} else {
ctx.log.success("Deployment complete");
}
},
});
await cli.runMain();Running acme deploy --help renders:
acme deploy v2.0.0 alias: d
Deploy service to target environment
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
USAGE acme deploy [options] <env> [region]
ARGUMENTS
<env> Target environment [staging|prod]
[region] Cloud region [default: us-east-1]
OPTIONS
-p, --port <1..65535> Server port [default: 3000] [env: PORT]
-n, --dry-run Preview without deploying
GLOBAL OPTIONS
-q, --quiet Suppress non-error output
-v, --verbose Enable verbose debug logging
-h, --help Show help
Mistyping a command or option prints actionable diagnostics:
$ acme deply
error unknown command 'deply'
tip a similar command exists: 'deploy'
For more information, try 'acme --help'.
Plugins encapsulate flags, subcommands, and middlewares, then compose onto any CLI with full type inference:
import { completionsPlugin } from "@clerc/plugin-completions";
import { mcpPlugin } from "@clerc/plugin-mcp";
import { clerc, plugin } from "clerc";
const dbPlugin = plugin("acme:db")
.flags({
databaseUrl: {
type: String,
env: "DATABASE_URL",
default: "sqlite://local.db",
},
})
.middleware(async (ctx, next) => {
const db = await connectDb(ctx.flags.databaseUrl);
try {
return await next({ db });
} finally {
await db.close();
}
});
const cli = clerc({ name: "acme", version: "2.0.0" })
.use(dbPlugin)
.use(completionsPlugin()) // adds 'acme completions <shell>'
.use(mcpPlugin()); // adds 'acme mcp' stdio server
cli.command("users list", {
meta: { mcp: { readOnly: true } },
async run(ctx) {
// ctx.state.db is typed from dbPlugin
const users = await ctx.state.db.query("SELECT id, email FROM users");
ctx.log.info(`Found ${users.length} users`);
},
});import { createMemoryTerminal } from "clerc";
import { expect, test } from "vitest";
test("previews staging deploy", async () => {
const terminal = createMemoryTerminal({ env: { PORT: "8080" } });
const result = await cli.run(["deploy", "staging", "-n"], { terminal });
expect(result.ok).toBe(true);
expect(terminal.stdoutText()).toContain("port=8080");
});| Package | Description |
|---|---|
clerc |
Core framework: POSIX argv parser, clerc, plugin, help renderer, diagnostics, built-in -v/-q verbosity, Standard Schema v1, and Terminal. |
@clerc/logger |
CLI logger with aligned word labels, node:util object/error formatting, streaming .line(), box, spinner, task, and JSON output. |
@clerc/theme |
Zero-dependency semantic ANSI theme and terminal color/Unicode detection. |
@clerc/plugin-completions |
Zero-dependency shell completions for Bash, Zsh, Fish, and PowerShell with async context-aware complete(). |
@clerc/plugin-mcp |
Zero-dependency MCP stdio server exposing CLI commands as tools and help pages as resources. |
@clerc/plugin-update-notifier |
Zero-dependency background npm update checker and banner renderer. |
- Documentation: clerc.so1ve.dev
- Examples:
examples/acme(full v2 tour) Β·examples/loggerΒ·examples/bumppΒ·examples/greeting
