Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add opts() for getting key-value pairs #262

Merged
merged 1 commit into from
Sep 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');