node-getopt: Command Line Argument Parser

0.3.2 · abandoned · verified Tue Apr 21

node-getopt is a minimalist command-line argument parser for Node.js, designed to mimic the traditional `getopt` utility. Currently at version 0.3.2, the package was last updated in 2013, indicating it is an abandoned project and no longer actively maintained. Its primary function is to define and parse both short and long options, supporting mandatory or optional arguments, and handling multiple occurrences of an option. It includes utilities for binding a 'help' option and generating usage messages. Its key differentiator at the time was its simplicity and close adherence to traditional `getopt` semantics for Node.js environments. Due to its age and reliance on very old Node.js versions (>=0.6.0), modern Node.js applications will likely find it incompatible or prefer more contemporary argument parsing libraries that support ESM and TypeScript.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to define command-line options, parse system arguments, and access the parsed options and remaining arguments using node-getopt.

const Getopt = require('node-getopt');

// Create a Getopt instance with option definitions
const opt = Getopt.create([
  ['s' , ''                    , 'short option.'],
  [''  , 'long'                , 'long option.'],
  ['S' , 'short-with-arg=ARG'  , 'option with argument', 'S'],
  ['L' , 'long-with-arg=ARG'   , 'long option with argument'],
  [''  , 'color[=COLOR]'       , 'COLOR is optional'],
  ['m' , 'multi-with-arg=ARG+' , 'multiple option with argument'],
  [''  , 'no-comment'],
  ['h' , 'help'                , 'display this help'],
  ['v' , 'version'             , 'show version']
])
.bindHelp(); // Bind the 'help' option to display usage information

// Parse command line arguments from process.argv
const parsed = opt.parseSystem();

console.log('Parsed Options:', parsed.options);
console.log('Remaining Arguments:', parsed.argv);

/*
To run:
  node your-script-name.js foo -s --long-with-arg bar -m a -m b -- --others

Example Output:
  Parsed Options: { 'short-with-arg': 'S', s: true, 'long-with-arg': 'bar', 'multi-with-arg': [ 'a', 'b' ], S: 'S', L: 'bar', m: [ 'a', 'b' ] }
  Remaining Arguments: [ 'foo', '--others' ]
*/

view raw JSON →