icore
v3.0.2
Published
Typed CLI command mechanics for Node.js that keep argument parsing separate from application logic
Maintainers
Readme
icore
icore is a TypeScript library for building command-line interfaces in Node.js® applications. It helps an application define commands, parse arguments, run the selected command, and write text, table, JSON, or CSV output.
The library provides the shared command-line mechanics. The application provides its API calls, configuration, business rules, help text, and interactive terminal behavior.
Installation
To use icore in your project, run:
npm install icoreQuick Start
Create commands, collect them in a command registry, and pass the registry to createTerminalApp():
import { createCommand, createTerminalApp } from 'icore';
const command = createCommand();
const commands = command.registry([
command.define({
path: ['hello'],
options: {
name: {
type: 'string',
default: 'world'
},
uppercase: {
type: 'boolean'
}
},
handle({ options }) {
const greeting = `Hello, ${options.name}!`;
return `${options.uppercase ? greeting.toUpperCase() : greeting}\n`;
}
})
] as const);
const app = createTerminalApp({ commands });
async function main(args: readonly string[]): Promise<void> {
process.exitCode = await app.run(args, undefined, {
strict: true
});
}
void main(process.argv.slice(2));After building the application:
$ node dist/cli.js hello
Hello, world!
$ node dist/cli.js hello --name Alice --uppercase
HELLO, ALICE!The options object describes arguments accepted after hello. The handle function receives their parsed values.
app.run(...) validates the arguments, runs the selected command, formats its result, and writes it to the terminal. With strict: true, the command name and any subcommands must come before options: users get --id 42.
Choose An API
| What the application needs | Start with | Example |
| --- | --- | --- |
| Run a command immediately | app.run(...) | Quick Start |
| Handle global flags or create and close resources | app.prepare(...) and app.commands.run(...) | Set Up Command Execution And Cleanup |
| Define commands and their options | createCommand(), createCommand.withTypes(), or createCommands() | Define Command Options |
| Prepare a command before creating application services | commands.prepare() and commands.run() | Prepare And Run Commands Yourself |
| Format JSON, table, and CSV with shared fields or separate data | createPresentation() for shared fields; renderJson(), renderTextTable(), or renderCsv() for separate data | Format And Write Output |
| Write directly to standard output or standard error | createOutput() | Write Output |
| Parse arguments or resolve a command directly | parseArgv(), resolveCommand(), and related exports | Direct APIs |
app.run(args, context) passes the application's context to the selected handler. When commands in a registry require different context types, TypeScript checks context against their union independently of args. For example, if users requires userId and projects requires projectId, app.run(['users'], { projectId: 'p42' }) can compile. To check the pairing, first get the selected command with const prepared = await app.prepare(args). Inside if (isPreparedCommandName(prepared, 'users')), app.runPrepared(prepared, context) requires the users context.
Errors
IcoreError describes invalid command lines and invalid command definitions. Use isIcoreError(...) to inspect one and isUsageError(...) to identify errors caused by user input. For application-specific input rules, throw CliUsageError. The current error types and codes are defined in the error source files.
Examples
All examples are grouped by task.
Release history is in CHANGELOG.md. Design decisions are in docs/roadmap.md.
See the development policies for contributor guidelines.
Licensed under the MIT License.
