commonline
v1.0.0
Published
Modern Custom CLI Framework for Node.js without external dependencies
Readme
commonLine
Modern Custom CLI Framework for Node.js. A lightweight, production-ready CLI library with a custom input engine (no readline dependency).
Features
- ✅ Custom input engine built from scratch (no readline/inquirer/prompts)
- ✅ Clean, modular architecture
- ✅ Command registration and execution
- ✅ Argument parsing with quote support
- ✅ Graceful error handling
- ✅ Ctrl+C support
- ✅ No external dependencies
- ✅ Fully tested
- ✅ Production-ready
Installation
npm install commonlineQuick Start
const cl = require('commonline');
cl.createInput('~ $ ');
cl.command('contoh', () => {
console.log('tes');
});
cl.command('say', (args) => {
console.log(args.join(' '));
});
cl.start();Run:
node app.jsOutput:
~ $ contoh
tes
~ $ say halo dunia
halo dunia
~ $ Usage
Basic Setup
const cl = require('commonline');
// Set custom prompt
cl.createInput('myapp> ');
// Define commands
cl.command('hello', () => {
console.log('Hello!');
});
// Start CLI
cl.start();Commands with Arguments
cl.command('greet', (args) => {
const name = args.join(' ') || 'Guest';
console.log(`Hello, ${name}!`);
});
cl.command('add', (args) => {
const sum = args
.map(n => parseInt(n, 10))
.filter(n => !isNaN(n))
.reduce((a, b) => a + b, 0);
console.log(`Sum: ${sum}`);
});Usage:
myapp> greet John Doe
Hello, John Doe!
myapp> add 5 10 15
Sum: 30Quote Support
Arguments with spaces can use quotes:
cl.command('echo', (args) => {
console.log(args.join(' '));
});Usage:
myapp> echo "hello world"
hello world
myapp> echo 'single quotes'
single quotesCommand Management
// Check if command exists
if (cl.hasCommand('test')) {
console.log('Command exists');
}
// Remove command
cl.removeCommand('test');
// Clear all commands
cl.clearCommands();
// Stop CLI
cl.stop();
process.exit(0);API Reference
createInput(prompt)
Set custom prompt string.
cl.createInput('> ');command(name, callback)
Register a command with callback.
Callback signature:
(args, context) => {
// args: string[] - command arguments
// context: CLIContext - CLI context object
}Example:
cl.command('test', (args, context) => {
console.log('Args:', args);
console.log('Prompt:', context.getPrompt());
});removeCommand(name)
Remove a registered command.
cl.removeCommand('test');hasCommand(name)
Check if command is registered.
if (cl.hasCommand('test')) {
// ...
}clearCommands()
Remove all registered commands.
cl.clearCommands();start()
Start the CLI input loop.
cl.start();stop()
Stop the CLI.
cl.stop();Architecture
commonLine uses a modular architecture:
CLI (Main)
├── InputEngine (Custom input handling)
│ ├── Terminal (Terminal I/O)
│ ├── LineEditor (Line display)
│ └── KeyHandler (Keyboard events)
├── CommandRegistry (Command storage)
├── CommandParser (Input parsing)
└── CLIContext (State management)Core Modules
- CLI.js - Main orchestrator
- CommandRegistry.js - Command management
- CommandParser.js - Input parsing
- CLIContext.js - State/configuration
- InputEngine.js - Input loop
- Terminal.js - Terminal I/O
- LineEditor.js - Line editing
- KeyHandler.js - Keyboard events
- Command.js - Individual commands
Examples
See examples/ directory:
basic.js- Basic commandsarguments.js- Argument handlingmultiple-commands.js- Complex example
Run examples:
npm run example:basic
npm run example:arguments
npm run example:multipleTesting
Run all tests:
npm testTests cover:
- Command registration and execution
- Argument parsing with quotes
- CLI lifecycle
- Error handling
- Command deduplication
Development
Project structure:
commonLine/
├── src/
│ ├── index.js # Entry point
│ ├── core/ # Core classes
│ ├── input/ # Input engine
│ ├── commands/ # Command classes
│ ├── errors/ # Error classes
│ └── utils/ # Utilities
├── examples/ # Example programs
├── test/ # Test suite
└── README.mdNo External Dependencies
commonLine doesn't use:
- readline
- inquirer
- prompts
- enquirer
- Any other CLI library
It uses only Node.js native APIs:
- process.stdin/stdout
- Buffer
- EventEmitter pattern
Keyboard Support
- Enter - Submit command
- Backspace - Delete character
- Ctrl+C - Exit gracefully
- Ctrl+D - Exit gracefully
Error Handling
Errors are handled gracefully:
cl.command('test', (args) => {
throw new Error('Something went wrong');
// Error is caught and logged
});Performance
- Zero initialization overhead
- Minimal memory footprint
- Direct terminal I/O
- No buffering overhead
License
MIT
Contributing
Contributions welcome!
