CerebroAPI ReferenceCli

Cli

Last updated:

API: CLI

The main CLI class for creating and managing command-line applications.

Creating a CLI Instance

Constructor

new Cerebro(name: string, options?: CliOptions)

Parameters:

  • name - The CLI application name
  • options - Optional configuration

Example:

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

const cli = new Cerebro("my-app", {
    packageName: "my-app",
    packageVersion: "1.0.0",
    cwd: process.cwd(),
    logger: console,
    argv: process.argv.slice(2),
});

Factory Function

import { createCerebro } from "@visulima/cerebro";

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

CliOptions

interface CliOptions<T extends Console = Console> {
    argv?: ReadonlyArray<string>;
    cwd?: string;
    logger?: T;
    packageName?: string;
    packageVersion?: string;
}

Methods

addCommand()

Add a command to the CLI:

cli.addCommand(command: Command): this

Example:

cli.addCommand({
    name: "build",
    description: "Build the project",
    execute: ({ logger }) => {
        logger.info("Building...");
    },
});

addPlugin()

Add a plugin to extend functionality:

cli.addPlugin(plugin: Plugin): this

Example:

cli.addPlugin({
    name: "my-plugin",
    execute: (toolbox) => {
        toolbox.myFeature = () => {};
    },
});

run()

Run the CLI application:

cli.run(options?: CliRunOptions): Promise<void>

Options:

interface CliRunOptions {
    shouldExitProcess?: boolean; // Default: true
    autoDispose?: boolean; // Default: true
    [key: string]: any; // Additional options
}

Example:

await cli.run({
    shouldExitProcess: false, // Don't exit process
    autoDispose: false, // Don't cleanup
});

runCommand()

Execute a command programmatically:

cli.runCommand(commandName: string, options?: RunCommandOptions): Promise<unknown>

Options:

interface RunCommandOptions {
    argv?: string[];
    [key: string]: any;
}

Example:

await runtime.runCommand("build", {
    argv: ["--production"],
});

setDefaultCommand()

Set the default command when none is provided:

cli.setDefaultCommand(commandName: string): this

Example:

cli.setDefaultCommand("help");

setCommandSection()

Configure help output:

cli.setCommandSection(section: CommandSection): this

Example:

cli.setCommandSection({
    header: "My CLI v1.0.0",
    footer: "Visit https://example.com for more info",
});

getCliName()

Get the CLI application name:

cli.getCliName(): string

getCommands()

Get all registered commands:

cli.getCommands(): Map<string, Command>

getCwd()

Get the current working directory:

cli.getCwd(): string

getPackageName()

Get the package name:

cli.getPackageName(): string | undefined

getPackageVersion()

Get the package version:

cli.getPackageVersion(): string | undefined

getPluginManager()

Get the plugin manager instance:

cli.getPluginManager(): PluginManager

dispose()

Clean up resources:

cli.dispose(): void

Complete Example

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

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

cli.setCommandSection({
    header: "My Awesome CLI v1.0.0",
});

cli.setDefaultCommand("help");

cli.addCommand({
    name: "hello",
    execute: ({ logger }) => {
        logger.info("Hello!");
    },
});

await cli.run();
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues