forked from libpag/vendor_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandLine.js
100 lines (93 loc) · 3.11 KB
/
CommandLine.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const os = require("os");
const KnownPlatforms = ["win", "mac", "ios", "linux", "android", "web"];
function getOptionNameMap(optionDeclarations) {
let optionNameMap = {};
let shortOptionNames = {};
optionDeclarations.forEach(function (option) {
optionNameMap[option.name.toLowerCase()] = option;
if (option.shortName) {
shortOptionNames[option.shortName] = option.name;
}
});
return {optionNameMap: optionNameMap, shortOptionNames: shortOptionNames};
}
function parse(args, optionDeclarations) {
let options = {};
options.errors = [];
options.targets = [];
let _a = getOptionNameMap(optionDeclarations), optionNameMap = _a.optionNameMap,
shortOptionNames = _a.shortOptionNames;
let i = 0;
while (i < args.length) {
let s = args[i];
i++;
if (s.charAt(0) === "-") {
s = s.slice(s.charAt(1) === "-" ? 2 : 1).toLowerCase();
if (s in shortOptionNames) {
s = shortOptionNames[s];
}
if (s in optionNameMap) {
let opt = optionNameMap[s];
if (!args[i] && opt.type !== "boolean") {
options.errors.push("Option '" + opt.name + "' expects an argument.");
}
switch (opt.type) {
case "number":
options[opt.name] = parseInt(args[i]);
i++;
break;
case "boolean":
options[opt.name] = true;
break;
case "string":
options[opt.name] = args[i] || "";
i++;
break;
}
} else {
options.errors.push("Unknown option '" + s + "'.");
}
} else {
options.targets.push(s);
}
}
if (!options.platform) {
let p = os.platform();
if (p === "darwin") {
options.platform = "mac";
} else if (p === "win32") {
options.platform = "win";
} else if (p === "linux") {
options.platform = "linux";
} else {
options.help = true;
}
} else if (KnownPlatforms.indexOf(options.platform) === -1) {
options.errors.push("Unknown platform '" + options.platform + "'.");
}
if (options.errors.length > 0) {
for (let error of options.errors) {
process.stderr.write(error + "\n");
}
options.help = true;
}
return options;
}
function makePadding(paddingLength) {
return Array(paddingLength + 1).join(" ");
}
function printOptions(optionDeclarations) {
let output = "Options:\n";
optionDeclarations.forEach(function (option) {
let name = "";
if (option.shortName) {
name += "-" + option.shortName + ", ";
}
name += "--" + option.name;
name += makePadding(25 - name.length);
output += name + option.description + "\n";
});
return output;
}
exports.parse = parse;
exports.printOptions = printOptions;