-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption-parser.js
338 lines (283 loc) · 7.94 KB
/
option-parser.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
(function () {
"use strict";
var OptionType = {
normal: function (opt) {
var s = " ";
if (opt.shortCode) {
s += "-" + opt.shortCode + ", ";
}
if (opt.longCode) {
s += "--" + opt.longCode;
}
if (opt.valueIsRequired()) {
s += "[=]";
}
s += ": ";
if (opt.description) {
s += opt.description;
}
if (opt.hasDefaultValue()) {
s += " [default: " + opt.value['default'] + "]";
}
s += "\n";
return s;
},
help: function (opt) {
return OptionType.normal(opt);
},
separator: function (opt) {
var s = "";
if (opt.description) {
if (opt.description.startsWith("--")) {
s += "\n" + "-".repeat(80);
} else if (opt.description.startsWith(" --")) {
s += "\n " + "-".repeat(77);
}
}
return s + "\n".repeat(opt.data.length || 1);
},
note: function (opt) {
if (opt.description) {
return " NOTE: " + opt.description + ")\n";
}
return "";
},
footer: function (opt) {
if (opt.description) {
return opt.description + "\n";
}
return "";
}
};
if (typeof String.prototype.startsWith !== "function") {
String.prototype.startsWith = function (str) {
return this.lastIndexOf(str, 0) === 0;
};
}
if (typeof String.prototype.startsWithICase !== "function") {
String.prototype.startsWithICase = function (str) {
return this.toLowerCase().startsWith(str.toLowerCase());
};
}
if (typeof String.prototype.repeat !== "function") {
String.prototype.repeat = function(count) {
if (count < 1) {
return "";
}
var result = "", pattern = this.valueOf();
while (count > 0) {
if (count & 1) {
result += pattern;
}
count >>= 1, pattern += pattern;
}
return result;
};
}
if (typeof String.prototype.toCamelCase !== "function") {
String.prototype.toCamelCase = function () {
return this.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase() });
};
}
function OptionParser(args, scriptName, options) {
this.args = args || [];
this.scriptName = scriptName || this.args[0] || "";
this.types = Object.keys(OptionType);
this.options = options || [];
}
/**
* creates a new OptionParser object
* @param {string} scriptName used in the banner
* @param {array} options array of Option
* @return {OptionParser}
*/
OptionParser.create = function (scriptName, options) {
return new OptionParser(scriptName, options);
};
/**
* creates a new Option object
* @param {object} optionParams params to override on the Option object
* @return {Option}
*/
OptionParser.createOption = function (optionParams) {
var option = new Option(optionParams);
return option;
};
/**
* creates a simple error object
* @param {string} msg
* @param {number} code
* @return {object}
*/
OptionParser.generateError = function (msg, code) {
return {
msg: msg || "",
code: code || -1
};
};
/**
* adds an option to the options array if it doesn"t exist
* @param {Option} option adds the option object to the options array if id does not exist
* @return {OptionParser} self allows for chaining
*/
OptionParser.prototype.addOption = function (optionParams) {
var self = this,
option, found;
if (optionParams.forEach) {
optionParams.forEach(function (op) {
option = OptionParser.createOption(op);
found = self.findOption(option.getId());
if (!found) {
self.options.push(option);
}
});
} else {
option = OptionParser.createOption(optionParams);
found = self.findOption(option.getId());
if (!found) {
self.options.push(option);
}
}
return self;
};
/**
* attempts to find an option in the options array by id
* @param {string} id or longCode or ShortCode
* @return {Option} null if not found
*/
OptionParser.prototype.findOption = function (id) {
var self = this, i;
for (i = 0; i < self.options.length; i += 1) {
if (
self.options[i].getId() === id ||
self.options[i].longCode === id ||
self.options[i].shortCode === id
) {
return self.options[i];
}
}
return null;
};
OptionParser.prototype.getBanner = function () {
var self = this,
s;
s = "\nUsage: " + self.scriptName + " [options]\n" +
"\nOptions (case-sensitive):\n\n";
self.options.forEach(function (opt) {
opt.type = opt.type || OptionType.normal;
s += opt.type(opt);
});
return s;
};
OptionParser.prototype.parse = function (callback) {
var self = this,
i, arg, argCode, index, opt, nextArg, msg;
// skip the first arg
for (i = 1; i < self.args.length; i += 1) {
arg = self.args[i];
if (arg.startsWith("--")) {
argCode = arg.slice(2).toLowerCase(); // strip the switch and lcase
} else if (arg.startsWith("-")) {
argCode = arg.slice(1); // strip the switch
}
// remove from code, if value passed as "="
index = argCode.indexOf("=");
if (index >= 1) {
argCode = argCode.slice(0, index);
}
// compare the args to the options array
opt = self.findOption(argCode);
if (opt) {
if (opt.type === OptionType.help) {
callback(true, null);
}
// grab the next arg, if any, for comparison
nextArg = self.args[(i + 1)];
if (arg.indexOf("=") >= 1) {
opt.value.current = arg.split("=")[1];
} else if ( !nextArg || (nextArg && nextArg.startsWith("-")) ) {
// the switch was provided but not a requisite value
if (opt.valueIsRequired() && !opt.getValue()) {
callback(OptionParser.generateError(argCode + " requires a value"), null);
}
opt.value.current = true;
} else {
i += 1;
opt.value.current = nextArg;
}
} else {
callback(OptionParser.generateError(argCode + " not found in options"), null);
}
}
// process required switches
self.options.forEach(function (option, i) {
var found;
if (option.required && typeof option.required === "string") {
found = self.findOption(option.required);
if (found && (!option.getValue() && !found.getValue())) {
msg = option.getCode() + " -OR- " + found.getCode() + " is required";
callback(OptionParser.generateError(msg), null);
}
} else if (option.required && !option.getValue()) {
callback(OptionParser.generateError(option.getCode() + " is required"), null);
}
});
callback(null, self.options);
};
function Option(opt) {
var self = this,
keys, filtered;
opt = opt || {};
self.longCode = opt.longCode || opt.lCode || null;
self.shortCode = opt.shortCode || opt.sCode || null;
self.description = opt.description || opt.desc || null;
self.type = opt.type || OptionType.normal;
self.value = {
current: (opt.value && opt.value.current) || null,
"default": (opt.value && opt.value["default"]) || null,
required: (opt.value && opt.value.required) || false
};
self.required = opt.required || false;
self.data = opt.data || {};
self.id = opt.id || self.longCode || self.shortCode || null;
// push any excess into the data object
keys = Object.keys(self);
filtered = Object.keys(opt).filter(function (item) {
return (keys.indexOf(item) < 0);
});
filtered.forEach(function (key) {
if (opt[key]) {
self.data[key] = opt[key];
}
});
}
Option.prototype.getCode = function () {
return this.longCode || this.shortCode || null;
};
Option.prototype.getValue = function () {
return this.value.current;
};
Option.prototype.hasDefaultValue = function () {
return this.value["default"];
};
Option.prototype.valueIsRequired = function () {
return this.value.required;
};
Option.prototype.hasData = function () {
return (this.data.length && this.data.length > 0);
};
Option.prototype.getId = function () {
var id = this.id || this.longCode || this.shortCode || null;
// convert id to camelCase
if (id) {
return id.toCamelCase();
}
return this.id;
};
if (module.exports) {
module.exports = {
create: OptionParser.create,
OptionType: OptionType
};
}
})();