What is subarg?
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.
What are subarg's main functionalities?
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
Other packages similar to subarg
minimist
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
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
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.
subarg
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.
example
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 } }
methods
var subarg = require('subarg')
var argv = subarg(args, opts)
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.
install
With npm do:
npm install subarg
license
MIT