CLI List Parser
cli-list is a utility package designed to parse command-line argument lists, typically sourced from `process.argv`, into a structured array of arrays. It specializes in breaking down a single array of arguments, where items might be comma-separated, into sub-arrays at each comma delimiter. This maintains the "argv style" of the sub-arrays, making them suitable for direct consumption by other command-line argument parsers like `minimist`. The current stable version is 1.0.0, which was released recently and updated the codebase to modern JavaScript standards while strictly enforcing a minimum Node.js engine requirement of 18.0.0. The package has a focused scope, providing a clear solution for preprocessing CLI arguments that utilize comma-separated values, acting as an essential parsing layer before more complex option handling.
Common errors
-
Error: Cannot find module 'cli-list'
cause Attempting to import `cli-list` using `import` syntax in a pure CommonJS environment, or using `require` in a pure ESM environment without proper interop configuration.fixFor CommonJS projects, use `const list = require('cli-list');`. For ESM projects, ensure your `package.json` has `"type": "module"` and your bundler/Node.js version supports CJS-ESM interop for default exports, using `import list from 'cli-list';`. -
npm ERR! code EUNSUPPORTEDENGINE npm ERR! Unsupported engine for cli-list@1.0.0: wanted: {"node": ">=18"} (current: v16.x.x)cause Attempting to install or run `cli-list` version 1.0.0 or newer on a Node.js version older than 18.fixUpgrade your Node.js environment to version 18 or higher. Use `node --version` to check your current Node.js version.
Warnings
- breaking Version 1.0.0 and above enforce a minimum Node.js engine requirement of `>=18`. Older Node.js versions will fail to install or run the package.
Install
-
npm install cli-list -
yarn add cli-list -
pnpm add cli-list
Imports
- list
import list from 'cli-list';
const list = require('cli-list'); - list (ESM usage)
import { list } from 'cli-list';import list from 'cli-list';
Quickstart
import list from 'cli-list';
import minimist from 'minimist'; // minimist needs to be installed via `npm install minimist`
// Simulate process.argv.slice(2) for a test case
// In a real CLI, you'd use: const args = process.argv.slice(2);
const rawCliArgs = ['foo', '--bar,', 'baz', '--qux', 'oof,value'];
console.log('Original CLI arguments:', rawCliArgs);
// Split the arguments into sub-arrays based on commas
const parsedLists = list(rawCliArgs);
console.log('cli-list parsed lists:', parsedLists);
// Expected: [['foo', '--bar'], ['baz'], ['--qux', 'oof'], ['value']]
// Process each sub-list using minimist
const opts = parsedLists.map(item => minimist(item));
console.log('Minimist parsed options for each list:', opts);
/*
Expected Output (opts):
[
{ _: [ 'foo' ], bar: true },
{ _: [ 'baz' ] },
{ _: [ 'oof' ], qux: true },
{ _: [ 'value' ] }
]
*/