Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add args() plugin

  • Loading branch information...
commit 5eaeaed3330bc085e5286af2d855d012a6f13216 1 parent a465eb3
@tj authored
View
1  Makefile
@@ -4,6 +4,7 @@ REPORTER=spec
test:
@./node_modules/.bin/mocha \
--require should \
+ --bail \
--reporter $(REPORTER)
test-cov: lib-cov
View
9 Readme.md
@@ -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":
View
1  lib/eson.js
@@ -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');
View
30 lib/plugins/args.js
@@ -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;
+ };
+};
View
23 test/args.js
@@ -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');
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.