@@ -129,6 +129,7 @@ class Command extends EventEmitter {
129
129
this . _aliases = [ ] ;
130
130
131
131
this . _hidden = false ;
132
+ this . _hasHelpOption = true ;
132
133
this . _helpFlags = '-h, --help' ;
133
134
this . _helpDescription = 'display help for command' ;
134
135
this . _helpShortFlag = '-h' ;
@@ -184,6 +185,7 @@ class Command extends EventEmitter {
184
185
if ( opts . isDefault ) this . _defaultCommandName = cmd . _name ;
185
186
186
187
cmd . _hidden = ! ! ( opts . noHelp || opts . hidden ) ;
188
+ cmd . _hasHelpOption = this . _hasHelpOption ;
187
189
cmd . _helpFlags = this . _helpFlags ;
188
190
cmd . _helpDescription = this . _helpDescription ;
189
191
cmd . _helpShortFlag = this . _helpShortFlag ;
@@ -1236,7 +1238,8 @@ Read more on https://git.io/JJc0W`);
1236
1238
partCommands . unshift ( parentCmd . name ( ) ) ;
1237
1239
}
1238
1240
const fullCommand = partCommands . join ( ' ' ) ;
1239
- const message = `error: unknown command '${ this . args [ 0 ] } '. See '${ fullCommand } ${ this . _helpLongFlag } '.` ;
1241
+ const message = `error: unknown command '${ this . args [ 0 ] } '.` +
1242
+ ( this . _hasHelpOption ? ` See '${ fullCommand } ${ this . _helpLongFlag } '.` : '' ) ;
1240
1243
console . error ( message ) ;
1241
1244
this . _exit ( 1 , 'commander.unknownCommand' , message ) ;
1242
1245
} ;
@@ -1345,9 +1348,11 @@ Read more on https://git.io/JJc0W`);
1345
1348
const args = this . _args . map ( ( arg ) => {
1346
1349
return humanReadableArgName ( arg ) ;
1347
1350
} ) ;
1348
- return '[options]' +
1349
- ( this . commands . length ? ' [command]' : '' ) +
1350
- ( this . _args . length ? ' ' + args . join ( ' ' ) : '' ) ;
1351
+ return [ ] . concat (
1352
+ ( this . options . length || this . _hasHelpOption ? '[options]' : [ ] ) ,
1353
+ ( this . commands . length ? '[command]' : [ ] ) ,
1354
+ ( this . _args . length ? args : [ ] )
1355
+ ) . join ( ' ' ) ;
1351
1356
}
1352
1357
1353
1358
this . _usage = str ;
@@ -1490,8 +1495,8 @@ Read more on https://git.io/JJc0W`);
1490
1495
} ) ;
1491
1496
1492
1497
// Implicit help
1493
- const showShortHelpFlag = this . _helpShortFlag && ! this . _findOption ( this . _helpShortFlag ) ;
1494
- const showLongHelpFlag = ! this . _findOption ( this . _helpLongFlag ) ;
1498
+ const showShortHelpFlag = this . _hasHelpOption && this . _helpShortFlag && ! this . _findOption ( this . _helpShortFlag ) ;
1499
+ const showLongHelpFlag = this . _hasHelpOption && ! this . _findOption ( this . _helpLongFlag ) ;
1495
1500
if ( showShortHelpFlag || showLongHelpFlag ) {
1496
1501
let helpFlags = this . _helpFlags ;
1497
1502
if ( ! showShortHelpFlag ) {
@@ -1577,11 +1582,14 @@ Read more on https://git.io/JJc0W`);
1577
1582
const commandHelp = this . commandHelp ( ) ;
1578
1583
if ( commandHelp ) cmds = [ commandHelp ] ;
1579
1584
1580
- const options = [
1581
- 'Options:' ,
1582
- '' + this . optionHelp ( ) . replace ( / ^ / gm, ' ' ) ,
1583
- ''
1584
- ] ;
1585
+ let options = [ ] ;
1586
+ if ( this . _hasHelpOption || this . options . length > 0 ) {
1587
+ options = [
1588
+ 'Options:' ,
1589
+ '' + this . optionHelp ( ) . replace ( / ^ / gm, ' ' ) ,
1590
+ ''
1591
+ ] ;
1592
+ }
1585
1593
1586
1594
return usage
1587
1595
. concat ( desc )
@@ -1615,15 +1623,20 @@ Read more on https://git.io/JJc0W`);
1615
1623
1616
1624
/**
1617
1625
* You can pass in flags and a description to override the help
1618
- * flags and help description for your command.
1626
+ * flags and help description for your command. Pass in false to
1627
+ * disable the built-in help option.
1619
1628
*
1620
- * @param {string } [flags]
1629
+ * @param {string | boolean } [flags]
1621
1630
* @param {string } [description]
1622
1631
* @return {Command } `this` command for chaining
1623
1632
* @api public
1624
1633
*/
1625
1634
1626
1635
helpOption ( flags , description ) {
1636
+ if ( typeof flags === 'boolean' ) {
1637
+ this . _hasHelpOption = flags ;
1638
+ return this ;
1639
+ }
1627
1640
this . _helpFlags = flags || this . _helpFlags ;
1628
1641
this . _helpDescription = description || this . _helpDescription ;
1629
1642
@@ -1756,7 +1769,7 @@ function optionalWrap(str, width, indent) {
1756
1769
*/
1757
1770
1758
1771
function outputHelpIfRequested ( cmd , args ) {
1759
- const helpOption = args . find ( arg => arg === cmd . _helpLongFlag || arg === cmd . _helpShortFlag ) ;
1772
+ const helpOption = cmd . _hasHelpOption && args . find ( arg => arg === cmd . _helpLongFlag || arg === cmd . _helpShortFlag ) ;
1760
1773
if ( helpOption ) {
1761
1774
cmd . outputHelp ( ) ;
1762
1775
// (Do not have all displayed text available so only passing placeholder.)
0 commit comments