forked from tj/commander.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp-groups.js
More file actions
75 lines (64 loc) · 2.64 KB
/
help-groups.js
File metadata and controls
75 lines (64 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 devOptionsHeading = 'Development Options:';
const managementCommandsHeading = '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(devOptionsHeading)
.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(managementCommandsHeading);
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(
devOptionsHeading,
),
)
.addOption(
new Option(
'-w, --watch',
'run and relaunch service on file changes',
).helpGroup(devOptionsHeading),
);
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(managementCommandsHeading);
docker2
.command('volumes')
.description('manage volumes')
.helpGroup(managementCommandsHeading);
// Customise group for built-ins by configuring 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') // or .helpOption(true) to use default flags
.commandsGroup('Built-in Commands:')
.helpCommand('help [command]'); // or .helpCommand(true) to use default name
program.parse();
// Try the following:
// node help-groups.js help docker1
// node help-groups.js help docker2
// node help-groups.js help built-in