Philipp Partosch 46a936d7de added all files to project | 2 years ago | |
---|---|---|
.. | ||
lib | 2 years ago | |
license.md | 2 years ago | |
package.json | 2 years ago | |
readme.md | 2 years ago |
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 world
const mri = require('mri');
const argv = process.argv.slice(2);
mri(argv);
//=> { _: ['hello', 'world'], foo:true, bar:'baz', m:true, t:true, v:true }
mri(argv, { boolean:['bar'] });
//=> { _: ['baz', 'hello', 'world'], foo:true, bar:true, m:true, t:true, v:true }
mri(argv, {
alias: {
b: 'bar',
foo: ['f', 'fuz']
}
});
//=> { _: ['hello', 'world'], foo:true, f:true, fuz:true, b:'baz', bar:'baz', m:true, t:true, v:true }
Return: Object
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 String
s or Array<String>
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 Boolean
s.
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
--foo
has a default oftrue
, its output is cast to a Boolean. This means thatfoo=true
, making'bar'
an extra argument (_
key).
Type: Array|String
Default: []
A single key (or array of keys) that should be parsed as String
s.
Type: Function
Default: undefined
Callback that is run when a parsed flag has not been defined as a known key or alias. Its only parameter is the unknown flag itself; eg --foobar
or -f
.
Once an unknown flag is encountered, parsing will terminate, regardless of your return value.
Note:
mri
only checks for unknown flags ifoptions.unknown
andoptions.alias
are populated. Otherwise, everything will be accepted.
mri
is 5x faster (see benchmarks)Number
s when possible
opts.boolean
or opts.string
Short flag groups are treated as Boolean
s by default:
minimist(['-abc', 'hello']);
//=> { _:[], a:'', b:'', c:'hello' }
mri(['-abc', 'hello']);
//=> { _:[], a:true, b:true, c:'hello' }
The opts.unknown
behaves differently:
minimist
, mri
will not continue continue parsing after encountering an unknown flagMissing options
:
opts.stopEarly
opts['--']
Ignores newlines (\n
) within args (see test)
Ignores slashBreaks within args (see test)
Ignores dot-nested flags (see test)
mri
is 40x faster (see benchmarks)Number
s when possible
opts.boolean
or opts.string
options
:
opts.array
opts.config
opts.coerce
opts.count
opts.envPrefix
opts.narg
opts.normalize
opts.configuration
opts.number
opts['--']
parser.detailed()
methodoptions.unknown
featureRunning Node.js v10.13.0
minimist x 312,417 ops/sec ±0.85% (93 runs sampled)
mri x 1,641,208 ops/sec ±0.24% (93 runs sampled)
nopt x 910,276 ops/sec ±1.11% (88 runs sampled)
yargs-parser x 40,943 ops/sec ±1.37% (93 runs sampled)
MIT © Luke Edwards