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 program.opts namespace for all options #35

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions lib/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function Command(name) {
this.commands = [];
this.options = [];
this.args = [];
this.opts = {};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we change it to .options im down

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't that conflict with how it's currently using .options (as an Array of option objects)?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arrays can have props, iirc that one is private anyway

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems to defeat the purpose of namespacing them. Take this example:

program
  .option('-a, --anchovies', 'Add anchovies?')
  .option('-c, --cheese [type]', 'optionally specify the type of cheese', 'mozzarella');

program.parse(['node', 'test']);
console.log('Expected: ', program.opts);
console.log('Not expected: ', program.options);

Ouput:

Expected:  { anchovies: false, cheese: 'mozzarella' }
Not expected:  [ { flags: '-a, --anchovies',
    required: 0,
    optional: 0,
    bool: true,
    short: '-a',
    long: '--anchovies',
    description: 'Add anchovies?' },
{ flags: '-c, --cheese [type]',
    required: 0,
    optional: -14,
    bool: true,
    short: '-c',
    long: '--cheese',
    description: 'optionally specify the type of cheese' },
anchovies: false,
cheese: 'mozzarella' ]

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's just rename the internal one to _options

this.name = name;
}

Expand Down Expand Up @@ -291,13 +292,18 @@ Command.prototype.option = function(flags, description, fn, defaultValue){

// default as 3rd arg
if ('function' != typeof fn) defaultValue = fn, fn = null;

// preassign default value only for --no-*, [optional], or <required>
if (false == option.bool || option.optional || option.required) {
// when --no-* we make sure default is true
if (false == option.bool) defaultValue = true;

// preassign only if we have a default
if (undefined !== defaultValue) self[name] = defaultValue;
if (undefined !== defaultValue) self.assign(name, defaultValue);

} else {
// set opt to false otherwise
self.opts[name] = false;
}

// register the option
Expand All @@ -313,21 +319,34 @@ Command.prototype.option = function(flags, description, fn, defaultValue){
if ('boolean' == typeof self[name] || 'undefined' == typeof self[name]) {
// if no value, bool true, and we have a default, then use it!
if (null == val) {
self[name] = option.bool
self.assign(name, option.bool
? defaultValue || true
: false;
: false);
} else {
self[name] = val;
self.assign(name, val);
}
} else if (null !== val) {
// reassign
self[name] = val;
self.assign(name, val);
}
});

return this;
};

/**
* Assign the option name `key` with the given `value`.
*
* @param {String} key
* @param {mixed} value
* @return {Command} for chaining
* @api private
*/
Command.prototype.assign = function(key, value) {
this.opts[key] = this[key] = value;
return this;
};

