OptionParser is a JavaScript library designed for parsing command-line options, mimicking the functionality of POSIX `getopt`. It supports both short (`-x`) and long (`--long`) options, including combined short options (`-xxxyxxz`), and handles both required and optional argument values (e.g., `-x=Value`, `--long Value`). Key features include nearly automatic help message generation, flexible option handling through callbacks or direct access to option objects, and the ability to return any unparsed arguments. As of version 1.0.2, the library appears to be in a maintenance state, with its last update occurring in August 2021. While robust for its intended use, its release cadence is low, distinguishing it from more actively developed, modern CLI parsing solutions that may offer broader ecosystem support or different API paradigms.
npm install option-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the OptionParser, define various types of options (help, flag, required argument, optional argument), and parse simulated command-line input. It shows how to access parsed values through callbacks and option objects, and how to retrieve unparsed arguments.
Use `const OptionParser = require('option-parser');` for CommonJS projects. For ESM, you might need a wrapper or a CJS-compatible loader, or consider a different CLI parser with explicit ESM support.Review the project's GitHub repository for any unreleased updates or known issues. For critical applications, evaluate if a more actively maintained CLI parsing library is a better fit.
Implement robust data validation within your option `action` callbacks. For example, use `parseInt()` and check `isNaN()` for numeric arguments, and provide user-friendly error messages if validation fails.
Ensure `const OptionParser = require('option-parser');` is at the top of your file before instantiating the parser.Verify that you have correctly instantiated the parser using `const parser = new OptionParser();` before attempting to call its methods.
Double-check the short and long option names registered with `addOption` against the command-line arguments. Ensure correct casing and that all aliases (e.g., both '-i' and '--input') are explicitly defined.
No dependency data recorded yet.