# @visulima/cerebro

> A delightful toolkit for building cross-runtime CLIs for Node.js and Bun.

Latest version **4.0.2** (published 2026-11-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @visulima/cerebro
pnpm add @visulima/cerebro
yarn add @visulima/cerebro
bun add @visulima/cerebro
```

## Health

**Score 65/100 (B)** — status: active.

Positive: esm support; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 4.0.2 |
| Published | 2026-11-11 |
| First published | 2024-03-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | ^22.14.0 \|\| >=24.10.0 |
| Dependencies | 3 |
| Unpacked size | 490.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 59 |
| Author | Daniel Bannert |
| Maintainers | prisis |
| Keywords | ansi, args, argument, argv, clap, class, cli, cli-app, command, command line apps, command-line-usage, commander, flag, gluegun, interactive, line, meow, minimist, nopt, oclif, option, options, opts, parser, terminal, visulima, yargs |

## Links

- npm: https://www.npmjs.com/package/@visulima/cerebro
- Repository: https://github.com/visulima/visulima
- Homepage: https://visulima.com/packages/cerebro
- Issues: https://github.com/visulima/visulima/issues
- Funding: https://github.com/sponsors/prisis
- npm.io page: https://npm.io/package/@visulima/cerebro

## Dependencies (3)

- [@visulima/tabular](https://npm.io/package/@visulima/tabular.md) 4.1.0
- [@visulima/colorize](https://npm.io/package/@visulima/colorize.md) 3.0.0
- [fastest-levenshtein](https://npm.io/package/fastest-levenshtein.md) ^1.0.16

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 4.0.2 (latest) — 2026-11-11
- 3.0.0-alpha.33 (alpha) — 2026-06-30
- 4.0.1 — 2026-10-24
- 4.0.0 — 2026-10-15
- 3.1.3 — 2026-10-13
- 3.1.2 — 2026-09-21
- 3.1.1 — 2026-09-03
- 3.1.0 — 2026-08-19
- 3.0.6 — 2026-08-08
- 3.0.5 — 2026-08-02
- 3.0.4 — 2026-07-27
- 3.0.3 — 2026-07-27
- 3.0.2 — 2026-07-26
- 3.0.1 — 2026-07-26
- 3.0.0 — 2026-07-03
- … 153 more at https://npm.io/package/@visulima/cerebro/versions

## README

<!-- START_PACKAGE_OG_IMAGE_PLACEHOLDER -->

<a href="https://www.anolilab.com/open-source" align="center">

  <img src="__assets__/package-og.svg" alt="cerebro" />

</a>

<h3 align="center">A delightful toolkit for building cross-runtime CLIs for Node.js and Bun.</h3>

<!-- END_PACKAGE_OG_IMAGE_PLACEHOLDER -->

<br />

<div align="center">

[![typescript-image][typescript-badge]][typescript-url]
[![mit licence][license-badge]][license]
[![npm downloads][npm-downloads-badge]][npm-downloads]
[![Chat][chat-badge]][chat]
[![PRs Welcome][prs-welcome-badge]][prs-welcome]

</div>

---

<div align="center">
    <p>
        <sup>
            Daniel Bannert's open source work is supported by the community on <a href="https://github.com/sponsors/prisis">GitHub Sponsors</a>
        </sup>
    </p>
</div>

---

## Install

```sh
npm install @visulima/cerebro
```

```sh
yarn add @visulima/cerebro
```

```sh
pnpm add @visulima/cerebro
```

## Usage

```ts
import { Cerebro } from "@visulima/cerebro";

// Create a CLI runtime
const cli = new Cerebro("my-cli");

// Add commands with options and arguments
cli.addCommand({
    name: "build",
    description: "Build the project",
    options: [
        {
            name: "output",
            alias: "o",
            type: String,
            description: "Output directory",
            defaultValue: "dist",
        },
        {
            name: "production",
            alias: "p",
            type: Boolean,
            description: "Build for production",
        },
        {
            name: "watch",
            alias: "w",
            type: Boolean,
            description: "Watch for changes",
        },
    ],
    argument: {
        name: "target",
        description: "Build target (optional)",
        type: String,
    },
    execute: ({ options, argument, logger, env }) => {
        const target = argument[0] || "all";
        const outputDir = options.output;

        logger.info(`Building target: ${target}`);
        logger.info(`Output directory: ${outputDir}`);

        if (options.production) {
            logger.info("Production build enabled");
        }

        if (options.watch) {
            logger.info("Watch mode enabled");
        }

        if (env.NODE_ENV) {
            logger.info(`Environment: ${env.NODE_ENV}`);
        }
    },
});

// Add another command with environment variables
cli.addCommand({
    name: "deploy",
    description: "Deploy the application",
    env: [
        {
            name: "DEPLOY_ENV",
            description: "Deployment environment",
            type: String,
            defaultValue: "staging",
        },
        {
            name: "API_KEY",
            description: "API key for deployment",
            type: String,
        },
    ],
    execute: ({ env, logger }) => {
        logger.info(`Deploying to ${env.DEPLOY_ENV}`);
        if (env.API_KEY) {
            logger.info("Using provided API key");
        }
    },
});

await cli.run();
```

Now you can run your CLI with `node index.js` (or `bun index.js`). Here are some example usages:

```bash
# Show help
node index.js --help

# Build with default options
node index.js build

# Build specific target with custom output
node index.js build --output ./build client

# Production build with watch mode
node index.js build --production --watch

# Deploy (uses environment variables)
node index.js deploy
```

You should see help output and command execution based on the options provided:

![Cli Output](./__assets__/cli_output.png)

### Typed commands with `defineCommand`

`options` and `env` can also be given as records keyed by name. Wrap the command in
`defineCommand` and the toolbox types are inferred from those definitions — no separate
options interface to keep in sync, and a renamed option becomes a compile error rather
than a silent `undefined`. `arguments` stays an array, because slot order is
load-bearing, and its names stay typed without an `as const`:

```ts
import { Cerebro, defineCommand } from "@visulima/cerebro";

const build = defineCommand({
    name: "build",
    description: "Build the project",
    arguments: [{ name: "target", type: String, required: true, description: "Build target" }],
    options: {
        "output-dir": { alias: "o", type: String, defaultValue: "dist" },
        production: { alias: "p", type: Boolean },
        tags: { type: String, multiple: true },
    },
    env: {
        NODE_ENV: { type: String },
    },
    execute: ({ args, env, logger, options }) => {
        args.target; // string                — required
        options.outputDir; // string                — has a defaultValue
        options.production; // boolean | undefined
        options.tags; // string[] | undefined
        env.nodeEnv; // string | undefined

        logger.info(`Building ${args.target} into ${options.outputDir}`);
    },
});

new Cerebro("my-cli").addCommand(build);
```

The array form of `options`/`env` and the single `argument` field keep working unchanged —
`defineCommand`, the record forms, and `arguments` are all purely additive.

## Lazy commands

For CLIs with many subcommands or heavy per-command dependencies, you can defer importing each handler until the command is actually invoked. Declare the metadata inline and point `loader` at a dynamic `import()`:

```ts
// commands/build.ts
export default ({ logger, options }) => {
    logger.info(`Building to ${options.output ?? "dist"}`);
};
```

```ts
// index.ts
cli.addCommand({
    name: "build",
    description: "Build the project",
    options: [{ name: "output", alias: "o", type: String, description: "Output directory" }],
    argument: { name: "target", type: String },
    loader: () => import("./commands/build"),
});
```

`loader` is a zero-argument function that returns a promise resolving to a module — typically `() => import("./path")`. The handler module's **default export** is the toolbox-receiving function. Help, completion, and option validation work entirely from the metadata you declared on `addCommand` and never trigger the loader. The first invocation of the command imports the module; subsequent calls reuse the cached handler.

A command may declare either `execute` or `loader`, but not both. If a loader rejects or returns a module without a default-exported function, a `CommandLoaderError` is thrown.

## Toolbox API

When your command's `execute` function is called, it receives a toolbox object with various utilities and context. Here's what you can access:

### Core Properties

- **`logger`**: Logger instance for output (debug, info, warn, error). Verbosity-gated via `--quiet`/`--verbose`/`--debug`.
- **`console`**: Alias for `logger`. Use it when porting goke-style code or when a `console`-named parameter reads more naturally.
- **`options`**: Parsed command-line options (camelCase keys)
- **`argument`**: Array of positional arguments
- **`args`**: Named positional arguments (camelCase keys) from the command's `arguments: [ ... ]` definitions; empty when none are declared
- **`env`**: Environment variables (camelCase keys) processed from the command's `env: [...]` definitions
- **`fs`**: Injected filesystem adapter (subset of `node:fs/promises`). Swap via `CliOptions.fs` for tests or sandboxed runtimes.
- **`process`**: Runtime snapshot — `cwd`, `env`, `argv`, `stdin`, `exit`, `platform`, `arch`. Prefer this over the global `process` so commands stay portable across Node, Bun, and mocked test runtimes.
- **`runtime`**: Reference to the CLI instance
- **`argv`**: Original command-line arguments array

### Example Usage

```ts
cli.addCommand({
    name: "example",
    description: "Example command showing toolbox usage",
    options: [
        { name: "verbose", alias: "v", type: Boolean, description: "Verbose output" },
        { name: "count", alias: "c", type: Number, description: "Count value", defaultValue: 1 },
    ],
    argument: {
        name: "input",
        description: "Input file",
        type: String,
    },
    env: [{ name: "DEBUG", type: Boolean, description: "Debug mode" }],
    execute: ({ logger, options, argument, env, runtime, argv }) => {
        // Use logger for output
        logger.info("Command started");

        // Access parsed options
        if (options.verbose) {
            logger.debug(`Count: ${options.count}`);
        }

        // Access positional arguments
        if (argument.length > 0) {
            logger.info(`Processing file: ${argument[0]}`);
        }

        // Access environment variables
        if (env.debug) {
            logger.debug("Debug mode enabled");
        }

        // Access CLI instance
        logger.info(`CLI name: ${runtime.cliName}`);

        // Access original argv
        logger.debug(`Full command: ${argv.join(" ")}`);
    },
});
```

## Runtime Injection

Commands receive an injected `{ fs, console, process }` context on the toolbox. Prefer reading from these over reaching for `node:fs/promises`, the global `console`, or the global `process` — commands written against the injected context stay testable, portable across Node/Bun, and ready to run inside sandboxed environments like MCP servers.

```ts
import { Cerebro } from "@visulima/cerebro";

const cli = new Cerebro("acme");

cli.addCommand({
    name: "login",
    description: "Save an auth token",
    options: [{ name: "token", type: String, description: "API token" }],
    execute: async ({ fs, console, process, options }) => {
        await fs.mkdir(".acme", { recursive: true });
        await fs.writeFile(".acme/auth.json", JSON.stringify({ token: options.token }), "utf8");
        console.log("saved credentials in", process.cwd);
    },
});

await cli.run();
```

### Overriding the runtime

Pass any of the new `CliOptions` to swap the runtime context. Each override defaults to a sensible host value:

| Option          | Default                      | Used for                                                                           |
| --------------- | ---------------------------- | ---------------------------------------------------------------------------------- |
| `fs`            | `node:fs/promises` adapter   | Filesystem operations from `toolbox.fs`                                            |
| `exit`          | Runtime-agnostic exit helper | `toolbox.process.exit`                                                             |
| `env`           | Host `process.env`           | `toolbox.process.env` (does **not** affect `toolbox.env`)                          |
| `stdin`         | `""` (empty string)          | `toolbox.process.stdin`                                                            |
| `cwd`           | Runtime cwd                  | `toolbox.process.cwd`                                                              |
| `logger`        | Verbosity-aware console shim | `toolbox.logger` and `toolbox.console`                                             |
| `maxArguments`  | A very generous cap          | Upper bound on argv tokens; set `Number.POSITIVE_INFINITY` to disable entirely     |
| `strictOptions` | `false`                      | Reject unknown `--options` (before `--`) with a did-you-mean instead of swallowing |

```ts
const exitSpy = vi.fn();
const fakeFs = new InMemoryFs();

const cli = new Cerebro("acme", {
    cwd: "/virtual/project",
    fs: fakeFs,
    exit: exitSpy,
    env: { NODE_ENV: "test" },
    stdin: "y\n",
});
```

### Testing with mocked runtime

The runtime overrides remove the need for `vi.spyOn(process, "exit")` or `vi.spyOn(console, "log")` in command tests. Pass mocks at CLI construction; assert on them directly.

```ts
import { describe, expect, test, vi } from "vitest";
import { Cerebro } from "@visulima/cerebro";

describe("deploy command", () => {
    test("exits with code 2 when env is missing", async () => {
        const exit = vi.fn();
        const calls: string[] = [];
        const logger = {
            log: (...args: unknown[]) => calls.push(String(args[0])),
            info: () => {},
            warn: () => {},
            error: () => {},
            debug: () => {},
        };

        const cli = new Cerebro("acme", { argv: ["deploy"], exit, logger });
        cli.addCommand({
            name: "deploy",
            execute: ({ console, process, options }) => {
                if (!options.env) {
                    console.log("missing --env");
                    process.exit(2);
                }
            },
        });

        await cli.run({ shouldExitProcess: false });

        expect(exit).toHaveBeenCalledWith(2);
        expect(calls).toStrictEqual(["missing --env"]);
    });
});
```

### `cli.clone(options?)`

Creates an independent CLI sharing the same command definitions. The clone has its own commands map, global options, default-command setting, and plugin manager — so adding commands or changing options on the clone never mutates the original. Primarily useful in tests to run the same CLI with different `argv`/`exit`/`fs` overrides without rebuilding the command tree.

```ts
const cli = new Cerebro("acme");
cli.addCommand({ name: "build", execute: ({ console }) => console.log("building") });

// In tests: clone with mocked exit + captured argv
const isolatedExit = vi.fn();
const isolated = cli.clone({ argv: ["build"], exit: isolatedExit });
await isolated.run({ shouldExitProcess: false });
```

### `cli.getAction(commandName)`

Returns the resolved `execute` function for a registered command. For lazy commands defined with `loader`, the module is loaded once and cached. Supports space-separated nested command paths.

```ts
const cli = new Cerebro("acme");

cli.addCommand({
    name: "deploy",
    execute: ({ console, options }) => console.log("deploying to", options.env),
});

// Call the action directly with a synthesized toolbox — no argv parsing.
const action = await cli.getAction("deploy");
await action({ console: fakeConsole, options: { env: "staging" } } as never);
```

Use this when you want to unit-test a command action in isolation without going through `run()`'s full lifecycle (plugin init, exception handlers, exit). For end-to-end tests that exercise argv parsing and lifecycle hooks, prefer `cli.clone(...).run(...)` instead.

## Validating option values with `choices`

Restrict an option to a fixed set of values, validated at parse time (like commander's `.choices()` / yargs `choices`). For `multiple` options every provided value must be a member of the set. An invalid value throws an `InvalidChoiceError` with a hint listing the allowed values.

```ts
cli.addCommand({
    name: "fmt",
    options: [{ name: "format", type: String, choices: ["json", "yaml", "table"] }],
    execute: ({ options }) => {
        // options.format is guaranteed to be one of json | yaml | table
    },
});

// `mycli fmt --format xml` →  Invalid value "xml" for option "format". Allowed values: json, yaml, table
```

## Strict unknown-option handling

By default, unknown long options on commands that accept a positional `argument` are routed to `toolbox.rawUnknown` (the passthrough buffer) rather than rejected. Set `strictOptions: true` to fail fast on a typo'd flag — tokens after a `--` separator are always preserved as passthrough.

```ts
const cli = new Cerebro("mycli", { argv: ["build", "app", "--produciton"], strictOptions: true });
// → throws UnknownOptionError: Found unknown option: --produciton  (Did you mean: --production?)

// Tokens after `--` are still passthrough, even in strict mode:
// `mycli build app -- --produciton`  →  toolbox.rawUnknown === ["--produciton"]
```

## Built-in Commands

Cerebro comes with several built-in commands that are automatically available:

### Help Command

The help command is automatically added to your CLI and provides usage information for all commands.

```bash
my-cli help
my-cli help <command>
```

### Version Command

Display version information for your CLI.

```ts
import { Cerebro } from "@visulima/cerebro";
import versionCommand from "@visulima/cerebro/command/version";

const cli = new Cerebro("my-cli", {
    packageName: "my-cli",
    packageVersion: "1.0.0",
});

cli.addCommand(versionCommand);

await cli.run();
```

```bash
my-cli version
```

### Readme Generator Command

Generate README documentation for your CLI commands.

```ts
import { Cerebro } from "@visulima/cerebro";
import readmeCommand from "@visulima/cerebro/command/readme-generator";

const cli = new Cerebro("my-cli");
cli.addCommand(readmeCommand);

await cli.run();
```

```bash
my-cli readme-generator
```

## Shell Completions

Cerebro supports intelligent shell autocompletions for **bash**, **zsh**, **fish**, and **powershell** through the optional `@bomb.sh/tab` integration. The completion system automatically detects your current shell and runtime, providing context-aware suggestions for commands, options, and arguments.

### Installation

To enable completions, first install the optional peer dependency:

```sh
pnpm add @bomb.sh/tab
```

Or with other package managers:

```sh
npm install @bomb.sh/tab
yarn add @bomb.sh/tab
```

### Adding Completion Command

Import and add the completion command to your CLI. The completion command supports two options:

- **`--shell`**: Shell type (bash, zsh, fish, powershell) - auto-detected by default
- **`--runtime`**: JavaScript runtime (node, bun) - auto-detected by default

```ts
import { Cerebro } from "@visulima/cerebro";
import completionCommand from "@visulima/cerebro/command/completion";

const cli = new Cerebro("my-cli");

// Add your commands with options
cli.addCommand({
    name: "build",
    description: "Build the project",
    options: [
        {
            name: "output",
            alias: "o",
            type: String,
            description: "Output directory",
        },
        {
            name: "production",
            alias: "p",
            type: Boolean,
            description: "Production build",
        },
    ],
    execute: ({ options }) => {
        console.log(`Building to ${options.output || "dist"}`);
    },
});

// Add completion command
cli.addCommand(completionCommand);

await cli.run();
```

### Generating Completion Scripts

Users can generate completion scripts for their shell. The completion command will automatically detect your shell and runtime, but you can override them if needed:

```bash
# Auto-detect shell and runtime (recommended)
my-cli completion > ~/.my-cli-completion.sh
echo 'source ~/.my-cli-completion.sh' >> ~/.bashrc  # or ~/.zshrc

# Explicitly specify shell
my-cli completion --shell=zsh > ~/.my-cli-completion.zsh
my-cli completion --shell=bash > ~/.my-cli-completion.bash
my-cli completion --shell=fish > ~/.config/fish/completions/my-cli.fish
my-cli completion --shell=powershell > ~/.my-cli-completion.ps1

# Override runtime detection
my-cli completion --runtime=node --shell=zsh > ~/.my-cli-completion.zsh
```

### Setup Instructions

**Bash:**

```bash
my-cli completion --shell=bash > ~/.my-cli-completion.bash
echo 'source ~/.my-cli-completion.bash' >> ~/.bashrc
source ~/.bashrc
```

**Zsh:**

```bash
my-cli completion --shell=zsh > ~/.my-cli-completion.zsh
echo 'source ~/.my-cli-completion.zsh' >> ~/.zshrc
source ~/.zshrc
```

**Fish:**

```bash
my-cli completion --shell=fish > ~/.config/fish/completions/my-cli.fish
```

**PowerShell:**

```powershell
my-cli completion --shell=powershell > $PROFILE.CurrentUserAllHosts
. $PROFILE.CurrentUserAllHosts
```

After setting up, users can press `TAB` to autocomplete:

- Command names
- Option flags (both long `--option` and short `-o`)
- Option values (when applicable)
- Subcommands

### Troubleshooting

If completions don't work:

1. Ensure `@bomb.sh/tab` is installed
2. Verify the completion script was sourced in your shell profile
3. Try restarting your shell or running `source ~/.bashrc` (or equivalent)
4. Check that your CLI name matches the completion script filename

## Supported Runtimes

Cerebro supports multiple JavaScript runtimes:

- **Node.js**: >=20.19 <=25.x (follows [Node.js' release schedule](https://github.com/nodejs/release#release-schedule))
- **Bun**: 1.0+

The library uses runtime-agnostic APIs to ensure compatibility across all supported runtimes. Here's [a post on why we think tracking Node.js releases is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).

## Contributing

If you would like to help take a look at the [list of issues](https://github.com/visulima/visulima/issues) and check our [Contributing](.github/CONTRIBUTING.md) guild.

> **Note:** please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

## Credits

- [Daniel Bannert](https://github.com/prisis)
- [All Contributors](https://github.com/visulima/visulima/graphs/contributors)

## About

### Related Projects

- [oclif](https://oclif.io) - The Open CLI Framework
- [gluegun](https://infinitered.github.io/gluegun/#/) - A delightful toolkit for building TypeScript-powered command-line apps.
- [meow](https://www.npmjs.com/package/meow) - CLI app helper
- [commander.js](https://github.com/tj/commander.js) - node.js command-line interfaces made easy
- [yargs](https://www.npmjs.com/package/yargs) - yargs the modern, pirate-themed successor to optimist.

## Made with ❤️ at Anolilab

This is an open source project and will always remain free to use. If you think it's cool, please star it 🌟. [Anolilab](https://www.anolilab.com/open-source) is a Development and AI Studio. Contact us at [hello@anolilab.com](mailto:hello@anolilab.com) if you need any help with these technologies or just want to say hi!

## License

The visulima package is open-sourced software licensed under the [MIT][license]

<!-- badges -->

[license-badge]: https://img.shields.io/npm/l/@visulima/cerebro?style=for-the-badge
[license]: https://github.com/visulima/visulima/blob/main/LICENSE
[npm-downloads-badge]: https://img.shields.io/npm/dm/@visulima/cerebro?style=for-the-badge
[npm-downloads]: https://www.npmjs.com/package/@visulima/cerebro
[prs-welcome-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=for-the-badge
[prs-welcome]: https://github.com/visulima/visulima/blob/main/.github/CONTRIBUTING.md
[chat-badge]: https://img.shields.io/discord/902465130518949899.svg?style=for-the-badge
[chat]: https://discord.gg/eajEZvk2PG
[typescript-badge]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: https://www.typescriptlang.org/

---
_Source: https://npm.io/package/@visulima/cerebro · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
