npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

cli-options

v1.0.0

Published

A simple command-line argument parser for Node.js applications.

Readme

parseargs

A simple, lightweight command-line argument parser for Node.js applications with built-in validation and automatic help generation.

Features

  • 🚀 Simple API - Easy to use with minimal configuration
  • Argument Validation - Automatically validates required and optional arguments
  • 📖 Auto-generated Help - Generates help text from your argument definitions
  • 🔢 Positional Arguments - Support for positional file arguments
  • 🏷️ Named Arguments - Support for --flag style arguments
  • 🎯 TypeScript Support - Full TypeScript definitions included
  • 🪶 Lightweight - Minimal dependencies (only word-wrap)

Installation

npm install parseargs

Quick Start

import { parseArgs } from 'parseargs';

const args = parseArgs({
    required: {
        0: "input file to process"
    },
    optional: {
        verbose: "enable verbose logging"
    }
});

console.log(`Processing file: ${args[0]}`);
if (args.verbose) {
    process.env.VERBOSE = '1';
    console.log('Verbose mode enabled');
}

Command line usage:

node myapp.js data.txt --verbose
node myapp.js --help

Usage Examples

Multiple Positional Arguments

const args = parseArgs({
    required: {
        0: "source file",
        1: "destination file"
    },
    optional: {
        force: "overwrite existing files",
        quiet: "suppress output"
    }
});

Command line usage:

node myapp.js input.txt output.txt --force

Custom Help Configuration

const args = parseArgs({
    required: {
        0: "configuration file"
    },
    optional: {
        port: "server port number",
        host: "server hostname"
    },
    help: {
        short: "A simple web server application",
        long: "This application starts a web server using the provided configuration file. You can customize the host and port using the optional arguments.",
        usage: "config.json [--port=3000] [--host=localhost]"
    }
});

Argument Validation

const args = parseArgs({
    required: {
        0: "input file"
    },
    optional: {
        threads: "number of threads"
    },
    validate: (args) => {
        if (args.threads && isNaN(parseInt(args.threads))) {
            return "threads must be a number";
        }
    }
});

API Reference

parseArgs(options)

Parses command-line arguments and returns an object containing the parsed values.

Parameters

  • options (object) - Configuration object with the following properties:

Options

| Property | Type | Description | |----------|------|-------------| | required | object | Required arguments. Keys can be numbers (for positional args) or strings (for named args) | | optional | object | Optional arguments. Keys can be numbers (for positional args) or strings (for named args) | | help | object | Help configuration (see Help Configuration below) | | validate | function | Custom validation function that receives parsed args and returns error message or undefined |

Help Configuration

| Property | Type | Description | |----------|------|-------------| | short | string | Short description displayed in help | | long | string | Detailed description displayed in help | | usage | string \| boolean | Custom usage string or true for auto-generated usage | | params | boolean | Whether to show parameter descriptions (default: true) |

Return Value

Returns an object where:

  • Positional arguments are accessible by numeric keys (args[0], args[1], etc.)
  • Named arguments are accessible by their names (args.debug, args.output, etc.)
  • All values are strings

Error Handling

The function will automatically:

  • Display help and exit when --help is passed
  • Display error message and exit when required arguments are missing
  • Display error message and exit when invalid arguments are provided
  • Display error message and exit when custom validation fails

Argument Types

Positional Arguments

Positional arguments are defined using numeric keys and are accessed the same way:

const args = parseArgs({
    required: {
        0: "first file",
        1: "second file"
    }
});

// Access: args[0], args[1]

Named Arguments

Named arguments use the -- prefix on the command line:

const args = parseArgs({
    optional: {
        output: "output file path",
        verbose: "enable verbose mode"
    }
});

// Command line: --output=file.txt --verbose
// Access: args.output, args.verbose

Named arguments can be specified as:

  • --flag (sets value to "1")
  • --flag=value (sets value to "value")

TypeScript Support

The library includes full TypeScript definitions:

import { parseArgs, ParseArgsOptions } from 'parseargs';

const options: ParseArgsOptions = {
    required: {
        0: "input file"
    },
    optional: {
        debug: "debug mode"
    }
};

const args: Record<string, string> = parseArgs(options);

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Repository