-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
use-subcommand.js
executable file
·58 lines (48 loc) · 1.45 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
#!/usr/bin/env node
// This is an example of useSubcommand
//
// $ use-subcommand journal list myjounal
// $ use-subcommand journal delete myjounal
//
// With options
// $ use-subcommand journal -q delete -f myjounal
// const { Command } = require('commander');
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.quiet));
});
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.quiet));
console.log('Force =', Boolean(cmdInstance.force));
});
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')
.useSubcommand(journalSubCommand);
program
.command('hello <name>')
.description('Greeting')
.action((name, cmdInstance) => {
console.log(`Hello ${name}!`);
});
if (process.argv.length <= 2) program.help();
program.parse(process.argv);