Skip to content

CLI Commands

Learn to create, organize, and execute CLI commands with CLI-John.

Basic Command Creation

Create simple commands:

typescript
import { CLI } from "@briklab/lib/cli-john";
import * as process from "node:process";

const cli = new CLI(process);

// Create a command
cli.command("greet").on("command", ({ commandArgs }) => {
  const name = commandArgs[0] || "World";
  console.log(`Hello, ${name}!`);
});

// Create another command  
cli.command("calculate").on("command", ({ commandArgs }) => {
  const sum = Number(commandArgs[0]) + Number(commandArgs[1]);
  console.log(`Result: ${sum}`);
});

// Run the CLI
cli.run();

Live demo of CLI commands:

Console
No logs yet.

Command Hierarchy

Organize commands into groups:

typescript
import { CLI } from "@briklab/lib/cli-john";
import * as process from "node:process";

const cli = new CLI(process);

// User commands
cli.command("user:create").on("command", ({ commandArgs }) => {
  console.log(`Creating user: ${commandArgs[0]}`);
});

cli.command("user:list").on("command", () => {
  console.log("Users: John, Jane, Bob");
});

cli.command("user:delete").on("command", ({ commandArgs }) => {
  console.log(`Deleting user ${commandArgs[0]}`);
});

// Database commands
cli.command("db:migrate").on("command", () => {
  console.log("Running migrations...");
});

cli.command("db:seed").on("command", () => {
  console.log("Seeding database...");
});

cli.run();

Option Handling

Add flags and options to commands:

typescript
import { CLI } from "@briklab/lib/cli-john";
import * as process from "node:process";

const cli = new CLI(process);

cli.command("build").on("command", ({ commandArgs, options }) => {
    const output = commandArgs[0] || "dist";
    const watch = options.some((opt) => opt.option === "watch");
    const minify = !options.some((opt) => opt.option === "no-minify");

    console.log(`Building to ${output}`);
    console.log(`Watch mode: ${watch}`);
    console.log(`Minify: ${minify}`);
});

cli.run();

Command Aliases

Create shortcuts for commands:

typescript
import { CLI } from "@briklab/lib/cli-john";
import * as process from "node:process";

const cli = new CLI(process);

cli.command("dev").on("command", () => {
  console.log("Starting dev server...");
});

cli.command("development").on("command", () => {
  console.log("Starting dev server...");
});

cli.command("d").on("command", () => {
  console.log("Starting dev server...");
});

cli.command("build").on("command", () => {
  console.log("Building...");
});

cli.command("b").on("command", () => {
  console.log("Building...");
});

cli.command("test").on("command", () => {
  console.log("Running tests...");
});

cli.command("t").on("command", () => {
  console.log("Running tests...");
});

cli.run();

Next Steps