Skip to content

Commit deb3bdf

Browse files
committed
Prevent adding version option with flags already in use
1 parent 5c87223 commit deb3bdf

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

lib/command.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,18 +501,32 @@ Expecting one of '${allowedValues.join("', '")}'`);
501501
}
502502

503503
/**
504-
* Add an option.
504+
* Check for option flag conflicts.
505+
* Register option if no conflicts found.
506+
* Throw otherwise.
505507
*
506508
* @param {Option} option
507-
* @return {Command} `this` command for chaining
509+
* @api private
508510
*/
509-
addOption(option) {
511+
512+
_registerOption(option) {
510513
const matchingOption = (option.short && this._findOption(option.short)) ||
511514
(option.long && this._findOption(option.long));
512515
if (matchingOption) {
513516
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to '${this._name}'`} since an option using same flag has already been added
514517
- conflicts with option '${matchingOption.flags}'`);
515518
}
519+
this.options.push(option);
520+
}
521+
522+
/**
523+
* Add an option.
524+
*
525+
* @param {Option} option
526+
* @return {Command} `this` command for chaining
527+
*/
528+
addOption(option) {
529+
this._registerOption(option);
516530

517531
const oname = option.name();
518532
const name = option.attributeName();
@@ -528,9 +542,6 @@ Expecting one of '${allowedValues.join("', '")}'`);
528542
this.setOptionValueWithSource(name, option.defaultValue, 'default');
529543
}
530544

531-
// register the option
532-
this.options.push(option);
533-
534545
// handler for cli and env supplied values
535546
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
536547
// val is null for optional option used without an optional-argument.
@@ -1827,7 +1838,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
18271838
description = description || 'output the version number';
18281839
const versionOption = this.createOption(flags, description);
18291840
this._versionOptionName = versionOption.attributeName();
1830-
this.options.push(versionOption);
1841+
this._registerOption(versionOption);
18311842
this.on('option:' + versionOption.name(), () => {
18321843
this._outputConfiguration.writeOut(`${str}\n`);
18331844
this._exit(0, 'commander.version', str);

0 commit comments

Comments
 (0)