Prefix events to prevent conflicts between commands and options (#494)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Augusto Franzoia · committed by Tomoaki Abe · parent 85450a0 · Browse files · 2 files·+35 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+35 added−10 removed
- tests:+25 added−0 removed
- other:+10 added−10 removed
Provenance
- Commit
78c563d- Tree
3fc454a- Author
Augusto Franzoia <ajfranzoia@gmail.com>- Committer
Tomoaki Abe <abe@enzou.tokyo>- Parent
85450a0
2 files changed+35 added−10 removed
added:test/test.command.noConflict.js+25 added
12345678910111213141516171819202122232425var 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();modified:index.js+10 added-10 removed
299 unmodified lines300 fn.apply(self, args);301 };302 var parent = this.parent || this;303 var name = parent === this ? '*' : this._name;304- parent.on(name, listener);305- if (this._alias) parent.on(this._alias, listener);304+ parent.on('command:' + name, listener);305+ if (this._alias) parent.on('command:' + this._alias, listener);306 return this;307 };308 309 /**78 unmodified lines388 this.options.push(option);389 390 // when it's passed assign the value391 // and conditionally invoke the callback392- this.on(oname, function(val) {392+ this.on('option:' + oname, function(val) {393 // coercion394 if (null !== val && fn) val = fn(val, undefined === self[name]395 ? defaultValue396 : self[name]);225 unmodified lines622 var name;623 624 if (args.length) {625 name = args[0];626- if (this.listeners(name).length) {627- this.emit(args.shift(), args, unknown);626+ if (this.listeners('command:' + name).length) {627+ this.emit('command:' + args.shift(), args, unknown);628 } else {629- this.emit('*', args);629+ this.emit('command:*', args);630 }631 } else {632 outputHelpIfNecessary(this, unknown);633 64 unmodified lines698 // requires arg699 if (option.required) {700 arg = argv[++i];701 if (null == arg) return this.optionMissingArgument(option);702- this.emit(option.name(), arg);702+ this.emit('option:' + option.name(), arg);703 // optional arg704 } else if (option.optional) {705 arg = argv[i+1];706 if (null == arg || ('-' == arg[0] && '-' != arg)) {707 arg = null;708 } else {709 ++i;710 }711- this.emit(option.name(), arg);711+ this.emit('option:' + option.name(), arg);712 // bool713 } else {714- this.emit(option.name());714+ this.emit('option:' + option.name());715 }716 continue;717 }718 112 unmodified lines831 if (0 == arguments.length) return this._version;832 this._version = str;833 flags = flags || '-V, --version';834 this.option(flags, 'output the version number');835- this.on('version', function() {835+ this.on('option:version', function() {836 process.stdout.write(str + '\n');837 process.exit(0);838 });839 return this;291 unmodified lines