@@ -7,7 +7,7 @@ const process = require('process');
7
7
const { Argument, humanReadableArgName } = require ( './argument.js' ) ;
8
8
const { CommanderError } = require ( './error.js' ) ;
9
9
const { Help } = require ( './help.js' ) ;
10
- const { Option, splitOptionFlags , DualOptions } = require ( './option.js' ) ;
10
+ const { Option, DualOptions } = require ( './option.js' ) ;
11
11
const { suggestSimilar } = require ( './suggestSimilar' ) ;
12
12
13
13
class Command extends EventEmitter {
@@ -66,11 +66,8 @@ class Command extends EventEmitter {
66
66
} ;
67
67
68
68
this . _hidden = false ;
69
- this . _hasHelpOption = true ;
70
- this . _helpFlags = '-h, --help' ;
71
- this . _helpDescription = 'display help for command' ;
72
- this . _helpShortFlag = '-h' ;
73
- this . _helpLongFlag = '--help' ;
69
+ /** @type {(Option | null | undefined) } */
70
+ this . _helpOption = undefined ; // Lazy created on demand. May be null if help option is disabled.
74
71
this . _addImplicitHelpCommand = undefined ; // undecided whether true or false yet, not inherited
75
72
/** @type {Command } */
76
73
this . _helpCommand = undefined ; // lazy initialised, inherited
@@ -87,11 +84,7 @@ class Command extends EventEmitter {
87
84
*/
88
85
copyInheritedSettings ( sourceCommand ) {
89
86
this . _outputConfiguration = sourceCommand . _outputConfiguration ;
90
- this . _hasHelpOption = sourceCommand . _hasHelpOption ;
91
- this . _helpFlags = sourceCommand . _helpFlags ;
92
- this . _helpDescription = sourceCommand . _helpDescription ;
93
- this . _helpShortFlag = sourceCommand . _helpShortFlag ;
94
- this . _helpLongFlag = sourceCommand . _helpLongFlag ;
87
+ this . _helpOption = sourceCommand . _helpOption ;
95
88
this . _helpCommand = sourceCommand . _helpCommand ;
96
89
this . _helpConfiguration = sourceCommand . _helpConfiguration ;
97
90
this . _exitCallback = sourceCommand . _exitCallback ;
@@ -1189,7 +1182,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
1189
1182
1190
1183
// Fallback to parsing the help flag to invoke the help.
1191
1184
return this . _dispatchSubcommand ( subcommandName , [ ] , [
1192
- this . _helpLongFlag || this . _helpShortFlag
1185
+ this . _getHelpOption ( ) ?. long ?? this . _getHelpOption ( ) ?. short ?? '--help'
1193
1186
] ) ;
1194
1187
}
1195
1188
@@ -2001,7 +1994,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
2001
1994
return humanReadableArgName ( arg ) ;
2002
1995
} ) ;
2003
1996
return [ ] . concat (
2004
- ( this . options . length || this . _hasHelpOption ? '[options]' : [ ] ) ,
1997
+ ( this . options . length || ( this . _helpOption !== null ) ? '[options]' : [ ] ) ,
2005
1998
( this . commands . length ? '[command]' : [ ] ) ,
2006
1999
( this . registeredArguments . length ? args : [ ] )
2007
2000
) . join ( ' ' ) ;
@@ -2122,35 +2115,69 @@ Expecting one of '${allowedValues.join("', '")}'`);
2122
2115
}
2123
2116
context . write ( helpInformation ) ;
2124
2117
2125
- if ( this . _helpLongFlag ) {
2126
- this . emit ( this . _helpLongFlag ) ; // deprecated
2118
+ if ( this . _getHelpOption ( ) ?. long ) {
2119
+ this . emit ( this . _getHelpOption ( ) . long ) ; // deprecated
2127
2120
}
2128
2121
this . emit ( 'afterHelp' , context ) ;
2129
2122
this . _getCommandAndAncestors ( ) . forEach ( command => command . emit ( 'afterAllHelp' , context ) ) ;
2130
2123
}
2131
2124
2132
2125
/**
2133
- * You can pass in flags and a description to override the help
2134
- * flags and help description for your command. Pass in false to
2135
- * disable the built-in help option.
2126
+ * You can pass in flags and a description to customise the built-in help option.
2127
+ * Pass in false to disable the built-in help option.
2136
2128
*
2137
- * @param {(string | boolean) } [flags]
2129
+ * @example
2130
+ * program.helpOption('-?, --help' 'show help'); // customise
2131
+ * program.helpOption(false); // disable
2132
+ *
2133
+ * @param {(string | boolean) } flags
2138
2134
* @param {string } [description]
2139
2135
* @return {Command } `this` command for chaining
2140
2136
*/
2141
2137
2142
2138
helpOption ( flags , description ) {
2139
+ // Support disabling built-in help option.
2143
2140
if ( typeof flags === 'boolean' ) {
2144
- this . _hasHelpOption = flags ;
2141
+ if ( flags ) {
2142
+ this . _helpOption = this . _helpOption ?? undefined ; // preserve existing option
2143
+ } else {
2144
+ this . _helpOption = null ; // disable
2145
+ }
2145
2146
return this ;
2146
2147
}
2147
- this . _helpFlags = flags || this . _helpFlags ;
2148
- this . _helpDescription = description || this . _helpDescription ;
2149
2148
2150
- const helpFlags = splitOptionFlags ( this . _helpFlags ) ;
2151
- this . _helpShortFlag = helpFlags . shortFlag ;
2152
- this . _helpLongFlag = helpFlags . longFlag ;
2149
+ // Customise flags and description.
2150
+ flags = flags ?? '-h, --help' ;
2151
+ description = description ?? 'display help for command' ;
2152
+ this . _helpOption = this . createOption ( flags , description ) ;
2153
+
2154
+ return this ;
2155
+ }
2153
2156
2157
+ /**
2158
+ * Lazy create help option.
2159
+ * Returns null if has been disabled with .helpOption(false).
2160
+ *
2161
+ * @returns {(Option | null) } the help option
2162
+ * @package internal use only
2163
+ */
2164
+ _getHelpOption ( ) {
2165
+ // Lazy create help option on demand.
2166
+ if ( this . _helpOption === undefined ) {
2167
+ this . helpOption ( undefined , undefined ) ;
2168
+ }
2169
+ return this . _helpOption ;
2170
+ }
2171
+
2172
+ /**
2173
+ * Supply your own option to use for the built-in help option.
2174
+ * This is an alternative to using helpOption() to customise the flags and description etc.
2175
+ *
2176
+ * @param {Option } option
2177
+ * @return {Command } `this` command for chaining
2178
+ */
2179
+ addHelpOption ( option ) {
2180
+ this . _helpOption = option ;
2154
2181
return this ;
2155
2182
}
2156
2183
@@ -2212,8 +2239,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
2212
2239
*/
2213
2240
2214
2241
_outputHelpIfRequested ( args ) {
2215
- const helpOption = this . _hasHelpOption && args . find ( arg => arg === this . _helpLongFlag || arg === this . _helpShortFlag ) ;
2216
- if ( helpOption ) {
2242
+ const helpOption = this . _getHelpOption ( ) ;
2243
+ const helpRequested = helpOption && args . find ( arg => helpOption . is ( arg ) ) ;
2244
+ if ( helpRequested ) {
2217
2245
this . outputHelp ( ) ;
2218
2246
// (Do not have all displayed text available so only passing placeholder.)
2219
2247
this . _exit ( 0 , 'commander.helpDisplayed' , '(outputHelp)' ) ;
0 commit comments