Skip to content

CLI

The main class for building a CLI with @briklab/lib/cli-john.

Diagram

100%Ctrl + wheel to zoom

Usage

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

const cli = new CLI(process);

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

cli.run();

Constructor

ts
new CLI(
  process: NodeJS.Process,
  options?: {
    warningLevel?: "silent" | "summary" | "full";
    protectionLevel?: ProtectionLevel;
  }
)

Constructs a new CLI instance.

ArgumentTypeDescription
processNodeJS.ProcessThe process object from Node.
optionsobjectOptional configuration.
OptionTypeDescription
warningLevel`"silent""summary"
protectionLevelProtectionLevelProtection level for input validation.

Properties

This class exposes no public properties.

Methods

.command(name: string)

ArgumentTypeDescription
namestringThe name of the command.

Creates a new command with the given name. If a command with the same name already exists, it is replaced. Returns a CLI.Command instance.

.on(event: CLI.ValidEvent, func: ({ commandArgs, command, options }) => any)

ArgumentTypeDescription
eventCLI.ValidEventThe event name. Currently only "command" is valid.
funcfunctionCallback invoked when the event fires.

Registers a handler on the CLI itself. This runs for any matched command after parsing.

Event payload:

ts
{
  commandArgs: string[];
  command: string;
  options: { option: string; arguments: string[] }[];
}

.run()

Parses process.argv, matches the command, then runs registered handlers and flushes warnings.

Parsing rules:

  • The command name is the first token after the script path.
  • commandArgs are all tokens until the first token that starts with --.
  • Options are parsed as --option followed by any number of values until the next --option.

If no command is provided or the command is not found, run() returns early without executing handlers.