`opts` is a lightweight command-line argument parser for Node.js, currently at version 2.0.2. It provides functionalities for parsing short (`-s`) and long (`--long`) options, as well as positional arguments, and automatically generates help text. A key differentiator is its minimal footprint and zero external dependencies, designed to work as a standalone JavaScript file without requiring NPM or other package managers. This makes it suitable for projects prioritizing simplicity and a small bundle size. While primarily a plain JavaScript library, it ships with TypeScript definitions for enhanced development experience. `opts` processes arguments through callbacks associated with each option, rather than returning a structured object of parsed values. Its stable nature suggests a focus on maintenance, offering a robust solution for basic to moderate CLI parsing needs.
npm install optsVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to define command-line options with short and long forms, descriptions, and callbacks using TypeScript, enabling automatic help generation and basic argument parsing for a CLI tool. It also shows how to handle option values and simple positional commands.
Ensure your `tsconfig.json` `moduleResolution` is set appropriately (e.g., `Node16` or `Bundler`) and consistently use explicit named or namespace imports in ESM contexts.
Design your option callbacks to directly invoke application logic or to store parsed values in a shared data structure (e.g., a mutable configuration object passed by reference) that can be accessed after `opts.parse` completes.
For standalone usage, manually download `opts.d.ts` from the GitHub repository and ensure it's included in your TypeScript project's `files` or `include` array within `tsconfig.json` to enable full type checking and IDE support.
Evaluate your CLI's complexity before choosing `opts`. For applications requiring advanced features such as subcommands, extensive validation, or custom help formatting, consider more comprehensive libraries like `commander.js` or `yargs`.
For ESM/TypeScript, use `import { parse } from 'opts';` or `import * as opts from 'opts';`. For CommonJS, ensure `var opts = require('opts');` then call `opts.parse(...)`.Run `npm install opts` or `yarn add opts`. If using the standalone version, ensure `opts.js` is in your project and referenced correctly, e.g., `require('./opts.js')` or `import { parse } from './opts.js';` depending on your module system.Set `value: true` in your `Option` definition if the option expects a value to follow it. For boolean flags that do not take a value (e.g., `--verbose`), `value` should be `false` or omitted. The actual parsed value is then passed to the option's `callback` function.
No dependency data recorded yet.