From d9a25a2fb135318bc1e07d8b165fe16d7bebdf2f Mon Sep 17 00:00:00 2001 From: Tony Lukasavage Date: Tue, 10 Feb 2015 15:08:05 -0500 Subject: [PATCH] handle when action and option have same name, fixes #346 --- index.js | 9 +++++---- test/test.options.same-name-arg.js | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 test/test.options.same-name-arg.js diff --git a/index.js b/index.js index 0aecc16ec..f99472a37 100644 --- a/index.js +++ b/index.js @@ -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; }; @@ -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); } diff --git a/test/test.options.same-name-arg.js b/test/test.options.same-name-arg.js new file mode 100644 index 000000000..4ce05a1b4 --- /dev/null +++ b/test/test.options.same-name-arg.js @@ -0,0 +1,14 @@ +/** + * Module dependencies. + */ + +var program = require('../') + , should = require('should'); + +program + .version('0.0.1') + .option('--string ', 'pass a string') + +program.parse('node test string --string myString'.split(' ')); +program.string.should.equal('myString'); +program.args.should.containEql('string');