Skip to content

Getting Started with CLI-John

CLI-John is a Node.js command-line framework for building CLI applications with command routing and option parsing.

What is CLI-John?

CLI-John helps you build command-line applications by handling command routing, argument parsing, and formatted output.

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

const cli = new CLI(process);

cli.command("hello").on("command", ({ commandArgs }) => {
  console.log(`Hello, ${commandArgs[0] || "World"}!`);
});

cli.run();

Basic Usage

Creating a CLI Application

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

const cli = new CLI(process);

// Define a command
cli.command("echo").on("command", ({ commandArgs }) => {
  console.log(commandArgs.join(" "));
});

// Run the CLI
cli.run();

Command Handlers

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

const cli = new CLI(process);

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

// Command with multiple handlers
cli.command("status")
  .on("command", () => {
    console.log("Status: OK");
  });

cli.run();

Live Demo

Console
No logs yet.

Features

  • Command Routing: Define commands and their handlers
  • Event Listeners: Use .on() for command events
  • Argument Parsing: Access command arguments easily
  • Process Integration: Works with Node.js process
  • Option Parsing: Read --option values from the event payload

Module Requirements

  • Node.js 14+ (runtime environment)
  • @briklab/lib installed

Common Commands

ts
// Command with arguments
cli.command("greet").on("command", ({ commandArgs }) => {
  console.log(`Hello, ${commandArgs.join(" ")}`);
});

// Command with options (if supported)
cli.command("list").on("command", ({ commandArgs, options }) => {
  console.log(`Listing: ${commandArgs[0]}`);
});

// Handle options for a command
cli.command("list").on("command", ({ options }) => {
  const verbose = options.some((opt) => opt.option === "verbose");
  console.log(`Verbose mode: ${verbose}`);
});

Next Steps

Learn more about CLI-John: