Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
The dashdash npm package is a lightweight parser for command-line options. It allows developers to easily define and parse options passed to Node.js scripts, providing a structured way to handle command-line arguments.
Option Parsing
This feature allows you to define a set of options and parse them from the command line. The example code demonstrates how to create a parser, define options, and parse them from `process.argv`.
const dashdash = require('dashdash');
const options = [
{ name: 'verbose', type: 'bool', help: 'Print verbose output' },
{ name: 'file', type: 'string', help: 'File to process' }
];
const parser = dashdash.createParser({options: options});
try {
const opts = parser.parse(process.argv);
console.log('Options:', opts);
} catch (e) {
console.error('Error parsing options:', e.message);
process.exit(1);
}
Help Text Generation
Dashdash can automatically generate help text for the defined options. This code sample shows how to add a help option and print the generated help text when it's used.
const dashdash = require('dashdash');
const options = [
{ name: 'help', type: 'bool', help: 'Print this help and exit.' }
];
const parser = dashdash.createParser({options: options});
if (opts.help) {
var help = parser.help().trimRight();
console.log('usage: node myscript.js [OPTIONS]
' + help);
process.exit(0);
}
Commander is a more feature-rich command-line option parsing library. It provides a high-level API for defining commands, options, and actions, and it's widely used in the Node.js community. Compared to dashdash, commander offers a more expressive syntax and additional features like command-specific help, custom option processing, and subcommands.
Yargs is another popular command-line option parser that provides a fluent interface for building complex command-line applications. It supports features like command chaining, argument validation, and context-aware help menus. Yargs is more suitable for applications that require a rich command-line interface, while dashdash is simpler and more focused on option parsing.
Minimist is a minimalistic option parsing library that focuses on simplicity and performance. It does not have the feature set of dashdash, such as typed options or automatic help generation, but it is very lightweight and easy to use for basic argument parsing needs.
A light, featureful and explicit option parsing library for node.js.
Why another one? See below. tl;dr: The others I've tried are one of too loosey goosey (not explicit), too big/too many deps, or ill specified. YMMV.
Follow @trentmick for updates to node-dashdash.
npm install dashdash
var dashdash = require('dashdash');
// Specify the options. Minimally `name` (or `names`) and `type`
// must be given for each.
var options = [
{
// `names` or a single `name`. First element is the `opts.KEY`.
names: ['help', 'h'],
// See "Option specs" below for types.
type: 'bool',
help: 'Print this help and exit.'
}
];
// Shortcut form. As called it infers `process.argv`. See below for
// the longer form to use methods like `.help()` on the Parser object.
var opts = dashdash.parse({options: options});
console.log("opts:", opts);
console.log("args:", opts._args);
A more realistic starter script "foo.js" is as follows.
This also shows using parser.help()
for formatted option help.
var dashdash = require('./lib/dashdash');
var options = [
{
name: 'version',
type: 'bool',
help: 'Print tool version and exit.'
},
{
names: ['help', 'h'],
type: 'bool',
help: 'Print this help and exit.'
},
{
names: ['verbose', 'v'],
type: 'arrayOfBool',
help: 'Verbose output. Use multiple times for more verbose.'
},
{
names: ['file', 'f'],
type: 'string',
help: 'File to process',
helpArg: 'FILE'
}
];
var parser = dashdash.createParser({options: options});
try {
var opts = parser.parse(process.argv);
} catch (e) {
console.error('foo: error: %s', e.message);
process.exit(1);
}
console.log("# opts:", opts);
console.log("# args:", opts._args);
// Use `parser.help()` for formatted options help.
if (opts.help) {
var help = parser.help({includeEnv: true}).trimRight();
console.log('usage: node foo.js [OPTIONS]\n'
+ 'options:\n'
+ help);
process.exit(0);
}
// ...
Some example output from this script (foo.js):
$ node foo.js -h
# opts: { help: true,
_order: [ { name: 'help', value: true, from: 'argv' } ],
_args: [] }
# args: []
usage: node foo.js [OPTIONS]
options:
--version Print tool version and exit.
-h, --help Print this help and exit.
-v, --verbose Verbose output. Use multiple times for more verbose.
-f FILE, --file=FILE File to process
$ node foo.js -v
# opts: { verbose: [ true ],
_order: [ { name: 'verbose', value: true, from: 'argv' } ],
_args: [] }
# args: []
$ node foo.js --version arg1
# opts: { version: true,
_order: [ { name: 'version', value: true, from: 'argv' } ],
_args: [ 'arg1' ] }
# args: [ 'arg1' ]
$ node foo.js -f bar.txt
# opts: { file: 'bar.txt',
_order: [ { name: 'file', value: 'bar.txt', from: 'argv' } ],
_args: [] }
# args: []
$ node foo.js -vvv --file=blah
# opts: { verbose: [ true, true, true ],
file: 'blah',
_order:
[ { name: 'verbose', value: true, from: 'argv' },
{ name: 'verbose', value: true, from: 'argv' },
{ name: 'verbose', value: true, from: 'argv' },
{ name: 'file', value: 'blah', from: 'argv' } ],
_args: [] }
# args: []
See the "examples" dir for a number of starter examples using some of dashdash's features.
If you want to allow environment variables to specify options to your tool, dashdash makes this easy. We can change the 'verbose' option in the example above to include an 'env' field:
{
names: ['verbose', 'v'],
type: 'arrayOfBool',
env: 'FOO_VERBOSE', // <--- add this line
help: 'Verbose output. Use multiple times for more verbose.'
},
then the "FOO_VERBOSE" environment variable can be used to set this option:
$ FOO_VERBOSE=1 node foo.js
# opts: { verbose: [ true ],
_order: [ { name: 'verbose', value: true, from: 'env' } ],
_args: [] }
# args: []
Boolean options will interpret the empty string as unset, '0' as false and anything else as true.
$ FOO_VERBOSE= node examples/foo.js # not set
# opts: { _order: [], _args: [] }
# args: []
$ FOO_VERBOSE=0 node examples/foo.js # '0' is false
# opts: { verbose: [ false ],
_order: [ { key: 'verbose', value: false, from: 'env' } ],
_args: [] }
# args: []
$ FOO_VERBOSE=1 node examples/foo.js # true
# opts: { verbose: [ true ],
_order: [ { key: 'verbose', value: true, from: 'env' } ],
_args: [] }
# args: []
$ FOO_VERBOSE=boogabooga node examples/foo.js # true
# opts: { verbose: [ true ],
_order: [ { key: 'verbose', value: true, from: 'env' } ],
_args: [] }
# args: []
Non-booleans can be used as well. Strings:
$ FOO_FILE=data.txt node examples/foo.js
# opts: { file: 'data.txt',
_order: [ { key: 'file', value: 'data.txt', from: 'env' } ],
_args: [] }
# args: []
Numbers:
$ FOO_TIMEOUT=5000 node examples/foo.js
# opts: { timeout: 5000,
_order: [ { key: 'timeout', value: 5000, from: 'env' } ],
_args: [] }
# args: []
$ FOO_TIMEOUT=blarg node examples/foo.js
foo: error: arg for "FOO_TIMEOUT" is not a positive integer: "blarg"
With the includeEnv: true
config to parser.help()
the environment
variable can also be included in help output:
usage: node foo.js [OPTIONS]
options:
--version Print tool version and exit.
-h, --help Print this help and exit.
-v, --verbose Verbose output. Use multiple times for more verbose.
Environment: FOO_VERBOSE=1
-f FILE, --file=FILE File to process
Parser construction (i.e. dashdash.createParser(CONFIG)
) takes the
following fields:
options
(Array of option specs). Required. See the
Option specs section below.
interspersed
(Boolean). Optional. Default is true. If true this allows
interspersed arguments and options. I.e.:
node ./tool.js -v arg1 arg2 -h # '-h' is after interspersed args
Set it to false to have '-h' not get parsed as an option in the above example.
allowUnknown
(Boolean). Optional. Default is false. If false, this causes
unknown arguments to throw an error. I.e.:
node ./tool.js -v arg1 --afe8asefksjefhas
Set it to true to treat the unknown option as a positional argument.
Caveat: When a shortopt group, such as -xaz
contains a mix of
known and unknown options, the entire group is passed through
unmolested as a positional argument.
Consider if you have a known short option -a
, and parse the
following command line:
node ./tool.js -xaz
where -x
and -z
are unknown. There are multiple ways to
interpret this:
-x
takes a value: {x: 'az'}
-x
and -z
are both booleans: {x:true,a:true,z:true}
Since dashdash does not know what -x
and -z
are, it can't know
if you'd prefer to receive {a:true,_args:['-x','-z']}
or
{x:'az'}
, or {_args:['-xaz']}
. Leaving the positional arg unprocessed
is the easiest mistake for the user to recover from.
Example using all fields:
{
names: ['file', 'f'], // Required (or `name`).
type: 'string', // Required.
env: 'MYTOOL_FILE',
help: 'Config file to load before running "mytool"',
helpArg: 'PATH',
helpWrap: false,
default: path.resolve(process.env.HOME, '.mytoolrc')
}
Each option spec in the options
array must/can have the following fields:
name
(String) or names
(Array). Required. These give the option name
and aliases. The first name (if more than one given) is the key for the
parsed opts
object.
type
(String). Required. One of:
YYYY-MM-DD[THH:MM:SS[.sss][Z]]
, e.g. "2014-03-28T18:35:01.489Z")FWIW, these names attempt to match with asserts on
assert-plus.
You can add your own custom option types with dashdash.addOptionType
.
See below.
env
(String or Array of String). Optional. An environment variable name
(or names) that can be used as a fallback for this option. For example,
given a "foo.js" like this:
var options = [{names: ['dry-run', 'n'], env: 'FOO_DRY_RUN'}];
var opts = dashdash.parse({options: options});
Both node foo.js --dry-run
and FOO_DRY_RUN=1 node foo.js
would result
in opts.dry_run = true
.
An environment variable is only used as a fallback, i.e. it is ignored if
the associated option is given in argv
.
help
(String). Optional. Used for parser.help()
output.
helpArg
(String). Optional. Used in help output as the placeholder for
the option argument, e.g. the "PATH" in:
...
-f PATH, --file=PATH File to process
...
helpWrap
(Boolean). Optional, default true. Set this to false
to have
that option's help
not be text wrapped in <parser>.help()
output.
default
. Optional. A default value used for this option, if the
option isn't specified in argv.
You can add headings between option specs in the options
array. To do so,
simply add an object with only a group
property -- the string to print as
the heading for the subsequent options in the array. For example:
var options = [
{
group: 'Armament Options'
},
{
names: [ 'weapon', 'w' ],
type: 'string'
},
{
group: 'General Options'
},
{
names: [ 'help', 'h' ],
type: 'bool'
}
];
...
Note: You can use an empty string, {group: ''}
, to get a blank line in help
output between groups of options.
The parser.help(...)
function is configurable as follows:
Options:
Armament Options:
^^ -w WEAPON, --weapon=WEAPON Weapon with which to crush. One of: |
/ sword, spear, maul |
/ General Options: |
/ -h, --help Print this help and exit. |
/ ^^^^ ^ |
\ `-- indent `-- helpCol maxCol ---'
`-- headingIndent
indent
(Number or String). Default 4. Set to a number (for that many
spaces) or a string for the literal indent.headingIndent
(Number or String). Default half length of indent
. Set to
a number (for that many spaces) or a string for the literal indent. This
indent applies to group heading lines, between normal option lines.nameSort
(String). Default is 'length'. By default the names are
sorted to put the short opts first (i.e. '-h, --help' preferred
to '--help, -h'). Set to 'none' to not do this sorting.maxCol
(Number). Default 80. Note that reflow is just done on whitespace
so a long token in the option help can overflow maxCol.helpCol
(Number). If not set a reasonable value will be determined
between minHelpCol
and maxHelpCol
.minHelpCol
(Number). Default 20.maxHelpCol
(Number). Default 40.helpWrap
(Boolean). Default true. Set to false
to have option help
strings not be textwrapped to the helpCol..maxCol range.includeEnv
(Boolean). Default false. If the option has associated
environment variables (via the env
option spec attribute), then
append mentioned of those envvars to the help string.Dashdash includes a good starter set of option types that it will parse for you. However you can add your own via:
var dashdash = require('dashdash');
dashdash.addOptionType({
name: '...',
takesArg: true,
helpArg: '...',
parseArg: function (option, optstr, arg) {
...
}
});
For example, a simple option type that accepts 'yes', 'y', 'no' or 'n' as a boolean argument would look like:
var dashdash = require('dashdash');
function parseYesNo(option, optstr, arg) {
var argLower = arg.toLowerCase()
if (~['yes', 'y'].indexOf(argLower)) {
return true;
} else if (~['no', 'n'].indexOf(argLower)) {
return false;
} else {
throw new Error(format(
'arg for "%s" is not "yes" or "no": "%s"',
optstr, arg));
}
}
dashdash.addOptionType({
name: 'yesno'
takesArg: true,
helpArg: '<yes|no>',
parseArg: parseYesNo
});
var options = {
{names: ['answer', 'a'], type: 'yesno'}
};
var opts = dashdash.parse({options: options});
See "examples/custom-option-type.js" for another example adding a "duration" option type. Please let me know on twitter or with an issue if you write a generally useful one.
Why another node.js option parsing lib?
nopt
really is just for "tools like npm". Implicit opts (e.g. '--no-foo'
works for every '--foo'). Can't disable abbreviated opts. Can't do multiple
usages of same opt, e.g. '-vvv' (I think). Can't do grouped short opts.
optimist
has surprise interpretation of options (at least to me).
Implicit opts mean ambiguities and poor error handling for fat-fingering.
process.exit
calls makes it hard to use as a libary.
optparse
Incomplete docs. Is this an attempted clone of Python's optparse
.
Not clear. Some divergence. parser.on("name", ...)
API is weird.
argparse
Dep on underscore. No thanks just for option processing.
find lib | wc -l
-> 26
. Overkill.
Argparse is a bit different anyway. Not sure I want that.
posix-getopt
No type validation. Though that isn't a killer. AFAIK can't
have a long opt without a short alias. I.e. no getopt_long
semantics.
Also, no whizbang features like generated help output.
"commander.js": I wrote a critique a while back. It seems fine, but last I checked had an outstanding bug that would prevent me from using it.
MIT. See LICENSE.txt.
FAQs
A light, featureful and explicit option parsing library.
We found that dashdash 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.