@@ -80,6 +80,12 @@ class Command extends EventEmitter {
80
80
/** @type {Command } */
81
81
this . _helpCommand = undefined ; // lazy initialised, inherited
82
82
this . _helpConfiguration = { } ;
83
+ /** @type {string | undefined } */
84
+ this . _helpGroupHeading = undefined ; // soft initialised when added to parent
85
+ /** @type {string | undefined } */
86
+ this . _defaultCommandGroup = undefined ;
87
+ /** @type {string | undefined } */
88
+ this . _defaultOptionGroup = undefined ;
83
89
}
84
90
85
91
/**
@@ -404,11 +410,15 @@ class Command extends EventEmitter {
404
410
helpCommand ( enableOrNameAndArgs , description ) {
405
411
if ( typeof enableOrNameAndArgs === 'boolean' ) {
406
412
this . _addImplicitHelpCommand = enableOrNameAndArgs ;
413
+ if ( enableOrNameAndArgs && this . _defaultCommandGroup ) {
414
+ // make the command to store the group
415
+ this . _initCommandGroup ( this . _getHelpCommand ( ) ) ;
416
+ }
407
417
return this ;
408
418
}
409
419
410
- enableOrNameAndArgs = enableOrNameAndArgs ?? 'help [command]' ;
411
- const [ , helpName , helpArgs ] = enableOrNameAndArgs . match ( / ( [ ^ ] + ) * ( .* ) / ) ;
420
+ const nameAndArgs = enableOrNameAndArgs ?? 'help [command]' ;
421
+ const [ , helpName , helpArgs ] = nameAndArgs . match ( / ( [ ^ ] + ) * ( .* ) / ) ;
412
422
const helpDescription = description ?? 'display help for command' ;
413
423
414
424
const helpCommand = this . createCommand ( helpName ) ;
@@ -418,6 +428,8 @@ class Command extends EventEmitter {
418
428
419
429
this . _addImplicitHelpCommand = true ;
420
430
this . _helpCommand = helpCommand ;
431
+ // init group unless lazy create
432
+ if ( enableOrNameAndArgs || description ) this . _initCommandGroup ( helpCommand ) ;
421
433
422
434
return this ;
423
435
}
@@ -439,6 +451,7 @@ class Command extends EventEmitter {
439
451
440
452
this . _addImplicitHelpCommand = true ;
441
453
this . _helpCommand = helpCommand ;
454
+ this . _initCommandGroup ( helpCommand ) ;
442
455
return this ;
443
456
}
444
457
@@ -617,6 +630,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
617
630
- already used by option '${ matchingOption . flags } '` ) ;
618
631
}
619
632
633
+ this . _initOptionGroup ( option ) ;
620
634
this . options . push ( option ) ;
621
635
}
622
636
@@ -644,6 +658,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
644
658
) ;
645
659
}
646
660
661
+ this . _initCommandGroup ( command ) ;
647
662
this . commands . push ( command ) ;
648
663
}
649
664
@@ -2320,6 +2335,75 @@ Expecting one of '${allowedValues.join("', '")}'`);
2320
2335
return this ;
2321
2336
}
2322
2337
2338
+ /**
2339
+ * Set/get the help group heading for this subcommand in parent command's help.
2340
+ *
2341
+ * @param {string } [heading]
2342
+ * @return {Command | string }
2343
+ */
2344
+
2345
+ helpGroup ( heading ) {
2346
+ if ( heading === undefined ) return this . _helpGroupHeading ?? '' ;
2347
+ this . _helpGroupHeading = heading ;
2348
+ return this ;
2349
+ }
2350
+
2351
+ /**
2352
+ * Set/get the default help group heading for subcommands added to this command.
2353
+ * (This does not override a group set directly on the subcommand using .helpGroup().)
2354
+ *
2355
+ * @example
2356
+ * program.commandsGroup('Development Commands:);
2357
+ * program.command('watch')...
2358
+ * program.command('lint')...
2359
+ * ...
2360
+ *
2361
+ * @param {string } [heading]
2362
+ * @returns {Command | string }
2363
+ */
2364
+ commandsGroup ( heading ) {
2365
+ if ( heading === undefined ) return this . _defaultCommandGroup ?? '' ;
2366
+ this . _defaultCommandGroup = heading ;
2367
+ return this ;
2368
+ }
2369
+
2370
+ /**
2371
+ * Set/get the default help group heading for options added to this command.
2372
+ * (This does not override a group set directly on the option using .helpGroup().)
2373
+ *
2374
+ * @example
2375
+ * program
2376
+ * .optionsGroup('Development Options:')
2377
+ * .option('-d, --debug', 'output extra debugging')
2378
+ * .option('-p, --profile', 'output profiling information')
2379
+ *
2380
+ * @param {string } [heading]
2381
+ * @returns {Command | string }
2382
+ */
2383
+ optionsGroup ( heading ) {
2384
+ if ( heading === undefined ) return this . _defaultOptionGroup ?? '' ;
2385
+ this . _defaultOptionGroup = heading ;
2386
+ return this ;
2387
+ }
2388
+
2389
+ /**
2390
+ * @param {Option } option
2391
+ * @private
2392
+ */
2393
+ _initOptionGroup ( option ) {
2394
+ if ( this . _defaultOptionGroup && ! option . helpGroupHeading )
2395
+ option . helpGroup ( this . _defaultOptionGroup ) ;
2396
+ }
2397
+
2398
+ /**
2399
+ * @param {Command } cmd
2400
+ * @private
2401
+ */
2402
+ _initCommandGroup ( cmd ) {
2403
+ if ( this . _defaultCommandGroup && ! cmd . helpGroup ( ) )
2404
+ cmd . helpGroup ( this . _defaultCommandGroup ) ;
2405
+ }
2406
+
2323
2407
/**
2324
2408
* Set the name of the command from script filename, such as process.argv[1],
2325
2409
* or require.main.filename, or __filename.
@@ -2474,22 +2558,27 @@ Expecting one of '${allowedValues.join("', '")}'`);
2474
2558
*/
2475
2559
2476
2560
helpOption ( flags , description ) {
2477
- // Support disabling built-in help option.
2561
+ // Support enabling/ disabling built-in help option.
2478
2562
if ( typeof flags === 'boolean' ) {
2479
- // true is not an expected value. Do something sensible but no unit-test.
2480
- // istanbul ignore if
2481
2563
if ( flags ) {
2482
- this . _helpOption = this . _helpOption ?? undefined ; // preserve existing option
2564
+ if ( this . _helpOption === null ) this . _helpOption = undefined ; // reenable
2565
+ if ( this . _defaultOptionGroup ) {
2566
+ // make the option to store the group
2567
+ this . _initOptionGroup ( this . _getHelpOption ( ) ) ;
2568
+ }
2483
2569
} else {
2484
2570
this . _helpOption = null ; // disable
2485
2571
}
2486
2572
return this ;
2487
2573
}
2488
2574
2489
2575
// Customise flags and description.
2490
- flags = flags ?? '-h, --help' ;
2491
- description = description ?? 'display help for command' ;
2492
- this . _helpOption = this . createOption ( flags , description ) ;
2576
+ this . _helpOption = this . createOption (
2577
+ flags ?? '-h, --help' ,
2578
+ description ?? 'display help for command' ,
2579
+ ) ;
2580
+ // init group unless lazy create
2581
+ if ( flags || description ) this . _initOptionGroup ( this . _helpOption ) ;
2493
2582
2494
2583
return this ;
2495
2584
}
@@ -2518,6 +2607,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
2518
2607
*/
2519
2608
addHelpOption ( option ) {
2520
2609
this . _helpOption = option ;
2610
+ this . _initOptionGroup ( option ) ;
2521
2611
return this ;
2522
2612
}
2523
2613
0 commit comments