Getopts

Fast, dependency-free CLI argument parser for Node.js

Library
npm
v2.3.0
636stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
37/100Needs Attention
Development Activity0
Maintenance20
Community48
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
72/100Good
Architecture65
Code Quality78
Innovation60
Learning Curve85

Getopts is a lightweight drop-in replacement for minimist and similar CLI argument parsers. At roughly 180 lines of code with zero dependencies, it breaks up process.argv into a plain key-value object, following the POSIX/GNU utility conventions that most command-line tools already expect.

It supports short options, grouped short flags, long options, aliases, boolean coercion, string coercion, default values, and a stopEarly mode for passing through remaining arguments untouched. The project advertises up to 6x faster parsing than comparable libraries, making it a common choice for CLI tools that want minimist-style ergonomics without the dependency weight.

What You Get

  • A single getopts(argv, options) function with no runtime dependencies
  • Support for short options, grouped short flags (-ab => {a:true,b:true}), and long options
  • Alias, boolean, string, and default-value configuration per option
  • A stopEarly mode to stop parsing at the first operand and pass through the rest
  • Both ESM (index.js) and CommonJS (index.cjs) builds

Common Use Cases

  • Parsing arguments in a custom Node.js CLI tool without pulling in a heavier dependency
  • Replacing minimist in an existing project for faster parsing with a compatible API shape
  • Building small build scripts or task runners that need simple flag/option handling

Under The Hood

Architecture The entire library lives in a single index.js file (~180 lines) that exports one function. It walks the input argv array once, classifying each token as a short option, long option, or operand, and accumulates results into a plain object keyed by option name plus an _ array for positional operands. Tech Stack Zero runtime dependencies; the package ships both an ESM entry (index.js) and a generated CommonJS build (index.cjs) produced by a small build script in package.json, plus TypeScript type declarations (index.d.ts). Code Quality Test coverage is organized by feature area (tests/short.test.js, long.test.js, alias.test.js, boolean.test.js, stopEarly.test.js, unknown.test.js, etc.) using the twist test runner with c8 for coverage, giving each parsing rule its own focused test file. API Design The API is a single function call with a plain options object (alias, boolean, string, default, stopEarly, unknown), requiring no setup or class instantiation, which keeps integration into any CLI script to one line.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search