-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (50 loc) · 1.4 KB
/
index.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
const commandLineArgs = require('command-line-args');
const getUsage = require('command-line-usage');
class CLA {
constructor(argDefs, title, description) {
this.argDefs = argDefs;
this.argDefs.push({
name: 'help',
alias: 'h',
type: Boolean,
});
this.title = title;
this.description = description;
}
usage(code) {
const sections = [
{
header: this.title,
content: this.description
},
{
header: 'Options',
optionList: this.argDefs
}
];
console.log(getUsage(sections));
process.exit(code);
}
parse() {
try {
const options = commandLineArgs(this.argDefs);
if (options.help) {
this.usage(0);
}
for (const argDef of this.argDefs) {
if (argDef.required && ! options[argDef.name]) {
console.log(`Missing required parameter [${argDef.name}]`);
this.usage(1);
}
}
return options;
} catch (ex) {
console.log(ex.message);
console.log("Usage:")
this.usage(1);
}
}
}
export function cla(argDefs, title, description) {
return new CLA(argDefs, title, description).parse();
}