Skip to content

Commit

Permalink
Merge pull request #347 from tonylukasavage/issue-346
Browse files Browse the repository at this point in the history
Issue #346, fix collisions when option and first arg have same name
  • Loading branch information
SomeKittens committed Feb 26, 2015
2 parents 2120cb1 + 98a390b commit f2e000b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ 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.parent.on('action_' + this._name, listener);
if (this._alias) this.parent.on('action_' + this._alias, listener);
return this;
};

Expand Down Expand Up @@ -564,8 +564,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 f2e000b

Please sign in to comment.