/**
* Parse `argv`, settings options and invoking commands when defined.
*
Expand Down
1 change: 1 addition & 0 deletions test/test.options.args.optional.given.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ program

program.parse(['node', 'test', '--cheese', 'feta']);
program.cheese.should.equal('feta');
program.opts.cheese.should.equal('feta');
1 change: 1 addition & 0 deletions test/test.options.args.optional.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ program

program.parse(['node', 'test', '--cheese']);
program.cheese.should.be.true;
program.opts.cheese.should.be.true;
3 changes: 3 additions & 0 deletions test/test.options.bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ program
program.parse(['node', 'test', '--pepper']);
program.pepper.should.be.true;
program.cheese.should.be.true;

program.opts.pepper.should.be.true;
program.opts.cheese.should.be.true;
1 change: 1 addition & 0 deletions test/test.options.bool.no.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ program
program.parse(['node', 'test', '--no-cheese']);
should.equal(undefined, program.pepper);
program.cheese.should.be.false;
program.opts.cheese.should.be.false;
3 changes: 3 additions & 0 deletions test/test.options.bool.small.combined.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ program
program.parse(['node', 'test', '-pc']);
program.pepper.should.be.true;
program.cheese.should.be.false;

program.opts.pepper.should.be.true;
program.opts.cheese.should.be.false;
3 changes: 3 additions & 0 deletions test/test.options.bool.small.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ program
program.parse(['node', 'test', '-p', '-c']);
program.pepper.should.be.true;
program.cheese.should.be.false;

program.opts.pepper.should.be.true;
program.opts.cheese.should.be.false;
7 changes: 7 additions & 0 deletions test/test.options.camelcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ program.myFLOAT.should.equal(5.5);
program.myVeryLongFloat.should.equal(6.5);
program.myURLCount.should.equal(7.5);
program.myLongRange.should.eql([1,5]);

program.opts.myInt.should.equal(5);
program.opts.myNum.should.equal(15.99);
program.opts.myFLOAT.should.equal(5.5);
program.opts.myVeryLongFloat.should.equal(6.5);
program.opts.myURLCount.should.equal(7.5);
program.opts.myLongRange.should.eql([1,5]);
5 changes: 5 additions & 0 deletions test/test.options.coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ program.int.should.equal(5);
program.num.should.equal(15.99);
program.float.should.equal(5.5);
program.range.should.eql([1,5]);

program.opts.int.should.equal(5);
program.opts.num.should.equal(15.99);
program.opts.float.should.equal(5.5);
program.opts.range.should.eql([1,5]);
7 changes: 7 additions & 0 deletions test/test.options.defaults.given.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ program.should.have.property('olives', 'black');
program.should.have.property('sauce', false);
program.should.have.property('crust', 'thin');
program.should.have.property('cheese', 'wensleydale');

program.opts.should.have.property('anchovies', true);
program.opts.should.have.property('onions', true);
program.opts.should.have.property('olives', 'black');
program.opts.should.have.property('sauce', false);
program.opts.should.have.property('crust', 'thin');
program.opts.should.have.property('cheese', 'wensleydale');
7 changes: 7 additions & 0 deletions test/test.options.defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ program.should.not.have.property('olives');
program.should.have.property('sauce', true);
program.should.have.property('crust', 'hand-tossed');
program.should.have.property('cheese', 'mozzarella');

program.opts.should.have.property('anchovies', false);
program.opts.should.have.property('onions', false);
program.opts.should.have.property('olives', false);
program.opts.should.have.property('sauce', true);
program.opts.should.have.property('crust', 'hand-tossed');
program.opts.should.have.property('cheese', 'mozzarella');
31 changes: 31 additions & 0 deletions test/test.options.defaults.not.given.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Module dependencies.
*/

var program = require('../')
, should = require('should');

program
.version('0.0.1')
.option('-a, --anchovies', 'Add anchovies?')
.option('-o, --onions', 'Add onions?')
.option('-v, --olives', 'Add olives? Sorry we only have black.', 'black')
.option('-s, --no-sauce', 'Uh… okay')
.option('-r, --crust <type>', 'What kind of crust would you like?', 'hand-tossed')
.option('-c, --cheese [type]', 'optionally specify the type of cheese', 'mozzarella');

program.parse(['node', 'test', '--olives'])

program.should.not.have.property('anchovies');
program.should.not.have.property('onions');
program.should.have.property('olives', 'black');
program.should.have.property('sauce', true);
program.should.have.property('crust', 'hand-tossed');
program.should.have.property('cheese', 'mozzarella');

program.opts.should.have.property('anchovies', false);
program.opts.should.have.property('onions', false);
program.opts.should.have.property('olives', 'black');
program.opts.should.have.property('sauce', true);
program.opts.should.have.property('crust', 'hand-tossed');
program.opts.should.have.property('cheese', 'mozzarella');
2 changes: 2 additions & 0 deletions test/test.options.large-only-with-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ program

program.parse(['node', 'test', '--longflag', 'something']);
program.longflag.should.equal('something');

program.opts.longflag.should.equal('something');
2 changes: 2 additions & 0 deletions test/test.options.large-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ program

program.parse(['node', 'test', '--verbose']);
program.verbose.should.be.true;

program.opts.verbose.should.be.true;