-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcustom-command-class.js
executable file
·50 lines (40 loc) · 1.38 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.
//
// Configuring the command for compatibility with Commander v6 defaults behaviours.
class Command6 extends commander.Command {
constructor(name) {
super(name);
// Revert to Commander v6 behaviours.
this.storeOptionsAsProperties();
this.allowExcessArguments();
}
createCommand(name) {
return new Command6(name);
}
};
function inspectCommand(command, optionName) {
// The option value is stored as property on command because we called .storeOptionsAsProperties()
console.log(`Inspecting '${command.name()}'`);
console.log(`option '${optionName}': ${command[optionName]}`);
console.log(`args: ${command.args}`);
};
const program = new Command6('program')
.option('-p, --port <number>')
.action(() => {
inspectCommand(program, 'port');
});
program
.command('sub')
.option('-d, --debug')
.action((options, command) => {
inspectCommand(command, 'debug');
});
program.parse();
// We can pass excess arguments without an error as we called .allowExcessArguments()
//
// Try the following:
// node custom-command-class.js --port 80 extra arguments
// node custom-command-class.js sub --debug extra arguments