0.3.0 • Published 9 years ago

minimist-events v0.3.0

Weekly downloads
70
License
MIT
Repository
github
Last release
9 years ago

minimist-events NPM version

Add events to minimist, ~30 sloc.

Example usage

var plugins = require('minimist-plugins');
var cli = plugins(require('minimist'))
  .use(require('minimist-events')());

cli.on('foo', function(val) {
  // do something when `foo` is emitted
});

cli.parse(process.argv.slice(2), function(err, argv) {
  console.log(argv);  
});

Install

Install with npm

$ npm i minimist-events --save

Usage

Setup listeners

Use a wildcard to listen to all events:

cli.on('*', function(key, val) {
  // do something when `key` is emitted
});

Or setup listeners for specific args you want to listen for:

cli.on('foo', function(val) {
  // do something when `foo` is emitted
});

cli.on('bar', function(val) {
  // do something when `bar` is emitted
});

cli.on('baz', function(val) {
  // do something when `baz` is emitted
});

cli.on('_', function(arr) {
  // do something when non-options args are emitted
});

Parse with minimist

Next, parse arguments. with minimist:

// the `parse` method is a proxy to minimist, 
// so pass any minimist args to this method
cli.parse(process.argv.slice(2), function(err, argv) {
  //=> parsed arguments
});

Complete example

Assuming --foo=bar a b c is passed on the command line:

var plugins = require('minimist-plugins');
var cli = plugins(require('minimist'))
  .use(require('minimist-events')());

cli.on('foo', function(val) {
  // - `val`: 'bar'
});
cli.on('a', function(idx, arr) {
  // - `idx`: '0'
  // - `arr`: ['a', 'b', 'c']
});
cli.on('b', function(idx, arr) {
  // - `idx`: '1'
  // - `arr`: ['a', 'b', 'c']
});
cli.on('c', function(idx, arr) {
  // - `idx`: '2'
  // - `arr`: ['a', 'b', 'c']
});

cli.parse(process.argv.slice(2), function(err, argv) {
  console.log(argv);  
});

Tip

process.argv is just an array, so you can mock command line arguments by just passing an array to cli.parse() (or minimist if you're not using this lib).

Example:

var args = ['--foo', '--bar=baz', 'a', 'b', 'c'];
cli.parse(args, function(err, argv) {
  //=> parsed args
});

options

help

The only option that minimist-events currently takes is help.

If options.help is true, when minimist returns an empty args object/array, help is emitted.

var plugins = require('minimist-plugins');
var cli = plugins(require('minimist'))
  .use(require('minimist-events')({help: true}));

cli.on('help', function () {
  console.log('Help!');
});

cli.parse(args, function(err, argv) {
  // argv => {_: []}
});

Events

Array arguments

An event is emitted for the _ array:

params

  • arr: the _ array
cli.on('_', function(arr) {
  // ['a', 'b', 'c']
});

cli.parse(['--foo', 'a', 'b', '--bar=baz', 'c'], cb);

Array items

An event is emitted for each item in the _ array.

params

  • i: array index
  • arr: the _ array
cli.on('a', function(i, arr) {
  // 0
});
cli.on('b', function(i, arr) {
  // 1
});
cli.on('c', function(i, arr) {
  // 2
});

cli.parse(['a', 'b', 'c'], cb);

Array index

An event is also emitted for each array index:

params

  • item: array item
  • arr: the _ array
cli.on(0, function(item, arr) {
  // 'a'
});
cli.on(1, function(item, arr) {
  // 'b'
});
cli.on(2, function(item, arr) {
  // 'c'
});

cli.parse(['a', 'b', 'c'], cb);

Option arguments

An event is emitted for each option.

Params

  • val: the option value
  • opts: the args object
cli.on('a', function(val) {
 //=> 'foo'
});

cli.on('b', function(val) {
 //=> 'bar'
});

cli.on('c', function(val) {
 //=> 'baz'
});

cli.parse(['--a=foo', '--b=bar', '--c=baz'], cb);

Related projects

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jon Schlinkert

Brian Woodward

License

Copyright © 2015 Jon Schlinkert Released under the MIT license.


This file was generated by verb-cli on August 15, 2015.