A lightweight, typed CLI program framework for building command-driven tools with consistent help output and parameter parsing.
- Typed program model with
Program,Command,Argument, andOptiondefinitions. - Built-in parsing for required arguments and optional flags.
- Three parameter modes:
value,switch, andenum. - Automatic help output for global and command-specific options.
- Validation and errors via
ParameterErrorwhen arguments are invalid or duplicated. - Pluggable console powered by
@northern/consolefor consistent styled output.
npm install @northern/cliRun the included example:
npm run example -- greet --name "Freddy Krueger"
Note: The first -- is to indicate that what follows are parameters for the command being executed, not parameters for npm run.
We can also turn off color output by adding the --nocolor parameter:
npm run example -- greet --name "Freddy Krueger" --nocolor
import { Interpreter, ParameterType, Program } from '@northern/cli';
const program: Program = {
title: 'Acme Tool',
version: '1.2.3',
name: 'acme',
options: [
{
name: 'verbose',
description: 'Enable verbose output',
type: ParameterType.SWITCH,
directives: ['-v', '--verbose'],
example: '--verbose',
default: false,
},
],
commands: [
{
name: 'build',
description: 'Build the project',
arguments: [
{
name: 'source',
description: 'Source file',
type: ParameterType.VALUE,
directives: ['-s', '--source'],
example: '--source ./app.yaml',
},
],
options: [
{
name: 'mode',
description: 'Build mode',
type: ParameterType.ENUM,
directives: ['-m', '--mode'],
example: '--mode production',
values: ['development', 'production'],
default: 'development',
},
],
},
],
};const interpreter = new Interpreter(program, process.argv.slice(2));
if (!interpreter.init()) {
process.exit(1);
}
const command = interpreter.getCommand();
if (!command) {
interpreter.displayHelp(process.argv[2]);
process.exit(1);
}
const params = interpreter.getParameters(command);
if (!params) {
interpreter.displayCommandHelp(command);
process.exit(1);
}
const globalOptions = interpreter.getGlobalOptions() ?? {};
// Execute your command using parsed values
// e.g. runBuild(params, globalOptions)- Required arguments must be present or
getParameters()returnsnull. - Options default to their
defaultvalue when not specified. - Switch options (
ParameterType.SWITCH) resolve totrueif present. - Enum options (
ParameterType.ENUM) must match one of the configuredvalues. - Duplicate directives (e.g.
--verbose --verbose) trigger a parse error.
ParameterType.VALUE— a directive that requires a value (e.g.--config path).ParameterType.SWITCH— a boolean flag (e.g.--verbose).ParameterType.ENUM— a directive that accepts one of a predefined set of values.
Required parameter definition:
| Field | Type | Description |
|---|---|---|
name |
string |
Internal key used in parsed output. |
description |
string |
Human-readable help text. |
type |
ParameterType |
Parameter type. |
directives |
string[] |
Accepted flags (e.g. ['-s', '--source']). |
example |
string |
Example usage shown in help. |
values? |
string[] |
Allowed values for ENUM parameters. |
Optional parameter definition. Same as Argument plus:
| Field | Type | Description |
|---|---|---|
default |
unknown |
Default value when option is omitted. |
| Field | Type | Description |
|---|---|---|
name |
string |
Command name (e.g. build). |
description |
string |
Help summary. |
arguments? |
Argument[] |
Required parameters. |
options? |
Option[] |
Command-specific options. |
| Field | Type | Description |
|---|---|---|
title |
string |
Display title shown in the banner. |
version |
string |
Version string. |
name |
string |
Executable name for usage output. |
options |
Option[] |
Global options. |
commands |
Command[] |
Available commands. |
Record<string, unknown> containing parsed values keyed by Argument/Option name.
interface ICommand {
run(parameters: Parameters): Promise<unknown>
}
Use this to define an execution contract for command handlers.
Thrown internally when parsing fails (missing values, invalid enums, duplicate directives).
The interpreter returns null from getParameters()/getGlobalOptions() to preserve backward compatibility.
new Interpreter(program: Program, args: string[], console?: Console)
new Interpreter(program: Program, console?: Console, args?: string[])
Creates a parser instance. You can pass args (typically process.argv.slice(2)) and/or a custom console. Both argument orders are supported for backward compatibility.
init(): boolean
Displays general help when no arguments are provided and returns false.
getCommand(): Command | null
Returns the command matching the first argument (case-insensitive), or null.
getBanner(): string
Returns a formatted banner string with title and version.
Displays general help and optionally an “unknown command” message.
Shows command-specific usage, required arguments, options, and global options.
getParameters(command: Command): Parameters | null
Parses required arguments and command options. Returns null on validation failure.
getGlobalOptions(): Parameters | null
Parses global options only. Returns null on validation failure.
Replaces the internal console instance (useful for testing).
getConsole(): Console
Returns the current console instance (useful for testing).