-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcustom-command-function.js
36 lines (28 loc) · 985 Bytes
/
custom-command-function.js
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
#!/usr/bin/env node
// const commander = require('commander'); // (normal include)
const commander = require('../'); // include commander in git clone of commander repo
// Override createCommand directly to customise subcommands,
// in this example by adding --debug option.
const program = commander.createCommand();
// Customise subcommand creation
program.createCommand = (name) => {
const cmd = commander.createCommand(name);
cmd.option('-d,--debug', 'output options');
return cmd;
};
program
.command('serve')
.option('--port <port-number>', 'specify port number', 80)
.action((cmd) => {
if (cmd.debug) {
console.log('Options:');
console.log(cmd.opts());
console.log();
}
console.log(`Start serve on port ${cmd.port}`);
});
program.parse();
// Try the following:
// node custom-command-function.js help serve
// node custom-command-function.js serve --debug
// node custom-command-function.js serve --debug --port 8080