CLI is a Node.js library for rapidly building command line applications. It provides essential tools for argument parsing and stdin processing, enabling developers to create CLI tools with minimal boilerplate code.
npm install cliconst cli = require('cli');For ES modules:
import cli from 'cli';const cli = require('cli');
// Parse command-line options
const options = cli.parse();
// Process stdin line by line
cli.withStdinLines(function(lines, newline) {
// Sort lines (example usage)
lines.sort(!options.n ? null : function(a, b) {
return parseInt(a) - parseInt(b);
});
// Reverse if -r option provided
if (options.r) lines.reverse();
// Output processed lines
this.output(lines.join(newline));
});Access the library version.
/**
* Version string for the CLI library
*/
cli.version: string;Parses command-line arguments and returns a structured options object.
/**
* Parse command-line arguments and options
* @param {Object} [options] - Configuration for parsing behavior
* @param {Object} [commands] - Command definitions and handlers
* @param {Object} [defaults] - Default values for options
* @returns {Object} Parsed options object with command-line flags and values
*/
function parse(options?: Object, commands?: Object, defaults?: Object): Object;The returned options object contains properties corresponding to command-line flags:
-r) appear as boolean properties-n value) appear with their associated valuesn for numeric operations and r for reverse operationsUsage Example:
const cli = require('cli');
// Parse arguments like: node script.js -r -n
const options = cli.parse();
console.log(options.r); // true if -r flag provided
console.log(options.n); // true if -n flag providedProcesses standard input line-by-line and executes a callback with the processed data.
/**
* Process stdin line by line with callback
* @param {Function} callback - Function to process stdin lines
*/
function withStdinLines(callback: (lines: string[], newline: string) => void): void;The callback function receives:
lines - Array of strings representing each line from stdinnewline - The newline character(s) used in the inputThe callback context (this) provides:
output(content) - Method to output processed contentUsage Example:
const cli = require('cli');
cli.withStdinLines(function(lines, newline) {
// Transform each line to uppercase
const transformed = lines.map(line => line.toUpperCase());
// Output the result
this.output(transformed.join(newline));
});Common Patterns:
// Sorting with options
cli.withStdinLines(function(lines, newline) {
const options = cli.parse();
// Numeric sort if -n flag provided
lines.sort(!options.n ? null : function(a, b) {
return parseInt(a) - parseInt(b);
});
// Reverse if -r flag provided
if (options.r) lines.reverse();
this.output(lines.join(newline));
});
// Filtering
cli.withStdinLines(function(lines, newline) {
const filtered = lines.filter(line => line.length > 0);
this.output(filtered.join(newline));
});
// Line-by-line processing
cli.withStdinLines(function(lines, newline) {
const processed = lines.map(line => {
return line.trim().split(' ').reverse().join(' ');
});
this.output(processed.join(newline));
});/**
* Main CLI module interface
*/
interface CLI {
/** Version string for the CLI library */
version: string;
/** Parse command-line arguments and options */
parse(options?: Object, commands?: Object, defaults?: Object): Object;
/** Process stdin line by line with callback */
withStdinLines(callback: (lines: string[], newline: string) => void): void;
}
/**
* Callback context for withStdinLines
*/
interface StdinCallbackContext {
/** Output processed content */
output(content: string): void;
}