/clibuilder:build-clifrom clibuilder
Builds, extends, and tests command-line tools using clibuilder 12. Covers specs, typed arguments/options, subcommands, error handling, config files, plugins, and in-process testing.
Ships in clibuilder. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Requires Node.js 20.19 or newer. clibuilder 12 is ESM only.
npm install clibuilder
Each command created or changed goes through these steps in order. Read references/command-spec.md before step 1.
--format by name, also when it is the package-wide default.Changing an existing command. Before step 1, find its default format in three places: its spec, its tests (a test run with no --format, and the format it passes to testCommand), and the entry file's cli({ format }), which is toon when it sets none. When two of them disagree, or the spec names none and the tests and code disagree, do not pick one: change nothing about that command, report each place and the default it gives, and ask which is right. A spec that is missing is written from the command's current behavior, with the default the code gives.
A request for a test of an existing command takes the spec as its source: step 1 when there is no spec yet, then step 2.
name and version are required. .parse() exists once the CLI has a .default() or .command().
#!/usr/bin/env node
import { cli } from 'clibuilder'
cli({ name: 'greet', version: '1.0.0' })
.default({
arguments: [{ name: 'name', description: 'who to greet' }],
options: { loud: { description: 'shout it', alias: ['l'] } },
run(args) {
const message = `hello, ${args.name}`
return args.loud ? message.toUpperCase() : message
}
})
.parse(process.argv)
Declare run as a method, not an arrow function: this carries this.ui, this.config, this.cwd, this.context and this.format. Give every command, argument and option a description: help is generated from them. Every CLI answers --help, --version, --verbose, --silent and --format toon|json|text; do not declare a format option of your own.
Import t for typed values. run(args) gets the exact inferred types.
import { command, t } from 'clibuilder'
export const serve = command({
name: 'serve',
description: 'start the server',
arguments: [{ name: 'root', description: 'root folder', type: t.string() }],
options: {
port: { description: 'port', type: t.int({ min: 1, max: 65535 }), default: 8080, alias: ['p'] },
tag: { description: 'tags', type: t.array(t.string()).optional() }
},
run(args) {
// args.root: string, args.port: number, args.tag: string[] | undefined
}
})
t.string, t.number, t.int, t.boolean, t.literal, t.enum, t.array, t.custom, t.json. Chain .optional() or .check(fn)..optional() and no default is required.t.int({ min: 1 })), not in chained methods.--flag=false. Array options repeat: --tag a --tag b. An array argument is variadic.command() so args stays typed.cli({ name: 'app', version: '1.0.0' })
.command(serve)
.command({
name: 'repo',
description: 'manage repositories',
commands: [command({ name: 'create', description: 'create one', run() {} })]
})
.parse(process.argv)
A command needs run or commands. A group without run is a missing-command usage error when invoked bare.
The returned value is the answer. By default (output: 'axi') it prints once on stdout in the caller's --format, and this.ui.info/warn/error/debug write diagnostics to stderr. Shape that output for agents with the axi skill. Return plain objects, arrays or strings, or use helpers:
import { document, empty, result } from 'clibuilder'
return result({ items }, { help: 'Run `app show <id>` for one' })
return empty('0 open tasks found', { help: 'Run `app add` to add one' })
return document(markdown) // verbatim, not encoded
return result({ exists }, { exitCode: 1 }) // answer plus non-zero exit
result() and document() accept exitCode from 0 to 255. empty() always succeeds.
Exit codes: 0 success, 1 could not complete, 2 called incorrectly. Import exitCodes for the names. Usage errors (unknown option, missing argument, bad value, a bare group) exit 2 on their own. To fail from run(), throw CliError:
import { CliError } from 'clibuilder'
throw new CliError({
message: `no plugin named ${name}`,
code: 'not-found', // stable kebab-case: says which failure
details: { name }, // scalars, scalar lists, or lists of scalar records
help: 'run `app list` to see them', // string or string[]
exitCode: 4 // 1 to 255, default 1
})
Give every failure a stable code. Any other thrown error is treated as a bug and propagates out of parse().
Config is opt-in through .use(config()) from clibuilder/config; plugins need @clibuilder/plugins. When a command reads a config file or the CLI loads plugins, read references/config-and-plugins.md first.
Run a command in-process. No process is spawned and no config file is read.
import { testCommand } from 'clibuilder/testing'
const { result, messages, exitCode } = await testCommand(serve, 'serve ./public --port 9000')
expect(exitCode).toBeUndefined()
argv is the command line after the CLI name. Address nested commands by full path: 'repo create x'.messages is stdout and stderr together, in order, including the printed answer. exitCode is undefined unless the command failed.exitCode and messages, for example 2 and code: unknown-option.{ config: {...} } to stand in for the config file. Inject fakes through the command's context field, read as this.context.Docs: https://clibuilder.github.io/clibuilder/ (guides and API reference).
Before reporting done, check each of these against the result. Report any that fails.
cli() with name and version, and calls .parse(process.argv) after a .default() or .command().run is a method, not an arrow function.t builder, with constraints in the builder options.run() throws CliError with a kebab-case code; no run() calls process.exit.testCommand from clibuilder/testing and asserts exitCode.clibuilder, clibuilder/axi, clibuilder/config, clibuilder/testing or @clibuilder/plugins.--format as toon, json or text.--format and asserts an answer in the default its spec names.README.md (opens in a new tab)evals/check.mjs (opens in a new tab)evals/evals.yaml (opens in a new tab)evals/expected.json (opens in a new tab)evals/files/format-mismatch/package.json (opens in a new tab)evals/files/format-mismatch/src/cli.ts (opens in a new tab)evals/files/format-mismatch/src/list.md (opens in a new tab)evals/files/format-mismatch/src/list.test.ts (opens in a new tab)evals/files/format-mismatch/src/list.ts (opens in a new tab)evals/files/todo/package.json (opens in a new tab)references/command-spec.md (opens in a new tab)references/config-and-plugins.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
2 skills in this plugin
npx claudepluginhub clibuilder/clibuilder --plugin clibuilderOther plugins' skills for the same job