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

Prefix events to prevent conflicts between commands and options #494

Merged
merged 1 commit into from
Jun 27, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ Command.prototype.action = function(fn) {
};
var parent = this.parent || this;
var name = parent === this ? '*' : this._name;
parent.on(name, listener);
if (this._alias) parent.on(this._alias, listener);
parent.on('command:' + name, listener);
if (this._alias) parent.on('command:' + this._alias, listener);
return this;
};

Expand Down Expand Up @@ -390,7 +390,7 @@ Command.prototype.option = function(flags, description, fn, defaultValue) {

// when it's passed assign the value
// and conditionally invoke the callback
this.on(oname, function(val) {
this.on('option:' + oname, function(val) {
// coercion
if (null !== val && fn) val = fn(val, undefined === self[name]
? defaultValue
Expand Down Expand Up @@ -611,10 +611,10 @@ 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('command:' + name).length) {
this.emit('command:' + args.shift(), args, unknown);
} else {
this.emit('*', args);
this.emit('command:*', args);
}
} else {
outputHelpIfNecessary(this, unknown);
Expand Down Expand Up @@ -687,7 +687,7 @@ Command.prototype.parseOptions = function(argv) {
if (option.required) {
arg = argv[++i];
if (null == arg) return this.optionMissingArgument(option);
this.emit(option.name(), arg);
this.emit('option:' + option.name(), arg);
// optional arg
} else if (option.optional) {
arg = argv[i+1];
Expand All @@ -696,10 +696,10 @@ Command.prototype.parseOptions = function(argv) {
} else {
++i;
}
this.emit(option.name(), arg);
this.emit('option:' + option.name(), arg);
// bool
} else {
this.emit(option.name());
this.emit('option:' + option.name());
}
continue;
}
Expand Down Expand Up @@ -820,7 +820,7 @@ Command.prototype.version = function(str, flags) {
this._version = str;
flags = flags || '-V, --version';
this.option(flags, 'output the version number');
this.on('version', function() {
this.on('option:version', function() {
process.stdout.write(str + '\n');
process.exit(0);
});
Expand Down
25 changes: 25 additions & 0 deletions test/test.command.noConflict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var program = require('../')
, sinon = require('sinon')
, should = require('should');

sinon.stub(process, 'exit');
sinon.stub(process.stdout, 'write');

program
.version('0.0.1')
.command('version')
.action(function() {
console.log('Version command invoked');
});

program.parse(['node', 'test', 'version']);

var output = process.stdout.write.args[0];
output[0].should.equal('Version command invoked\n');

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

var output = process.stdout.write.args[1];
output[0].should.equal('0.0.1\n');

sinon.restore();