Quickly scan for CLI flags and arguments
This is a fast and lightweight alternative to minimist and yargs-parser.
It only exists because I find that I usually don't need most of what minimist and yargs-parser have to offer. However, mri is similar enough that it might function as a "drop-in replacement" for you, too!
See Comparisons for more info.
$ npm install --save mri
$ demo-cli --foo --bar=baz -mtv -- hello worldconst mri = require('mri');
const args = process.argv.slice(2);
mri(args);
//=> { _: ['hello', 'world'], foo:true, bar:'baz', m:true, t:true, v:true }
mri(args, { boolean:['bar'] });
//=> { _: ['baz', 'hello', 'world'], foo:true, bar:true, m:true, t:true, v:true }
mri(args, {
alias: {
b: 'bar',
foo: ['f', 'fuz']
}
});
//=> { _: ['hello', 'world'], foo:true, f:true, fuz:true, b:'baz', bar:'baz', m:true, t:true, v:true }Type: array
Default: []
An array of arguments to parse. For CLI usage, send process.argv.slice(2). See process.argv for info.
Type: object
Default: {}
An object of keys whose values are a string or array of aliases. These will be added to the parsed output with matching values.
Type: array|string
Default: []
A single key (or array of keys) that should be parsed as Booleans.
Type: object
Default: {}
An key:value object of defaults. If a default is provided for a key, its type (typeof) will be used to cast parsed arguments.
mri(['--foo', 'bar']);
//=> { _:[], foo:'bar' }
mri(['--foo', 'bar'], {
default:{ foo:true, baz:'hello', bat:42 }
});
//=> { _:['bar'], foo:true, baz:'hello', bat:42 }Note: Because
--foohas a default oftrue, its output is cast to a boolean. This means thatfoo:true, which makes'bar'an extra argument (_key).
Type: array|string
Default: []
A single key (or array of keys) that should be parsed as Strings.
mriis 2.5x faster (see benchmarks)- Numerical values are cast as
Numbers when possible- A key (and its aliases) will always honor
opts.booleanoropts.string
- A key (and its aliases) will always honor
- Short flag groups are treated as
Booleans by default:minimist(['-abc', 'hello']); //=> { _:[], a:'', b:'', c:'hello' } mri(['-abc', 'hello']); //=> { _:[], a:true, b:true, c:'hello' }
- Missing
options:opts.stopEarlyopts.unknownopts['--']
- Ignores newlines (
\n) within args (see test) - Ignores slashBreaks within args (see test)
- Ignores dot-nested flags (see test)
mriis 20x faster (see benchmarks)- Numerical values are cast as
Numbers when possible- A key (and its aliases) will always honor
opts.booleanoropts.string
- A key (and its aliases) will always honor
- Missing
options:opts.arrayopts.configopts.coerceopts.countopts.envPrefixopts.nargopts.normalizeopts.configurationopts.numberopts['--']
- Missing
parser.detailed()method - No additional configuration object
mri
--> 328,452 ops/sec ±0.28% (92 runs sampled)
yargs
--> 16,014 ops/sec ±0.69% (94 runs sampled)
minimist
--> 131,896 ops/sec ±0.92% (90 runs sampled)
MIT © Luke Edwards