Skip to content

Commit

Permalink
add args() plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 28, 2012
1 parent a465eb3 commit 5eaeaed
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ REPORTER=spec
test:
@./node_modules/.bin/mocha \
--require should \
--bail \
--reporter $(REPORTER)

test-cov: lib-cov
Expand Down
9 changes: 9 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ yields:
{ "upload path": "/www/example.com/tmp" }
```

### eson.args([args])

Parse from the given `args` or __ARGV__. For example if you have a setting
named "dev ui" with a default value of `false`, `--dev-ui` would enable it,
or `--dev-ui yes` would provide the value "yes" which is of course also truthy.

To compliment `--NAME` you may also negate this, if "dev ui" is enabled by default
then you may use `--no-dev-ui` to disable it.

### eson.glob

The glob plugin allows you to specify glob strings, prefixed by "glob":
Expand Down
1 change: 1 addition & 0 deletions lib/eson.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports.version = '0.0.1';

exports.ms = require('./plugins/ms');
exports.env = require('./plugins/env');
exports.args = require('./plugins/args');
exports.glob = require('./plugins/glob');
exports.bools = require('./plugins/bools');
exports.replace = require('./plugins/replace');
Expand Down
30 changes: 30 additions & 0 deletions lib/plugins/args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

/**
* Allow ARGV to take precedence over `val`, supports:
*
* --NAME
* --NAME value
* --no-NAME
*
*/

module.exports = function(args){
args = args || process.argv.slice(1);
return function(key, val){
var name = key.split(' ').join('-');

for (var i = 0, len = args.length; i < len; ++i) {
// --NAME
if (args[i] == '--' + name) {
// --NAME val
if (args[i + 1] && '-' != args[i + 1]) return args[++i];
return true;
}

// --no-NAME
if (args[i] == '--no-' + name) return false;
}

return val;
};
};
23 changes: 23 additions & 0 deletions test/args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

var Parser = require('../')
, args = Parser.args;

describe('args', function(){
it('should support --NAME', function(){
var fn = args(['foo', 'bar']);
fn('dev ui', false).should.be.false;

fn = args(['foo', '--dev-ui']);
fn('dev ui', false).should.be.true;
})

it('should support --no-NAME', function(){
var fn = args(['foo', '--no-dev-ui']);
fn('dev ui', true).should.be.false;
})

it('should support --NAME val', function(){
var fn = args(['foo', '--dev-ui', 'yes']);
fn('dev ui', false).should.equal('yes');
})
})

0 comments on commit 5eaeaed

Please sign in to comment.