or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-cli

A tool for rapidly building command line apps with argument parsing and stdin processing

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/cli@1.0.x

To install, run

npx @tessl/cli install tessl/npm-cli@1.0.0

index.mddocs/

CLI

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.

Package Information

  • Package Name: cli
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install cli

Core Imports

const cli = require('cli');

For ES modules:

import cli from 'cli';

Basic Usage

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));
});

Capabilities

Version Information

Access the library version.

/**
 * Version string for the CLI library
 */
cli.version: string;

Command-Line Argument Parsing

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:

  • Boolean flags (e.g., -r) appear as boolean properties
  • Value flags (e.g., -n value) appear with their associated values
  • Common options include n for numeric operations and r for reverse operations

Usage 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 provided

Standard Input Processing

Processes 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 stdin
  • newline - The newline character(s) used in the input

The callback context (this) provides:

  • output(content) - Method to output processed content

Usage 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));
});

Types

/**
 * 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;
}