Skip to content
Myr

help-groups.js

This file

Path

examples/help-groups.js

75 lines · 2.6 KiB · JavaScript

Last changed by

Add support for groups of options and commands in the help (#2328)John Geebefore adoptionc324ea3

History of this file

Take it

Add support for groups of options and commands in the help (#2328)John Geebefore adoptionc324ea3

lines 1–75why, one step back
1const { Command, Option } = require('commander');
23// Show the two approaches for adding help groups, and how to customise the built-in help and version.45const program = new Command();6const devOptionsHeading = 'Development Options:';7const managementCommandsHeading = 'Management Commands:';89// The high-level approach is use .optionsGroup() and .commandsGroup() before adding the options/commands.10const docker1 = program11 .command('docker1')12 .description('help groups created using .optionsGroup() and .commandsGroup()')13 .addOption(new Option('-h, --hostname <name>', 'container host name'))14 .addOption(new Option('-p, --port <number>', 'container port number'))15 .optionsGroup(devOptionsHeading)16 .option('-d, --debug', 'add extra trace information')17 .option('-w, --watch', 'run and relaunch service on file changes');1819docker120 .command('run')21 .description('create and run a new container from an image');22docker1.command('exec').description('execute a command in a running container');2324docker1.commandsGroup(managementCommandsHeading);25docker1.command('images').description('manage images');26docker1.command('volumes').description('manage volumes');2728// The low-level approach is using .helpGroup() on the Option or Command.29const docker2 = program30 .command('docker2')31 .description('help groups created using .helpGroup()')32 .addOption(new Option('-h, --hostname <name>', 'container host name'))33 .addOption(new Option('-p, --port <number>', 'container port number'))34 .addOption(35 new Option('-d, --debug', 'add extra trace information').helpGroup(36 devOptionsHeading,37 ),38 )39 .addOption(40 new Option(41 '-w, --watch',42 'run and relaunch service on file changes',43 ).helpGroup(devOptionsHeading),44 );4546docker247 .command('run')48 .description('create and run a new container from an image');49docker2.command('exec').description('execute a command in a running container');5051docker252 .command('images')53 .description('manage images')54 .helpGroup(managementCommandsHeading);55docker256 .command('volumes')57 .description('manage volumes')58 .helpGroup(managementCommandsHeading);5960// Customise group for built-ins by configuring them with default group set.61program62 .command('built-in')63 .description('help groups for help and version')64 .optionsGroup('Built-in Options:')65 .version('v2.3.4')66 .helpOption('-h, --help') // or .helpOption(true) to use default flags67 .commandsGroup('Built-in Commands:')68 .helpCommand('help [command]'); // or .helpCommand(true) to use default name6970program.parse();7172// Try the following:73// node help-groups.js help docker174// node help-groups.js help docker275// node help-groups.js help built-in