jackspeak CLI Argument Parser

4.2.3 · active · verified Tue Apr 21

jackspeak is a strict command-line argument parser for Node.js, currently at version 4.2.3. It focuses on rigorous validation of string, boolean, and number options, reading from both command-line arguments and environment variables. The library leverages a fluent, chainable API starting with `jack()`, which returns a `Jack` object for defining flags, options, and positionals. A key differentiator is its automatic type inference for TypeScript when defining configurations via object literals, ensuring strong typing for parsed values. It also supports automatic generation of `usage`/`help` banners and integrates environmental variable handling via an `envPrefix`, allowing configuration to be passed easily to child processes. Its minimal dependency footprint, especially the `/min` export introduced in v4.2, makes it suitable for CLI tools where startup performance is crucial. The project maintains an active development status with a positive version release cadence, typically releasing new versions within a three-month period.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic argument parsing, flag handling, option lists, and environment variable integration using the `jackspeak/min` entry point.

import { jack } from 'jackspeak/min'

const { positionals, values } = jack({ envPrefix: 'FOO' })
  .flag({
    asdf: { description: 'sets the asfd flag', short: 'a', default: true },
    'no-asdf': { description: 'unsets the asdf flag', short: 'A' },
    foo: { description: 'another boolean', short: 'f' },
  })
  .optList({
    'ip-addrs': {
      description: 'addresses to ip things',
      delim: ',', // defaults to '\n'
      default: ['127.0.0.1'],
    },
  })
  .parse([
    'some',
    'positional',
    '--ip-addrs',
    '192.168.0.1',
    '--ip-addrs',
    '1.1.1.1',
    'args',
    '--foo', // sets the foo flag
    '-A', // short for --no-asdf, sets asdf flag to false
  ])

console.log(process.env.FOO_ASDF) // '0'
console.log(process.env.FOO_FOO) // '1'
console.log(values) // { /* ... parsed values ... */ }
console.log(process.env.FOO_IP_ADDRS) // '192.168.0.1,1.1.1.1'
console.log(positionals) // ['some', 'positional', 'args']

view raw JSON →