Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80621 views
1
// Generated by LiveScript 1.3.1
2
(function(){
3
var VERSION, ref$, id, map, compact, any, groupBy, partition, chars, isItNaN, keys, Obj, camelize, deepIs, closestString, nameToRaw, dasherize, generateHelp, generateHelpForOption, parsedTypeCheck, parseType, parseLevn, camelizeKeys, parseString, main, toString$ = {}.toString, slice$ = [].slice;
4
VERSION = '0.5.0';
5
ref$ = require('prelude-ls'), id = ref$.id, map = ref$.map, compact = ref$.compact, any = ref$.any, groupBy = ref$.groupBy, partition = ref$.partition, chars = ref$.chars, isItNaN = ref$.isItNaN, keys = ref$.keys, Obj = ref$.Obj, camelize = ref$.camelize;
6
deepIs = require('deep-is');
7
ref$ = require('./util'), closestString = ref$.closestString, nameToRaw = ref$.nameToRaw, dasherize = ref$.dasherize;
8
ref$ = require('./help'), generateHelp = ref$.generateHelp, generateHelpForOption = ref$.generateHelpForOption;
9
ref$ = require('type-check'), parsedTypeCheck = ref$.parsedTypeCheck, parseType = ref$.parseType;
10
parseLevn = require('levn').parsedTypeParse;
11
camelizeKeys = function(obj){
12
var key, value, resultObj$ = {};
13
for (key in obj) {
14
value = obj[key];
15
resultObj$[camelize(key)] = value;
16
}
17
return resultObj$;
18
};
19
parseString = function(string){
20
var assignOpt, regex, replaceRegex, result;
21
assignOpt = '--?[a-zA-Z][-a-z-A-Z0-9]*=';
22
regex = RegExp('(?:' + assignOpt + ')?(?:\'(?:\\\\\'|[^\'])+\'|"(?:\\\\"|[^"])+")|[^\'"\\s]+', 'g');
23
replaceRegex = RegExp('^(' + assignOpt + ')?[\'"]([\\s\\S]*)[\'"]$');
24
result = map(function(it){
25
return it.replace(replaceRegex, '$1$2');
26
}, string.match(regex) || []);
27
return result;
28
};
29
main = function(libOptions){
30
var opts, defaults, required, traverse, getOption, parse;
31
opts = {};
32
defaults = {};
33
required = [];
34
if (toString$.call(libOptions.stdout).slice(8, -1) === 'Undefined') {
35
libOptions.stdout = process.stdout;
36
}
37
traverse = function(options){
38
var i$, len$, option, name, e, parsedPossibilities, parsedType, j$, ref$, len1$, possibility, that, rawDependsType, dependsOpts, dependsType, alias, shortNames, longNames;
39
if (toString$.call(options).slice(8, -1) !== 'Array') {
40
throw new Error('No options defined.');
41
}
42
for (i$ = 0, len$ = options.length; i$ < len$; ++i$) {
43
option = options[i$];
44
if (option.heading == null) {
45
name = option.option;
46
if (opts[name] != null) {
47
throw new Error("Option '" + name + "' already defined.");
48
}
49
if (option.type === 'Boolean') {
50
option.boolean == null && (option.boolean = true);
51
}
52
if (option.parsedType == null) {
53
if (!option.type) {
54
throw new Error("No type defined for option '" + name + "'.");
55
}
56
try {
57
option.parsedType = parseType(option.type);
58
} catch (e$) {
59
e = e$;
60
throw new Error("Option '" + name + "': Error parsing type '" + option.type + "': " + e.message);
61
}
62
}
63
if (option['default']) {
64
try {
65
defaults[name] = parseLevn(option.parsedType, option['default']);
66
} catch (e$) {
67
e = e$;
68
throw new Error("Option '" + name + "': Error parsing default value '" + option['default'] + "' for type '" + option.type + "': " + e.message);
69
}
70
}
71
if (option['enum'] && !option.parsedPossiblities) {
72
parsedPossibilities = [];
73
parsedType = option.parsedType;
74
for (j$ = 0, len1$ = (ref$ = option['enum']).length; j$ < len1$; ++j$) {
75
possibility = ref$[j$];
76
try {
77
parsedPossibilities.push(parseLevn(parsedType, possibility));
78
} catch (e$) {
79
e = e$;
80
throw new Error("Option '" + name + "': Error parsing enum value '" + possibility + "' for type '" + option.type + "': " + e.message);
81
}
82
}
83
option.parsedPossibilities = parsedPossibilities;
84
}
85
if (that = option.dependsOn) {
86
if (that.length) {
87
ref$ = [].concat(option.dependsOn), rawDependsType = ref$[0], dependsOpts = slice$.call(ref$, 1);
88
dependsType = rawDependsType.toLowerCase();
89
if (dependsOpts.length) {
90
if (dependsType === 'and' || dependsType === 'or') {
91
option.dependsOn = [dependsType].concat(slice$.call(dependsOpts));
92
} else {
93
throw new Error("Option '" + name + "': If you have more than one dependency, you must specify either 'and' or 'or'");
94
}
95
} else {
96
if ((ref$ = dependsType.toLowerCase()) === 'and' || ref$ === 'or') {
97
option.dependsOn = null;
98
} else {
99
option.dependsOn = ['and', rawDependsType];
100
}
101
}
102
} else {
103
option.dependsOn = null;
104
}
105
}
106
if (option.required) {
107
required.push(name);
108
}
109
opts[name] = option;
110
if (option.alias || option.aliases) {
111
if (name === 'NUM') {
112
throw new Error("-NUM option can't have aliases.");
113
}
114
if (option.alias) {
115
option.aliases == null && (option.aliases = [].concat(option.alias));
116
}
117
for (j$ = 0, len1$ = (ref$ = option.aliases).length; j$ < len1$; ++j$) {
118
alias = ref$[j$];
119
if (opts[alias] != null) {
120
throw new Error("Option '" + alias + "' already defined.");
121
}
122
opts[alias] = option;
123
}
124
ref$ = partition(fn$, option.aliases), shortNames = ref$[0], longNames = ref$[1];
125
option.shortNames == null && (option.shortNames = shortNames);
126
option.longNames == null && (option.longNames = longNames);
127
}
128
if ((!option.aliases || option.shortNames.length === 0) && option.type === 'Boolean' && option['default'] === 'true') {
129
option.negateName = true;
130
}
131
}
132
}
133
function fn$(it){
134
return it.length === 1;
135
}
136
};
137
traverse(libOptions.options);
138
getOption = function(name){
139
var opt, possiblyMeant;
140
opt = opts[name];
141
if (opt == null) {
142
possiblyMeant = closestString(keys(opts), name);
143
throw new Error("Invalid option '" + nameToRaw(name) + "'" + (possiblyMeant ? " - perhaps you meant '" + nameToRaw(possiblyMeant) + "'?" : '.'));
144
}
145
return opt;
146
};
147
parse = function(input, arg$){
148
var slice, obj, positional, restPositional, overrideRequired, prop, setValue, setDefaults, checkRequired, mutuallyExclusiveError, checkMutuallyExclusive, checkDependency, checkDependencies, args, key, value, option, ref$, i$, len$, arg, that, result, short, argName, usingAssign, val, flags, len, j$, len1$, i, flag, opt, name, negated, noedName, valPrime;
149
slice = (arg$ != null
150
? arg$
151
: {}).slice;
152
obj = {};
153
positional = [];
154
restPositional = false;
155
overrideRequired = false;
156
prop = null;
157
setValue = function(name, value){
158
var opt, val, e, currentType;
159
opt = getOption(name);
160
if (opt.boolean) {
161
val = value;
162
} else {
163
try {
164
val = parseLevn(opt.parsedType, value);
165
} catch (e$) {
166
e = e$;
167
throw new Error("Invalid value for option '" + name + "' - expected type " + opt.type + ", received value: " + value + ".");
168
}
169
if (opt['enum'] && !any(function(it){
170
return deepIs(it, val);
171
}, opt.parsedPossibilities)) {
172
throw new Error("Option " + name + ": '" + val + "' not in [" + opt['enum'].join(', ') + "].");
173
}
174
}
175
currentType = toString$.call(obj[name]).slice(8, -1);
176
if (obj[name] != null) {
177
if (libOptions.concatRepeatedArrays && currentType === 'Array') {
178
obj[name] = obj[name].concat(val);
179
} else if (libOptions.mergeRepeatedObjects && currentType === 'Object') {
180
import$(obj[name], val);
181
} else {
182
obj[name] = val;
183
}
184
} else {
185
obj[name] = val;
186
}
187
if (opt.restPositional) {
188
restPositional = true;
189
}
190
if (opt.overrideRequired) {
191
overrideRequired = true;
192
}
193
};
194
setDefaults = function(){
195
var name, ref$, value;
196
for (name in ref$ = defaults) {
197
value = ref$[name];
198
if (obj[name] == null) {
199
obj[name] = value;
200
}
201
}
202
};
203
checkRequired = function(){
204
var i$, ref$, len$, name;
205
if (overrideRequired) {
206
return;
207
}
208
for (i$ = 0, len$ = (ref$ = required).length; i$ < len$; ++i$) {
209
name = ref$[i$];
210
if (!obj[name]) {
211
throw new Error("Option " + nameToRaw(name) + " is required.");
212
}
213
}
214
};
215
mutuallyExclusiveError = function(first, second){
216
throw new Error("The options " + nameToRaw(first) + " and " + nameToRaw(second) + " are mutually exclusive - you cannot use them at the same time.");
217
};
218
checkMutuallyExclusive = function(){
219
var rules, i$, len$, rule, present, j$, len1$, element, k$, len2$, opt;
220
rules = libOptions.mutuallyExclusive;
221
if (!rules) {
222
return;
223
}
224
for (i$ = 0, len$ = rules.length; i$ < len$; ++i$) {
225
rule = rules[i$];
226
present = null;
227
for (j$ = 0, len1$ = rule.length; j$ < len1$; ++j$) {
228
element = rule[j$];
229
if (toString$.call(element).slice(8, -1) === 'Array') {
230
for (k$ = 0, len2$ = element.length; k$ < len2$; ++k$) {
231
opt = element[k$];
232
if (opt in obj) {
233
if (present != null) {
234
mutuallyExclusiveError(present, opt);
235
} else {
236
present = opt;
237
break;
238
}
239
}
240
}
241
} else {
242
if (element in obj) {
243
if (present != null) {
244
mutuallyExclusiveError(present, element);
245
} else {
246
present = element;
247
}
248
}
249
}
250
}
251
}
252
};
253
checkDependency = function(option){
254
var dependsOn, type, targetOptionNames, i$, len$, targetOptionName, targetOption;
255
dependsOn = option.dependsOn;
256
if (!dependsOn || option.dependenciesMet) {
257
return true;
258
}
259
type = dependsOn[0], targetOptionNames = slice$.call(dependsOn, 1);
260
for (i$ = 0, len$ = targetOptionNames.length; i$ < len$; ++i$) {
261
targetOptionName = targetOptionNames[i$];
262
targetOption = obj[targetOptionName];
263
if (targetOption && checkDependency(targetOption)) {
264
if (type === 'or') {
265
return true;
266
}
267
} else if (type === 'and') {
268
throw new Error("The option '" + option.option + "' did not have its dependencies met.");
269
}
270
}
271
if (type === 'and') {
272
return true;
273
} else {
274
throw new Error("The option '" + option.option + "' did not meet any of its dependencies.");
275
}
276
};
277
checkDependencies = function(){
278
var name;
279
for (name in obj) {
280
checkDependency(opts[name]);
281
}
282
};
283
switch (toString$.call(input).slice(8, -1)) {
284
case 'String':
285
args = parseString(input.slice(slice != null ? slice : 0));
286
break;
287
case 'Array':
288
args = input.slice(slice != null ? slice : 2);
289
break;
290
case 'Object':
291
obj = {};
292
for (key in input) {
293
value = input[key];
294
if (key !== '_') {
295
option = getOption(dasherize(key));
296
if (parsedTypeCheck(option.parsedType, value)) {
297
obj[option.option] = value;
298
} else {
299
throw new Error("Option '" + option.option + "': Invalid type for '" + value + "' - expected type '" + option.type + "'.");
300
}
301
}
302
}
303
checkMutuallyExclusive();
304
checkDependencies();
305
setDefaults();
306
checkRequired();
307
return ref$ = camelizeKeys(obj), ref$._ = input._ || [], ref$;
308
default:
309
throw new Error("Invalid argument to 'parse': " + input + ".");
310
}
311
for (i$ = 0, len$ = args.length; i$ < len$; ++i$) {
312
arg = args[i$];
313
if (arg === '--') {
314
restPositional = true;
315
} else if (restPositional) {
316
positional.push(arg);
317
} else {
318
if (that = arg.match(/^(--?)([a-zA-Z][-a-zA-Z0-9]*)(=)?(.*)?$/)) {
319
result = that;
320
if (prop) {
321
throw new Error("Value for '" + prop + "' of type '" + getOption(prop).type + "' required.");
322
}
323
short = result[1].length === 1;
324
argName = result[2];
325
usingAssign = result[3] != null;
326
val = result[4];
327
if (usingAssign && val == null) {
328
throw new Error("No value for '" + argName + "' specified.");
329
}
330
if (short) {
331
flags = chars(argName);
332
len = flags.length;
333
for (j$ = 0, len1$ = flags.length; j$ < len1$; ++j$) {
334
i = j$;
335
flag = flags[j$];
336
opt = getOption(flag);
337
name = opt.option;
338
if (restPositional) {
339
positional.push(flag);
340
} else if (opt.boolean) {
341
setValue(name, true);
342
} else if (i === len - 1) {
343
if (usingAssign) {
344
setValue(name, val);
345
} else {
346
prop = name;
347
}
348
} else {
349
throw new Error("Can't set argument '" + flag + "' when not last flag in a group of short flags.");
350
}
351
}
352
} else {
353
negated = false;
354
if (that = argName.match(/^no-(.+)$/)) {
355
negated = true;
356
noedName = that[1];
357
opt = getOption(noedName);
358
} else {
359
opt = getOption(argName);
360
}
361
name = opt.option;
362
if (opt.boolean) {
363
valPrime = usingAssign ? parseLevn([{
364
type: 'Boolean'
365
}], val) : true;
366
if (negated) {
367
setValue(name, !valPrime);
368
} else {
369
setValue(name, valPrime);
370
}
371
} else {
372
if (negated) {
373
throw new Error("Only use 'no-' prefix for Boolean options, not with '" + noedName + "'.");
374
}
375
if (usingAssign) {
376
setValue(name, val);
377
} else {
378
prop = name;
379
}
380
}
381
}
382
} else if (that = arg.match(/^-([0-9]+(?:\.[0-9]+)?)$/)) {
383
opt = opts.NUM;
384
if (!opt) {
385
throw new Error('No -NUM option defined.');
386
}
387
setValue(opt.option, that[1]);
388
} else {
389
if (prop) {
390
setValue(prop, arg);
391
prop = null;
392
} else {
393
positional.push(arg);
394
}
395
}
396
}
397
}
398
checkMutuallyExclusive();
399
checkDependencies();
400
setDefaults();
401
checkRequired();
402
return ref$ = camelizeKeys(obj), ref$._ = positional, ref$;
403
};
404
return {
405
parse: parse,
406
generateHelp: generateHelp(libOptions),
407
generateHelpForOption: generateHelpForOption(getOption, libOptions)
408
};
409
};
410
main.VERSION = VERSION;
411
module.exports = main;
412
function import$(obj, src){
413
var own = {}.hasOwnProperty;
414
for (var key in src) if (own.call(src, key)) obj[key] = src[key];
415
return obj;
416
}
417
}).call(this);
418
419