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 🙏

© 2026 – Pkg Stats / Ryan Hefner

structured-args

v1.1.0

Published

A feature-complete, type safe and highly flexible command line argument parsing library

Readme

structured-args

A feature-complete, type safe and highly flexible command line argument parsing library for JavaScript.

import { parse_args, boolean, string } from "structured-args";

const args = parse_args({
    verbose: { alias: "v", type: boolean() },
    name: { alias: "n", type: string(), default: "world" }
});

console.log(`Hello, ${args.name}!`);
if (args.verbose) console.log("Verbose mode enabled.");
$ node index.js -v=false --name CLI
Hello, CLI!

Parsing

Parsing supports:

  • Bundling short options with - (All options except for the last ones must be boolean)
  • getopt style short options immediately followed by their value, eg -O3 or -ifile.js
  • Assigning values with a = sign, eg --letter=a
  • Using a standalone -- to delimit options and values
  • Treating negative numbers as values rather than flags (including Infinity and -Infinity)
  • Optionally accepting multiple values for an option
  • Automatic type conversion and validation via option-type processors
  • Collecting positional arguments (standalone values) into the output array

The output object of parse_args is an array with the numeric indices corresponding to the standalone positional arguments, and additional fields for options that were passed (or that have a default value).

const args = parse_args({
    recursive: { alias: "r", type: boolean() },
    depth: { type: int(), default: 1 }
});

console.log(args[0]);
console.log(args.recursive);
$ node index.js -r --depth 3 path/to/dir
'path/to/dir'
true

Error handling

By default, parse_args prints a descriptive error message to stderr and exits the process when it encounters invalid input.

# Missing value for non-boolean flag
$ node index.js --name
Argument error: Expected a value after option 'name' (Found nothing)

# Unrecognized option
$ node index.js --unknown
Argument error: Unrecognised option 'unknown'

# Type validation failure (from built-in processor - see example below)
$ node index.js --port=99999
Argument error: port: Must be an integer between 1 and 65535 (Received: '99999')

Advanced usage

parse_args handles complex scenarios like multi-value flags and custom type validation out of the box.

import { parse_args, int, list, boolean } from "structured-args";

const options = {
    port: { alias: "p", type: int(1, 65535), default: 8080 },
    tags: { type: string(), multiple: true },
    debug: { alias: "d", type: boolean() }
};

const args = parse_args(options);
console.log(args);
$ node node index.js -p 0xBB8 --tags web api prod --debug extra_value
['extra_value', port: 3000, tags: ['web', 'api', 'prod'], debug: true]

Configuration

The parse_args function accepts an optional configuration object:

| Option | Type | Default | Description | | ----------------------------- | ----------------------- | ----------------------- | ------------------------------------------------ | | collect_values | boolean | true | Collect standalone values into the output array. | | collect_unknown_options | boolean | false | Parse options not defined in the schema. | | allow_double_dash_delimeter | boolean | true | Allow passing -- to stop option parsing. | | argv | string[] | process.argv.slice(2) | Array of arguments to parse. | | on_error | (msg: string) => void | standard_error | Custom error handler. |

Option types

The library includes several built-in processors in structured-args/option-types:

| Processor | Description | | ------------------------ | --------------------------------------------------------------------- | | string() | Returns the argument as a string. | | boolean() | Returns true (can be explicitly set to false using =false). | | int(min, max) | Validates and returns an integer, optionally between min and max. | | float(min, max) | Validates and returns a number, optionally between min and max. | | one_of(...options) | Ensures value is one of the provided strings. | | not_one_of(...options) | Ensures value is not one of the provided strings. | | list(separator) | Splits a string into an array by separator (default ,). |

Custom option types

A flag type is simply a function that takes a string and returns a value of any type. If the input is invalid, it should throw an error message as a string.

const absolutePath = (arg: string) => {
    if (!arg.startsWith("/")) throw "Must be an absolute path";
    return arg;
};

const args = parse_args({ path: { type: absolutePath } });

If a custom processor throws an error, the library catches it and appends the received value:

$ node index.js --path=./relative/path
Argument error: path: Must be an absolute path (Received: './relative/path')

Help menu generation

This library can also be used to generate a flag table to use in help menus:

import { help_string, boolean, string } from "structured-args";

const options = {
    verbose: { alias: "v", type: boolean(), description: "Show verbose output" },
    name: { alias: "n", type: string(), arg_label: "<name>", description: "Your name" }
};

console.log("Usage: my-app [options]\n");
console.log(help_string(options));

Output:

Usage: my-app [options]

 -v, --verbose        Show verbose output
 -n, --name <name>    Your name

Help configuration

The help_string generator can be customized with a HelpConfig object:

| Option | Type | Default | Description | | ---------------------- | ------------------- | ----------- | --------------------------------------------- | | descriptions_only | boolean | false | Only include options that have a description. | | option_format | util.InspectColor | undefined | Color or style used to format flag strings. | | spaces_before_option | number | 1 | Padding before the flags. | | spaces_after_option | number | 4 | Padding between flags and description. |