forked from tj/commander.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom-command-class.js
executable file
·50 lines (41 loc) · 1.47 KB
/
custom-command-class.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env node
// const commander = require('commander'); // (normal include)
const commander = require('../'); // include commander in git clone of commander repo
// Use a class override to customise the command and its subcommands.
class CommandWithTrace extends commander.Command {
createCommand(name) {
const cmd = new CommandWithTrace(name);
// Add an option to subcommands created using `.command()`
cmd.option('-t, --trace', 'display extra information when run command');
return cmd;
}
};
function inpectCommand(command) {
// The option value is stored as property on command because we called .storeOptionsAsProperties()
console.log(`Called '${command.name()}'`);
console.log(`args: ${command.args}`);
console.log('opts: %o', command.opts());
};
const program = new CommandWithTrace('program')
.option('-v, ---verbose')
.action((options, command) => {
inpectCommand(command);
});
program
.command('serve [params...]')
.option('-p, --port <number>', 'port number')
.action((params, options, command) => {
inpectCommand(command);
});
program
.command('build <target>')
.action((buildTarget, options, command) => {
inpectCommand(command);
});
program.parse();
// Try the following:
// node custom-command-class.js --help
// node custom-command-class.js serve --help
// node custom-command-class.js serve -t -p 80 a b c
// node custom-command-class.js build --help
// node custom-command-class.js build --trace foo