@@ -12,6 +12,8 @@ const { suggestSimilar } = require('./suggestSimilar');
12
12
13
13
// @ts -check
14
14
15
+ const PRODUCTION = process . env . NODE_ENV === 'production' ;
16
+
15
17
class Command extends EventEmitter {
16
18
/**
17
19
* Initialize a new `Command`.
@@ -521,6 +523,11 @@ Expecting one of '${allowedValues.join("', '")}'`);
521
523
522
524
// register the option
523
525
this . options . push ( option ) ;
526
+ this . _checkForObscuredHelpOption ( ( matchingOptionFlags ) => (
527
+ `Help option '${ this . _helpOption . flags } ' is obscured after adding option '${ option . flags } '${ matchingOptionFlags . length > 1
528
+ ? `
529
+ - conflicts with options '${ matchingOptionFlags . join ( "' and '" ) } '`
530
+ : '' } `) ) ;
524
531
525
532
// handler for cli and env supplied values
526
533
const handleOptionValue = ( val , invalidValueMessage , valueSource ) => {
@@ -1099,7 +1106,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
1099
1106
}
1100
1107
1101
1108
// Fallback to parsing the help flag to invoke the help.
1102
- return this . _dispatchSubcommand ( subcommandName , [ ] , [ this . _helpLongFlag ] ) ;
1109
+ return this . _dispatchSubcommand ( subcommandName , [ ] , [ this . _helpOption . long ] ) ;
1103
1110
}
1104
1111
1105
1112
/**
@@ -2032,7 +2039,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
2032
2039
}
2033
2040
context . write ( helpInformation ) ;
2034
2041
2035
- this . emit ( this . _helpLongFlag ) ; // deprecated
2042
+ this . emit ( this . _helpOption . long ) ; // deprecated
2036
2043
this . emit ( 'afterHelp' , context ) ;
2037
2044
getCommandAndParents ( this ) . forEach ( command => command . emit ( 'afterAllHelp' , context ) ) ;
2038
2045
}
@@ -2056,13 +2063,37 @@ Expecting one of '${allowedValues.join("', '")}'`);
2056
2063
this . _helpFlags = flags = flags || this . _helpFlags ;
2057
2064
this . _helpDescription = description = description || this . _helpDescription ;
2058
2065
2059
- const helpOption = this . createOption ( flags , description ) ;
2060
- this . _helpShortFlag = helpOption . short ;
2061
- this . _helpLongFlag = helpOption . long ;
2066
+ this . _helpOption = this . createOption ( flags , description ) ;
2067
+ this . _checkForObscuredHelpOption ( ( matchingOptionFlags ) => (
2068
+ `Newly added help option '${ this . _helpOption . flags } ' is obscured
2069
+ - conflicts with ${ matchingOptionFlags . length > 1 ? 'options' : 'option' } '${ matchingOptionFlags . join ( "' and '" ) } '` ) ) ;
2062
2070
2063
2071
return this ;
2064
2072
}
2065
2073
2074
+ /**
2075
+ * @api private
2076
+ */
2077
+
2078
+ _checkForObscuredHelpOption ( makeMessage ) {
2079
+ if ( ! PRODUCTION && this . _hasHelpOption ) {
2080
+ const shortMatchingOption = this . _helpOption . short &&
2081
+ this . _findOption ( this . _helpOption . short ) ;
2082
+ if ( shortMatchingOption || ! this . _helpOption . short ) {
2083
+ const longMatchingOption = this . _helpOption . long &&
2084
+ this . _findOption ( this . _helpOption . long ) ;
2085
+ if ( longMatchingOption || ! this . _helpOption . long ) {
2086
+ const matchingOptionFlags = (
2087
+ shortMatchingOption === longMatchingOption
2088
+ ? [ shortMatchingOption ]
2089
+ : [ shortMatchingOption , longMatchingOption ]
2090
+ ) . filter ( option => option ) . map ( option => option . flags ) ;
2091
+ console . warn ( makeMessage ( matchingOptionFlags ) ) ;
2092
+ }
2093
+ }
2094
+ }
2095
+ }
2096
+
2066
2097
/**
2067
2098
* Output help information and exit.
2068
2099
*
@@ -2123,7 +2154,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
2123
2154
*/
2124
2155
2125
2156
function outputHelpIfRequested ( cmd , args ) {
2126
- const helpOption = cmd . _hasHelpOption && args . find ( arg => arg === cmd . _helpLongFlag || arg === cmd . _helpShortFlag ) ;
2157
+ const helpOption = cmd . _hasHelpOption && args . find ( arg => arg === cmd . _helpOption . long || arg === cmd . _helpOption . short ) ;
2127
2158
if ( helpOption ) {
2128
2159
cmd . outputHelp ( ) ;
2129
2160
// (Do not have all displayed text available so only passing placeholder.)
0 commit comments