Wargs - Wrong Args Parser
Wargs is a JavaScript utility designed for parsing command-line-like arguments from both strings and `argv`-style arrays, distinguishing itself from conventional argument parsers like `commander` or `yargs`. Unlike most alternatives, it leverages regular expressions internally, allowing it to process raw string inputs which traditional parsers typically do not support. It categorizes parsed arguments into `_` (positional arguments), `raw` (arguments after `--`), `data` (key=value pairs), `flags` (boolean flags like `--json` or `-x`), and `params` (key:value pairs). The current stable version is 0.10.0, however, the project appears to be abandoned with the last commit made in September 2020 and the latest npm publish being 5 years ago, indicating no ongoing development or maintenance. This tool is particularly suited for scenarios where arguments might come from varied sources beyond `process.argv` and require a flexible, regex-driven extraction approach.
Common errors
-
TypeError: (0 , wargs__WEBPACK_IMPORTED_MODULE_0__.wargs) is not a function
cause Attempting to use `wargs` with named import syntax (`import { wargs } from 'wargs'`) when it is a default export.fixUse a default import: `import wargs from 'wargs';` -
TypeError: wargs is not a function
cause Attempting to destructure `wargs` from `require()` in CommonJS (`const { wargs } = require('wargs')`) when it is a default export.fixUse the direct `require()` assignment: `const wargs = require('wargs');` -
Arguments not parsed as expected (e.g., 'key=value' in 'params' instead of 'data')
cause Misunderstanding the distinction between `data` (for `key=value` syntax) and `params` (for `key:value` syntax) in `wargs`'s output structure.fixRefer to the `wargs` documentation carefully. `data` collects `key=value` pairs, while `params` collects `key:value` pairs. Adjust your input or access the correct output property accordingly.
Warnings
- gotcha Wargs is explicitly designed as a 'wrong args parser' and deviates significantly from common CLI parsing libraries like `minimist` or `yargs`. Its regex-based approach and unique categorization of arguments (`data` for `key=value` vs. `params` for `key:value`) requires careful understanding of its output structure.
- deprecated The `wargs` package appears to be abandoned. The last commit to its GitHub repository was in September 2020, and the latest version (0.10.0) was published 5 years ago on npm. This means there is no active development, bug fixes, or security patches, making it unsuitable for new projects or critical applications.
- gotcha Wargs does not ship with TypeScript type definitions. Developers using TypeScript will need to create their own declaration files (`.d.ts`) or use `declare module 'wargs';` to avoid type errors, losing the benefits of strong typing.
Install
-
npm install wargs -
yarn add wargs -
pnpm add wargs
Imports
- wargs
import { wargs } from 'wargs';import wargs from 'wargs';
- wargs
const { wargs } = require('wargs');const wargs = require('wargs');
Quickstart
import wargs from 'wargs';
const strInput = '/ _csrf=`token` --json accept:"text/plain; charset=utf8" -- x foo:bar';
const arrayInput = ['/', '_csrf=`token`', '--json', 'accept:text/plain; charset=utf8', '--', 'x', 'foo:bar'];
console.log('--- Parsing String Input ---');
const parsedFromString = wargs(strInput, { camelCase: true });
console.log('Parsed Object:', parsedFromString);
console.log('Positional arguments (_):', parsedFromString._);
console.log('Raw arguments (raw):', parsedFromString.raw);
console.log('Data (key=value):', parsedFromString.data);
console.log('Flags (--flag, -f):', parsedFromString.flags);
console.log('Parameters (key:value):', parsedFromString.params);
console.log('\n--- Parsing Array Input ---');
const parsedFromArray = wargs(arrayInput, { format: v => String(v).toUpperCase() });
console.log('Parsed Object:', parsedFromArray);
console.log('Positional arguments (_):', parsedFromArray._);
console.log('Raw arguments (raw):', parsedFromArray.raw);
console.log('Data (key=value):', parsedFromArray.data);
console.log('Flags (--flag, -f):', parsedFromArray.flags);
console.log('Parameters (key:value):', parsedFromArray.params);