Code Monkey home page Code Monkey logo

visit-args's Introduction

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.

visit-args's People

Contributors

jonschlinkert avatar

Stargazers

Cat  avatar Charlike Mike Reagent avatar  avatar

Watchers

 avatar Brian Woodward avatar  avatar

visit-args's Issues

expand option (using expand-object)

I believe it should have expand option in line 40, like so

var val = opts.expand ? expandObject(args[key]) : args[key]

to things work more properly (for example things like set method). Because currently the way is to use expand-object from the event (method) listener and call the app method again with the expanded. Actually, even such workaround won't work.

What will be possible:

var cli = require('visit-args')
var argv = require('minimist')
var app = {
  cache: {},
  set: function (key, value) {
    obj.cache[key] = value
    return obj
  },
  get: function (key) {
    return obj.cache[key]
  }
}

cli.on('set', function (key, value) {
  // currently `value` is always undefined or boolean (if toBoolean options is true)
  console.log('set:', key, value) // => a, b
})
cli.on('get', function (key, value) {
  // value is undefined
  console.log('get:', key) // => b
})
cli.visit(app, argv(['--set=a:b', '--get=a']))

edit: found few bugs on expand-args and expand-object from here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.