-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add support for groups of options and commands in the help #2328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shadowspawn
merged 11 commits into
tj:release/14.x
from
shadowspawn:feature/help-group-4
May 8, 2025
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
16752e0
Add support for command and option grouping in help output
shadowspawn 1b5e3f0
Add .optionsGroup() and .commandsGroup() implementation
shadowspawn 7c83ea8
Add example of using help groups
shadowspawn 09ff045
Rename routine
shadowspawn 7a7d80d
Add missing param in JSDoc
shadowspawn 2ce7006
Add test for undocumented edge case of calling .helpOption(true)
shadowspawn 6053835
Experimental support for adding help group to built-in help option+co…
shadowspawn e5cd8fd
Add help groups to README and change group title to heading
shadowspawn 30b2316
Tweak example
shadowspawn 3b26bff
Add tests
shadowspawn 7f31d74
Change Title to Heading in example
shadowspawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| const { Command, Option } = require('commander'); | ||
|
|
||
| // Show the two approaches for adding help groups, and how to customise the built-in help and version. | ||
|
|
||
| const program = new Command(); | ||
| const devOptionsTitle = 'Development Options:'; | ||
| const managementCommandsTitle = 'Management Commands:'; | ||
|
|
||
| // The high-level approach is use .optionsGroup() and .commandsGroup() before adding the options/commands. | ||
| const docker1 = program | ||
| .command('docker1') | ||
| .description('help groups created using .optionsGroup() and .commandsGroup()') | ||
| .addOption(new Option('-h, --hostname <name>', 'container host name')) | ||
| .addOption(new Option('-p, --port <number>', 'container port number')) | ||
| .optionsGroup(devOptionsTitle) | ||
| .option('-d, --debug', 'add extra trace information') | ||
| .option('-w, --watch', 'run and relaunch service on file changes'); | ||
|
|
||
| docker1 | ||
| .command('run') | ||
| .description('create and run a new container from an image'); | ||
| docker1.command('exec').description('execute a command in a running container'); | ||
|
|
||
| docker1.commandsGroup(managementCommandsTitle); | ||
| docker1.command('images').description('manage images'); | ||
| docker1.command('volumes').description('manage volumes'); | ||
|
|
||
| // The low-level approach is using .helpGroup() on the Option or Command. | ||
| const docker2 = program | ||
| .command('docker2') | ||
| .description('help groups created using .helpGroup()') | ||
| .addOption(new Option('-h, --hostname <name>', 'container host name')) | ||
| .addOption(new Option('-p, --port <number>', 'container port number')) | ||
| .addOption( | ||
| new Option('-d, --debug', 'add extra trace information').helpGroup( | ||
| devOptionsTitle, | ||
| ), | ||
| ) | ||
| .addOption( | ||
| new Option( | ||
| '-w, --watch', | ||
| 'run and relaunch service on file changes', | ||
| ).helpGroup(devOptionsTitle), | ||
| ); | ||
|
|
||
| docker2 | ||
| .command('run') | ||
| .description('create and run a new container from an image'); | ||
| docker2.command('exec').description('execute a command in a running container'); | ||
|
|
||
| docker2 | ||
| .command('images') | ||
| .description('manage images') | ||
| .helpGroup(managementCommandsTitle); | ||
| docker2 | ||
| .command('volumes') | ||
| .description('manage volumes') | ||
| .helpGroup(managementCommandsTitle); | ||
|
|
||
| // Customise group for built-ins by explicitly adding them with default group set. | ||
| program | ||
| .command('built-in') | ||
| .description('help groups for help and version') | ||
| .optionsGroup('Built-in Options:') | ||
| .version('v2.3.4') | ||
| .helpOption('-h, --help') | ||
| .commandsGroup('Built-in Commands:') | ||
| .helpCommand('help'); | ||
|
|
||
| program.parse(); | ||
|
|
||
| // Try the following: | ||
| // node help-groups.js help docker1 | ||
| // node help-groups.js help docker2 | ||
| // node help-groups.js help built-in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.