CLI
The main class for building a CLI with @briklab/lib/cli-john.
Diagram
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.
| Argument | Type | Description |
|---|---|---|
process | NodeJS.Process | The process object from Node. |
options | object | Optional configuration. |
| Option | Type | Description |
|---|---|---|
warningLevel | `"silent" | "summary" |
protectionLevel | ProtectionLevel | Protection level for input validation. |
Properties
This class exposes no public properties.
Methods
.command(name: string)
| Argument | Type | Description |
|---|---|---|
name | string | The 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)
| Argument | Type | Description |
|---|---|---|
event | CLI.ValidEvent | The event name. Currently only "command" is valid. |
func | function | Callback 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.
commandArgsare all tokens until the first token that starts with--.- Options are parsed as
--optionfollowed 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.