0.1.0 • Published 9 years ago

visit-args v0.1.0

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

visit-args NPM version

Visit application methods that map directly to command line arguments and emit events for flags.

Install

Install with npm

$ npm i visit-args --save

Usage

First, an example, so the following docs make sense.

var argv = require('minimist');
var cli = require('visit-args');

// see the `app` example in `./examples/app.js` for details
var App = require('./examples/app');
var app = new App();

cli.on('set', function (key, val) {
  // do stuff with key/val 
});

cli.on('get', function (key, val) {
  // do stuff with key/val
});

cli.visit(app, argv(['--set=a', '--get=a']));

How this works

The idea is that app is an object with methods on it, and when a command line flag matches the name of a method on app, that method is called and is passed any additional arguments related to that method call (that's the tricky part: "additional arguments". Which is what this lib does - figure out how to invoke that method with the right arguments...)

Method arguments

This brings up the question: "What is passed to the method when it's called, if all we have is a command line flag?". That's a good question. This is easiest to explain by way of examples. Let's say you pass the following flag:

$ --a=b

Minimist would parse this to {a: 'b'}. Next, if you're application happens to have a method named a, then visit-args would invoke the method and pass b to it. A more meaningful example might be something like:

$ --del=foo

Which would invoke the del method on app, with the foo argument. In other words:

app.del('foo');

If, more often than not, your methods need more information than just a string or boolean to be able to take any kind of meaningful action, then it's worth considering using a library like expand-args, which will post-process arguments after minimist, but before visit-args, so that the following is possible:

$ --set=a:b

Which would invoke the following on app:

app.set('a', 'b');

See expand-args and expand-object for more details.

Related projects

  • expand-object: Expand a string into a JavaScript object using a simple notation. Use the CLI or… more
  • expand-args: Expand parsed command line arguments using expand-object.
  • minimist-plugins: Simple wrapper to make minimist pluggable. ~20 sloc.
  • minimist: parse argument options

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

License

Copyright © 2015 Jon Schlinkert Released under the MIT license.


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