CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-yargs

Command-line argument parsing library for Node.js with commands, options, and validation.

Overview
Eval results
Files

core-parser.mddocs/

Core Parser Configuration

Primary yargs factory function and fundamental configuration methods for setting up argument parsing behavior.

Capabilities

Yargs Factory Function

Creates a new yargs parser instance with optional configuration.

/**
 * Creates a new yargs parser instance
 * @param processArgs - Arguments to parse (default: [])
 * @param cwd - Current working directory  
 * @param parentRequire - Parent require function
 * @returns YargsInstance for method chaining
 */
function yargs(processArgs?: string | string[], cwd?: string, parentRequire?: Function): YargsInstance;

Usage Examples:

import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

// Parse process.argv (most common)
const argv1 = yargs(hideBin(process.argv)).parse();

// Parse custom arguments
const argv2 = yargs(['--port', '3000', '--verbose']).parse();

// Parse with custom working directory
const argv3 = yargs(hideBin(process.argv), '/custom/path').parse();

Script Name Configuration

Configure how yargs identifies and displays the script name.

/**
 * Set custom script name (overrides automatic detection)
 * @param scriptName - Custom script name to display
 * @returns YargsInstance for chaining
 */
scriptName(scriptName: string): YargsInstance;

Usage Examples:

yargs()
  .scriptName('my-tool')
  .help()
  .parse();
// Help will show "my-tool [options]" instead of auto-detected name

Parser Configuration

Configure low-level parsing behavior.

/**
 * Configure parser behavior with advanced options
 * @param config - Parser configuration object
 * @returns YargsInstance for chaining
 */
parserConfiguration(config: Configuration): YargsInstance;

Usage Examples:

yargs()
  .parserConfiguration({
    'duplicate-arguments-array': false,
    'flatten-duplicate-arrays': true,
    'populate--': true,
    'set-placeholder-key': true,
    'halt-at-non-option': false,
    'strip-aliased': false,
    'strip-dashed': false,
    'boolean-negation': true,
    'camel-case-expansion': true,
    'deep-merge-config': false,
    'sort-commands': false
  })
  .parse();

Terminal Width

Control help text formatting based on terminal width.

/**
 * Get current terminal width
 * @returns Terminal width in columns or null
 */
terminalWidth(): number | null;

/**
 * Set wrap width for help text formatting
 * @param cols - Number of columns to wrap at, or null for no wrapping
 * @returns YargsInstance for chaining
 */
wrap(cols: number | null): YargsInstance;

Usage Examples:

const width = yargs().terminalWidth();
console.log(`Terminal width: ${width}`);

// Force wrap at 80 columns
yargs()
  .wrap(80)
  .help()
  .parse();

// Disable wrapping
yargs()
  .wrap(null)
  .help()
  .parse();

Process Control

Control how yargs interacts with the process lifecycle.

/**
 * Control whether yargs calls process.exit() on completion
 * @param enabled - Whether to exit process (default: true)
 * @returns YargsInstance for chaining
 */
exitProcess(enabled?: boolean): YargsInstance;

/**
 * Get current exit process setting
 * @returns Whether yargs will exit the process
 */
getExitProcess(): boolean;

Usage Examples:

// Disable process exit (useful for testing)
yargs()
  .exitProcess(false)
  .help()
  .parse();

// Check current setting
const willExit = yargs().getExitProcess();

Configuration Objects

Load configuration from objects or package.json.

/**
 * Load configuration from package.json
 * @param key - Key in package.json to read config from
 * @param rootPath - Root path for package.json search
 * @returns YargsInstance for chaining
 */
pkgConf(key: string, rootPath?: string): YargsInstance;

Usage Examples:

// Load config from package.json "myapp" field
yargs()
  .pkgConf('myapp')
  .parse();

// package.json:
// {
//   "myapp": {
//     "port": 3000,
//     "verbose": true
//   }
// }

Types

/**
 * Parser configuration options
 */
interface Configuration {
  /** Create array for duplicate arguments */
  'duplicate-arguments-array'?: boolean;
  /** Flatten duplicate arrays */
  'flatten-duplicate-arrays'?: boolean;
  /** Populate -- with remaining args */
  'populate--'?: boolean;
  /** Set placeholder key for missing options */
  'set-placeholder-key'?: boolean;
  /** Stop parsing at first non-option */
  'halt-at-non-option'?: boolean;
  /** Remove aliased keys from result */
  'strip-aliased'?: boolean;
  /** Remove dashed keys from result */
  'strip-dashed'?: boolean;
  /** Enable --no-flag negation */
  'boolean-negation'?: boolean;
  /** Convert kebab-case to camelCase */
  'camel-case-expansion'?: boolean;
  /** Deep merge config objects */
  'deep-merge-config'?: boolean;
  /** Sort commands in help */
  'sort-commands'?: boolean;
  /** Parse positional numbers */
  'parse-positional-numbers'?: boolean;
}

Install with Tessl CLI

npx tessl i tessl/npm-yargs

docs

command-system.md

core-parser.md

help-output.md

index.md

option-definition.md

parsing.md

validation.md

tile.json