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.
npm install getoptsVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing command-line arguments, including aliases and explicit type definitions for options.
Migrate your project to use ES modules (`import`) or switch to dynamic `import('getopts')` if staying in CommonJS. Ensure your `package.json` specifies `"type": "module"` or uses `.mjs` file extensions for ESM.Review your argument parsing logic for handling of single hyphens (`-`) and ensure `options.default` is not expected to be mutated. Make explicit copies if modification is necessary.
Always check for the presence of an option's value if `undefined` is expected when the option is not provided. Filter out default values if they are indistinguishable from deliberately provided empty strings or `false`.
Always explicitly define option types using `opts.boolean: ['a']` or `opts.string: ['a']` to ensure consistent parsing behavior regardless of argument placement.
Anticipate array values for options that can legitimately be repeated. Process these arrays to consolidate or use all values as required by your application logic.
Change `const getopts = require('getopts')` to `import getopts from 'getopts'` and ensure your project is configured for ESM.For ESM, use `import getopts from 'getopts'`. For CommonJS, either stick to v2.2.x or use dynamic import: `(await import('getopts')).default` to access the default export.To force an option to be parsed as a string, use `opts.string: ['optionName']`. To force it as a boolean (and push any subsequent argument into the operand array `_`), use `opts.boolean: ['optionName']`.
Ensure you are on `getopts@2.0.0` or newer. If you need to modify the default options based on parsed arguments, create a deep copy of your default options object before passing it to `getopts`.
No dependency data recorded yet.