forked from tj/commander.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-subcommand.js
executable file
·67 lines (56 loc) · 1.76 KB
/
use-subcommand.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
// This is an example of useSubcommand
// and collectAllOptions
//
// try
// $ use-subcommand journal list myjounal
// $ use-subcommand journal delete myjounal
// or with options
// $ use-subcommand journal -q delete -f myjounal
// const { Command } = require('commander'); << would be in a real program
const { Command } = require('..');
function importSubCommand() {
const journalCmd = new Command()
.name('journal')
.description('Journal utils');
journalCmd
.command('list <path>')
.description('List journal')
.action((path, cmdInstance) => {
console.log('List journal');
console.log('Path is', path);
console.log('Quiet =', Boolean(cmdInstance.parent.parent.quiet));
// list is a child of journal, which is a child of main cmd
console.log('collectAllOptions:', cmdInstance.collectAllOptions());
});
journalCmd
.command('delete <path>')
.description('Delete journal')
.option('-f, --force')
.action((path, cmdInstance) => {
console.log('List journal');
console.log('Path is', path);
console.log('Quiet =', Boolean(cmdInstance.parent.parent.quiet));
console.log('Force =', Boolean(cmdInstance.force));
console.log('collectAllOptions:', cmdInstance.collectAllOptions());
});
return journalCmd;
}
// this is supposedly a module, so in real case this would be `require`
const journalSubCommand = importSubCommand();
const program = new Command();
program
.option('-q, --quiet');
program
.command('hello <name>')
.description('Greeting')
.action((name, cmdInstance) => {
console.log(`Hello ${name}!`);
});
program
.useSubcommand(journalSubCommand);
if (process.argv.length <= 2) {
program.help();
} else {
program.parse(process.argv);
}