Skip to content

Commit

Permalink
handle when action and option have same name, fixes #346
Browse files Browse the repository at this point in the history
  • Loading branch information
tonylukasavage committed Feb 10, 2015
1 parent 6135fbc commit d9a25a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,9 @@ Command.prototype.action = function(fn) {

fn.apply(self, args);
};
this.parent.on(this._name, listener);
if (this._alias) this.parent.on(this._alias, listener);
this._hasAction = true;
this.parent.on('action_' + this._name, listener);
if (this._alias) this.parent.on('action_' + this._alias, listener);
return this;
};

Expand Down Expand Up @@ -562,8 +563,8 @@ Command.prototype.parseArgs = function(args, unknown) {

if (args.length) {
name = args[0];
if (this.listeners(name).length) {
this.emit(args.shift(), args, unknown);
if (this.listeners('action_' + name).length) {
this.emit('action_' + args.shift(), args, unknown);
} else {
this.emit('*', args);
}
Expand Down
14 changes: 14 additions & 0 deletions test/test.options.same-name-arg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Module dependencies.
*/

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

program
.version('0.0.1')
.option('--string <n>', 'pass a string')

program.parse('node test string --string myString'.split(' '));
program.string.should.equal('myString');
program.args.should.containEql('string');

0 comments on commit d9a25a2

Please sign in to comment.