CLI-John Examples
Real-world examples and common patterns for building CLI applications.
Package Manager Interface
Build a package manager CLI:
ts
import { CLI } from "@briklab/lib/cli-john";
import * as process from "node:process";
class PackageManager {
private cli: CLI;
constructor() {
this.cli = new CLI(process);
this.setupCommands();
}
private setupCommands(): void {
this.cli.command("install").on("command", ({ commandArgs }) => {
const pkg = commandArgs.join(" ");
console.log(`Installing package: ${pkg}`);
});
this.cli.command("remove").on("command", ({ commandArgs }) => {
const pkg = commandArgs.join(" ");
console.log(`Removing package: ${pkg}`);
});
this.cli.command("list").on("command", () => {
console.log("Installed packages:");
console.log(" @briklab/lib");
console.log(" @briklab/parsee");
});
this.cli.command("update").on("command", () => {
console.log("Updating all packages...");
});
}
run(): void {
this.cli.run();
}
}
const pm = new PackageManager();
pm.run();Development Server CLI
Build a dev server controller:
ts
import { CLI } from "@briklab/lib/cli-john";
import * as process from "node:process";
class DevServer {
private running = false;
private port = 3000;
private cli: CLI;
constructor() {
this.cli = new CLI(process);
this.setupCommands();
}
private setupCommands(): void {
this.cli.command("start").on("command", () => {
this.running = true;
console.log(`Server started on port ${this.port}`);
});
this.cli.command("stop").on("command", () => {
if (!this.running) {
console.error("Server not running");
return;
}
this.running = false;
console.log("Server stopped");
});
this.cli.command("restart").on("command", () => {
console.log("Restarting server...");
this.running = false;
this.running = true;
console.log(`Server restarted on port ${this.port}`);
});
this.cli.command("status").on("command", () => {
console.log(`Server: ${this.running ? "RUNNING" : "STOPPED"}`);
});
}
run(): void {
this.cli.run();
}
}
const server = new DevServer();
server.run();Live Demo - CLI Builder
Console
No logs yet.
Database CLI Tool
Build a database management tool:
ts
import { CLI } from "@briklab/lib/cli-john";
import * as process from "node:process";
class DatabaseCLI {
private cli: CLI;
constructor() {
this.cli = new CLI(process);
this.setupCommands();
}
private setupCommands(): void {
this.cli.command("migrate").on("command", ({ commandArgs }) => {
const version = commandArgs[0] || "latest";
console.log(`Running migrations to ${version}`);
});
this.cli.command("seed").on("command", () => {
console.log("Seeding database with test data");
});
this.cli.command("backup").on("command", () => {
console.log("Creating database backup...");
console.log("Backup created: backup-2024-01-01.sql");
});
this.cli.command("restore").on("command", ({ commandArgs }) => {
const file = commandArgs[0];
console.log(`Restoring from ${file}...`);
});
}
run(): void {
this.cli.run();
}
}
const db = new DatabaseCLI();
db.run();Test Runner CLI
Build a test execution interface:
ts
import { CLI } from "@briklab/lib/cli-john";
import * as process from "node:process";
class TestRunner {
private cli: CLI;
constructor() {
this.cli = new CLI(process);
this.setupCommands();
}
private setupCommands(): void {
this.cli.command("test").on("command", ({ commandArgs }) => {
const file = commandArgs[0] || "all";
console.log(`Running tests for ${file}`);
console.log("✓ 10 tests passed");
});
this.cli.command("test:watch").on("command", () => {
console.log("Running tests in watch mode");
console.log("Watching for changes...");
});
this.cli.command("test:coverage").on("command", () => {
console.log("Generating coverage report");
console.log("Coverage: 85%");
});
}
run(): void {
this.cli.run();
}
}
const testRunner = new TestRunner();
testRunner.run();Build System CLI
Create a build task runner:
ts
import { CLI } from "@briklab/lib/cli-john";
import * as process from "node:process";
class BuildSystem {
private cli: CLI;
constructor() {
this.cli = new CLI(process);
this.setupCommands();
}
private setupCommands(): void {
this.cli.command("build").on("command", () => {
console.log("Building application...");
console.log("✓ Compiled 42 files");
console.log("✓ Minified bundle (245kb)");
});
this.cli.command("clean").on("command", () => {
console.log("Cleaning build artifacts...");
console.log("✓ Removed dist/");
console.log("✓ Removed .cache/");
});
this.cli.command("dev").on("command", () => {
console.log("Starting development build");
console.log("Watching for changes...");
});
}
run(): void {
this.cli.run();
}
}
const build = new BuildSystem();
build.run();Next Steps
- Advanced Usage: Complex patterns
- Getting Started: Back to basics