Getopts CLI Argument Parser

2.3.0 · active · verified Wed Apr 22

getopts is a lightweight and performant CLI argument parser for Node.js, currently at stable version 2.3.0. It aims to be a drop-in replacement for `minimist` and similar libraries, distinguishing itself through its small footprint (180 LOC), zero dependencies, and significantly faster parsing speeds, reportedly up to 6 times faster than alternatives. The library follows established utility conventions for argument parsing, providing sane defaults for common CLI patterns. It transitioned to ES modules in version 2.3.0 and has shipped TypeScript types since 2.1.1, facilitating modern JavaScript and TypeScript development workflows. Releases are generally driven by new features, bug fixes, and maintenance, with a recent focus on ESM migration.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing command-line arguments, including aliases and explicit type definitions for options.

import getopts from "getopts";

const args = process.argv.slice(2);
const options = getopts(args, {
  alias: {
    output: ["o", "f"],
    type: "t",
  },
  boolean: ["verbose"],
  string: ["name"]
});

console.log("Parsed options:", options);
// Example usage: node your-script.js --type=module -o main.js *.{js,json} --name="My App" -v
// Expected output: { _: ['*.{js,json}'], output: 'main.js', type: 'module', o: 'main.js', f: 'main.js', t: 'module', name: 'My App', verbose: true }

view raw JSON →