![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
The 'subarg' npm package allows you to parse command-line arguments with nested sub-arguments. This is particularly useful for complex command-line interfaces where arguments can have their own sub-arguments.
Basic Parsing
This feature allows you to parse command-line arguments into a structured object. The `process.argv.slice(2)` part skips the first two default arguments (node and script path).
const subarg = require('subarg');
const argv = subarg(process.argv.slice(2));
console.log(argv);
Nested Arguments
This feature allows you to parse nested sub-arguments. For example, `--foo [--bar baz]` will be parsed into an object where `foo` is an array containing an object with `bar` set to `baz`.
const subarg = require('subarg');
const argv = subarg(process.argv.slice(2));
console.log(argv);
// Example: node script.js --foo [--bar baz]
Default Values
This feature allows you to set default values for arguments. If an argument is not provided, it will take the default value specified.
const subarg = require('subarg');
const argv = subarg(process.argv.slice(2), { default: { foo: 'default' } });
console.log(argv);
// Example: node script.js
Minimist is a lightweight package for parsing command-line arguments. It is simpler than subarg and does not support nested sub-arguments. It is useful for straightforward argument parsing.
Yargs is a more feature-rich package for parsing command-line arguments. It supports nested arguments, command handling, and more. It is more complex than subarg but offers a broader range of functionalities.
Commander is another powerful package for building command-line interfaces. It supports nested commands and options, making it comparable to subarg in terms of handling complex argument structures.
parse arguments with recursive contexts using minimist
This module is useful if you need to pass arguments into a piece of code without coordinating ahead of time with the main program, like with a plugin system.
var subarg = require('subarg');
var argv = subarg(process.argv.slice(2));
console.log(argv);
Contexts are denoted with square brackets:
$ node example/show.js rawr --beep [ boop -a 3 ] -n4 --robots [ -x 8 -y 6 ]
{ _: [ 'rawr' ],
beep: { _: [ 'boop' ], a: 3 },
n: 4,
robots: { _: [], x: 8, y: 6 } }
var subarg = require('subarg')
Parse the arguments array args
, passing opts
to
minimist.
An opening [
in the args
array creates a new context and a ]
closes a
context. Contexts may be nested.
With npm do:
npm install subarg
MIT
FAQs
parse arguments with recursive contexts
The npm package subarg receives a total of 0 weekly downloads. As such, subarg popularity was classified as not popular.
We found that subarg demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.