Commands
Last updated:
Commands
Learn about Cerebro's command system and how to create powerful CLI commands.
Command Structure
A command is defined by its metadata and execution function:
{
name: string, // Command name
description: string, // Help text description
argument?: ArgumentDef, // Positional argument
options?: OptionDef[], // Named options/flags
env?: EnvDef, // Environment variables
commands?: Command[], // Nested subcommands
execute: (toolbox) => void // Execution function
}Basic Commands
Simple command with no arguments:
cli.addCommand({
name: "hello",
description: "Say hello",
execute: ({ logger }) => {
logger.log("Hello!");
},
});Arguments
Positional arguments that come after the command name:
cli.addCommand({
name: "greet",
description: "Greet someone",
argument: {
name: "name",
description: "Person to greet",
type: String,
required: true,
},
execute: ({ argument, logger }) => {
logger.log(`Hello, ${argument[0]}!`);
},
});Usage:
$ my-cli greet Alice
Hello, Alice!Options
Named options with values or flags:
cli.addCommand({
name: "build",
description: "Build project",
options: [
{
name: "output",
alias: "o",
description: "Output directory",
type: String,
defaultValue: "./dist",
},
{
name: "production",
alias: "p",
description: "Production build",
type: Boolean,
},
{
name: "minify",
description: "Minify output",
type: Boolean,
defaultValue: true,
},
],
execute: ({ options, logger }) => {
logger.log(`Building to ${options.output}`);
logger.log(`Production: ${options.production}`);
logger.log(`Minify: ${options.minify}`);
},
});Usage:
$ my-cli build --output ./build --production
Building to ./build
Production: true
Minify: trueOption Types
Cerebro supports multiple option types:
String Options
{
name: "config",
type: String,
defaultValue: "config.json",
}Boolean Flags
{
name: "verbose",
type: Boolean,
}Number Options
{
name: "port",
type: Number,
defaultValue: 3000,
}Negatable Options
Options that can be negated with --no- prefix:
{
name: "color",
type: Boolean,
defaultValue: true,
negatable: true, // Allows --no-color
}Usage:
$ my-cli build --color # color = true
$ my-cli build --no-color # color = falseRequired Options
Mark options as required:
cli.addCommand({
name: "deploy",
description: "Deploy application",
options: [
{
name: "environment",
alias: "e",
description: "Target environment",
type: String,
required: true, // Must be provided
},
],
execute: ({ options, logger }) => {
logger.log(`Deploying to ${options.environment}`);
},
});Error if not provided:
$ my-cli deploy
Error: Required option --environment is missingConflicting Options
Prevent incompatible options:
cli.addCommand({
name: "test",
description: "Run tests",
options: [
{
name: "watch",
type: Boolean,
conflicts: ["ci"], // Can't use with --ci
},
{
name: "ci",
type: Boolean,
conflicts: ["watch"], // Can't use with --watch
},
],
execute: ({ options }) => {
// ...
},
});Implied Options
Set default values for other options:
{
name: "production",
type: Boolean,
implies: {
minify: true, // Sets minify to true
sourcemap: false, // Sets sourcemap to false
},
}Environment Variables
Access typed environment variables:
cli.addCommand({
name: "deploy",
description: "Deploy application",
env: {
API_KEY: {
description: "API authentication key",
required: true,
},
API_URL: {
description: "API endpoint URL",
required: false,
defaultValue: "https://api.example.com",
},
},
execute: ({ env, logger }) => {
logger.log(`Deploying to ${env.API_URL}`);
logger.log(`Using key: ${env.API_KEY}`);
},
});Nested Commands
Create command hierarchies for better organization:
cli.addCommand({
name: "database",
alias: "db",
description: "Database management",
commands: [
{
name: "migrate",
description: "Run migrations",
options: [
{
name: "rollback",
type: Boolean,
description: "Rollback migrations",
},
],
execute: ({ options, logger }) => {
if (options.rollback) {
logger.log("Rolling back migrations...");
} else {
logger.log("Running migrations...");
}
},
},
{
name: "seed",
description: "Seed database",
execute: ({ logger }) => {
logger.log("Seeding database...");
},
},
],
});Usage:
$ my-cli db migrate
$ my-cli db migrate --rollback
$ my-cli db seedDefault Commands
Set a default command when no command is specified:
cli.setDefaultCommand("help");
// or with options
cli.addCommand({
name: "serve",
description: "Start server",
isDefault: true,
execute: ({ logger }) => {
logger.log("Starting server...");
},
});Command Sections
Organize help output with sections:
cli.addCommandSection({
name: "Database",
description: "Database management commands",
commands: ["db:migrate", "db:seed", "db:backup"],
});