Skip to content

Commit

Permalink
Merge pull request #262 from tonylukasavage/options_func
Browse files Browse the repository at this point in the history
add opts() for getting key-value pairs of options
  • Loading branch information
thethomaseffect committed Sep 21, 2014
2 parents c0fcf8f + ed8c945 commit 6089ed4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,23 @@ Command.prototype.parseOptions = function(argv){
return { args: args, unknown: unknownOptions };
};

/**
* Return an object containing options as key-value pairs
*
* @return {Object}
* @api public
*/
Command.prototype.opts = function() {
var result = {}
, len = this.options.length;

for (var i = 0 ; i < len; i++) {
var key = this.options[i].name();
result[key] = key === 'version' ? this._version : this[key];
}
return result;
};

/**
* Argument `name` is missing.
*
Expand Down
21 changes: 21 additions & 0 deletions test/test.options.func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var program = require('../')
, should = require('should');

program
.version('0.0.1')
.description('sdfdsfsfsdfdsf')
.option('-f, --foo', 'add some foo')
.option('-b, --bar', 'add some bar')
.option('-M, --no-magic', 'disable magic')
.option('-q, --quux <quux>', 'add some quux');

program.parse(['node', 'test', '--foo', '--bar', '--no-magic', '--quux', 'value']);
program.opts.should.be.a.Function;

var opts = program.opts();
opts.should.be.an.Object;
opts.version.should.equal('0.0.1');
opts.foo.should.be.true;
opts.bar.should.be.true;
opts.magic.should.be.false;
opts.quux.should.equal('value');

0 comments on commit 6089ed4

Please sign in to comment.