@@ -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