flags 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -17
- package/dist/flags.cjs +347 -0
- package/dist/flags.cjs.map +1 -0
- package/dist/flags.d.cts +82 -0
- package/dist/flags.d.ts +82 -0
- package/dist/flags.js +311 -0
- package/dist/flags.js.map +1 -0
- package/package.json +39 -15
- package/examples/example.js +0 -34
- package/lib/flags.js +0 -495
- package/lib/flags_test.js +0 -204
package/lib/flags.js
DELETED
|
@@ -1,495 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Flag parsing library for node.js. See ../README.md for info.
|
|
3
|
-
*
|
|
4
|
-
* @author dan@pupi.us (Daniel Pupius)
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
var util = require('util');
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* An object containing a map of flag objects. If different modules are
|
|
12
|
-
* importing different versions of the library we want them to use the same
|
|
13
|
-
* FLAGS object. This does mean that the flags library needs to be backwards
|
|
14
|
-
* compatible. This is a pretty ugly solution, should look for a better one.
|
|
15
|
-
* @type {!Object}
|
|
16
|
-
*/
|
|
17
|
-
if (typeof GLOBAL_FLAGS == 'undefined') {
|
|
18
|
-
var registerFlags = true;
|
|
19
|
-
GLOBAL_FLAGS = {};
|
|
20
|
-
}
|
|
21
|
-
var FLAGS = exports.FLAGS = GLOBAL_FLAGS;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* If true, then the process will be nicely exit on a parse error. If false,
|
|
26
|
-
* an Error will be thrown. Set to false in tests.
|
|
27
|
-
* @type {boolean}
|
|
28
|
-
*/
|
|
29
|
-
exports.exitOnError = true;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Allows an app to add extra usage information that will be shown in the help
|
|
34
|
-
* message, above the flags.
|
|
35
|
-
* @type {string}
|
|
36
|
-
*/
|
|
37
|
-
exports.usageInfo = 'Usage: node ' +
|
|
38
|
-
(process.argv[1] || '').split('/').pop() + ' [options]';
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Defines a string flag. e.g. --servername=bob
|
|
43
|
-
* @param {string} name The flag name, should be [a-zA-Z0-9]+.
|
|
44
|
-
* @param {string=} opt_default The default value, should the flag not be
|
|
45
|
-
* explicitly specified.
|
|
46
|
-
* @param {string=} opt_description Optional description to use in help text.
|
|
47
|
-
* @return {!Flag} The flag object.
|
|
48
|
-
*/
|
|
49
|
-
exports.defineString = function(name, opt_default, opt_description) {
|
|
50
|
-
return addFlag(name, new Flag(name, opt_default, opt_description));
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Defines a boolean flag. e.g. --turnonlights
|
|
56
|
-
* @param {string} name The flag name, should be [a-zA-Z0-9]+.
|
|
57
|
-
* @param {boolean=} opt_default The default value, should the flag not be
|
|
58
|
-
* explicitly specified.
|
|
59
|
-
* @param {string=} opt_description Optional description to use in help text.
|
|
60
|
-
* @return {!BooleanFlag} The flag object.
|
|
61
|
-
*/
|
|
62
|
-
exports.defineBoolean = function(name, opt_default, opt_description) {
|
|
63
|
-
return addFlag(name, new BooleanFlag(name, opt_default, opt_description));
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Defines an integer flag. e.g. --age=12
|
|
69
|
-
* @param {string} name The flag name, should be [a-zA-Z0-9]+.
|
|
70
|
-
* @param {number=} opt_default The default value, should the flag not be
|
|
71
|
-
* explicitly specified.
|
|
72
|
-
* @param {string=} opt_description Optional description to use in help text.
|
|
73
|
-
* @return {!IntegerFlag} The flag object.
|
|
74
|
-
*/
|
|
75
|
-
exports.defineInteger = function(name, opt_default, opt_description) {
|
|
76
|
-
return addFlag(name, new IntegerFlag(name, opt_default, opt_description));
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Defines a number flag. e.g. --number=1.345
|
|
82
|
-
* @param {string} name The flag name, should be [a-zA-Z0-9]+.
|
|
83
|
-
* @param {number=} opt_default The default value, should the flag not be
|
|
84
|
-
* explicitly specified.
|
|
85
|
-
* @param {string=} opt_description Optional description to use in help text.
|
|
86
|
-
* @return {!NumberFlag} The flag object.
|
|
87
|
-
*/
|
|
88
|
-
exports.defineNumber = function(name, opt_default, opt_description) {
|
|
89
|
-
return addFlag(name, new NumberFlag(name, opt_default, opt_description));
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Defines a string list flag. e.g. --anmial=frog,bat,chicken
|
|
95
|
-
* @param {string} name The flag name, should be [a-zA-Z0-9]+.
|
|
96
|
-
* @param {!Array.<string>=} opt_default The default value, should the flag not
|
|
97
|
-
* be explicitly specified.
|
|
98
|
-
* @param {string=} opt_description Optional description to use in help text.
|
|
99
|
-
* @return {!StringListFlag} The flag object.
|
|
100
|
-
*/
|
|
101
|
-
exports.defineStringList = function(name, opt_default, opt_description) {
|
|
102
|
-
return addFlag(name, new StringListFlag(name, opt_default, opt_description));
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Defines a multi string flag. e.g. --allowedip=127.0.0.1 --allowedip=127.0.0.2
|
|
108
|
-
* @param {string} name The flag name, should be [a-zA-Z0-9]+.
|
|
109
|
-
* @param {!Array.<string>=} opt_default The default value, should the flag not
|
|
110
|
-
* be explicitly specified.
|
|
111
|
-
* @param {string=} opt_description Optional description to use in help text.
|
|
112
|
-
* @return {!MultiStringFlag} The flag object.
|
|
113
|
-
*/
|
|
114
|
-
exports.defineMultiString = function(name, opt_default, opt_description) {
|
|
115
|
-
return addFlag(name, new MultiStringFlag(name, opt_default, opt_description));
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Dumps the help text to the console.
|
|
121
|
-
*/
|
|
122
|
-
exports.help = function() {
|
|
123
|
-
if (exports.usageInfo) {
|
|
124
|
-
console.log(exports.usageInfo + '\n');
|
|
125
|
-
}
|
|
126
|
-
console.log('Options:');
|
|
127
|
-
for (var flag in FLAGS) {
|
|
128
|
-
if (!FLAGS[flag].isSecret) {
|
|
129
|
-
console.log(FLAGS[flag].toHelpString());
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Resets the flag values.
|
|
137
|
-
*/
|
|
138
|
-
exports.reset = function() {
|
|
139
|
-
parseCalled = false;
|
|
140
|
-
FLAGS = exports.FLAGS = GLOBAL_FLAGS = {};
|
|
141
|
-
registerInternalFlags();
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Gets the current value of the given flag.
|
|
147
|
-
* @param {string} name The flag name.
|
|
148
|
-
* @return {*}
|
|
149
|
-
*/
|
|
150
|
-
exports.get = function(name) {
|
|
151
|
-
if (!FLAGS[name]) throw Error('Unknown flag "' + name + '"');
|
|
152
|
-
return FLAGS[name].get(name);
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Parses process.argv for flags. Idempotent if called multiple times.
|
|
158
|
-
* @param {Array.<string>=} opt_args Optional arguments array to use instead of
|
|
159
|
-
* process.argv.
|
|
160
|
-
* @param {boolean} opt_ignoreUnrecognized Don't throw errors if unrecognized
|
|
161
|
-
* flags are encountered.
|
|
162
|
-
*/
|
|
163
|
-
exports.parse = function(opt_args, opt_ignoreUnrecognized) {
|
|
164
|
-
var args = opt_args || process.argv.slice(2);
|
|
165
|
-
if (parseCalled) return;
|
|
166
|
-
|
|
167
|
-
var parsedFlags = {};
|
|
168
|
-
var lastflag = null;
|
|
169
|
-
for (var i = 0; i < args.length; i++) {
|
|
170
|
-
var arg = args[i];
|
|
171
|
-
|
|
172
|
-
// Terminate any flag processing
|
|
173
|
-
if (arg == '--') {
|
|
174
|
-
break;
|
|
175
|
-
|
|
176
|
-
// Handle a typical long form flag --foo or --foo=bar.
|
|
177
|
-
} else if (arg.substr(0, 2) == '--') {
|
|
178
|
-
var flag = arg.substr(2);
|
|
179
|
-
var value = null;
|
|
180
|
-
if (flag.indexOf('=') > -1) {
|
|
181
|
-
var parts = flag.split('=');
|
|
182
|
-
flag = parts[0];
|
|
183
|
-
value = parts.slice(1).join('=');
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// Hacky munging of the args to handle space separated flags.
|
|
187
|
-
if (value == null && args[i + 1] && args[i + 1].substr(0, 2) != '--') {
|
|
188
|
-
args[i] = args[i] + ' ' + args[i + 1];
|
|
189
|
-
value = args[i + 1];
|
|
190
|
-
args.splice(i + 1, 1);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// Special case boolean flags of the form '--noflagname'.
|
|
194
|
-
if (!FLAGS[flag] && value == null && flag.substr(0, 2) == 'no') {
|
|
195
|
-
flag = flag.substr(2);
|
|
196
|
-
value = '0';
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (FLAGS[flag]) {
|
|
200
|
-
try {
|
|
201
|
-
FLAGS[flag].set(value);
|
|
202
|
-
} catch (e) {
|
|
203
|
-
throwFlagParseError(args, i, e.message);
|
|
204
|
-
}
|
|
205
|
-
} else if (!opt_ignoreUnrecognized) {
|
|
206
|
-
throwFlagParseError(args, i, 'Unrecognized flag name "' + arg + '"');
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// For now we only handle simple flags like --foo=bar, so fail out.
|
|
210
|
-
} else {
|
|
211
|
-
throwFlagParseError(args, i, 'Invalid argument "' + arg + '"');
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
parseCalled = true;
|
|
216
|
-
|
|
217
|
-
// Intercept the --help flag.
|
|
218
|
-
if (FLAGS.help.get()) {
|
|
219
|
-
exports.help();
|
|
220
|
-
process.exit(0);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (i != args.length) {
|
|
224
|
-
return args.slice(i + 1);
|
|
225
|
-
} else {
|
|
226
|
-
return [];
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
// Private helpers
|
|
232
|
-
//==================
|
|
233
|
-
|
|
234
|
-
var parseCalled = false;
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
function throwFlagParseError(args, i, msg) {
|
|
238
|
-
// Show a nice error message with the offending arg underlined.
|
|
239
|
-
var msg = 'FLAG PARSING ERROR: ' + msg + '\n ' +
|
|
240
|
-
args.join(' ') + '\n ' +
|
|
241
|
-
new Array(args.slice(0, i).join(' ').length + 2).join(' ') +
|
|
242
|
-
new Array(args[i].length + 1).join('^');
|
|
243
|
-
|
|
244
|
-
if (exports.exitOnError) {
|
|
245
|
-
console.error(msg);
|
|
246
|
-
process.exit(1);
|
|
247
|
-
} else {
|
|
248
|
-
throw Error(msg);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
function addFlag(name, flag) {
|
|
254
|
-
if (parseCalled) throw Error('Can not register new flags after parse()');
|
|
255
|
-
if (FLAGS[name]) throw Error('Flag already defined: "' + flag + '"')
|
|
256
|
-
FLAGS[name] = flag;
|
|
257
|
-
return flag;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
function wrapText(text, maxLen) {
|
|
262
|
-
var lines = text.split('\n');
|
|
263
|
-
var out = [];
|
|
264
|
-
for (var i = 0; i < lines.length; i++) {
|
|
265
|
-
// Adjust the maxLength for to take into account the lack of indent on the
|
|
266
|
-
// first line.
|
|
267
|
-
var maxLenx = maxLen + (out.length == 0 ? 4 : 0);
|
|
268
|
-
var line = lines[i];
|
|
269
|
-
if (line.length < maxLenx) {
|
|
270
|
-
// Line doesn't exceed length so just push it.
|
|
271
|
-
out.push(line)
|
|
272
|
-
} else {
|
|
273
|
-
// Wrap the line on spaces.
|
|
274
|
-
// TODO : Would be nice to split URLs and long phrases that have no
|
|
275
|
-
// natural spaces.
|
|
276
|
-
var current = wrapLine(line, ' ', maxLenx, out);
|
|
277
|
-
if (current != '') {
|
|
278
|
-
out.push(current);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
return out.join('\n ');
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
function wrapLine(line, delimiter, maxLen, out) {
|
|
287
|
-
var parts = line.split(delimiter);
|
|
288
|
-
var current = '';
|
|
289
|
-
for (var i = 0; i < parts.length; i++) {
|
|
290
|
-
var part = parts[i];
|
|
291
|
-
var next = current + part + delimiter;
|
|
292
|
-
if (next.length > maxLen) {
|
|
293
|
-
out.push(current);
|
|
294
|
-
current = part + delimiter;
|
|
295
|
-
} else {
|
|
296
|
-
current = next;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
return current;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* @constructor
|
|
305
|
-
*/
|
|
306
|
-
function Flag(name, defaultValue, description) {
|
|
307
|
-
this.name = name;
|
|
308
|
-
this.defaultValue = defaultValue;
|
|
309
|
-
this.description = description;
|
|
310
|
-
this.currentValue = null;
|
|
311
|
-
this.validator = null;
|
|
312
|
-
this.isSecret = false;
|
|
313
|
-
this.isSet = false;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
Flag.prototype.setDefault = function(defaultValue) {
|
|
318
|
-
this.defaultValue = defaultValue;
|
|
319
|
-
return this;
|
|
320
|
-
};
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
Flag.prototype.setDescription = function(description) {
|
|
324
|
-
this.description = description;
|
|
325
|
-
return this;
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
Flag.prototype.setValidator = function(validator) {
|
|
330
|
-
this.validator = validator;
|
|
331
|
-
return this;
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
Flag.prototype.setSecret = function(isSecret) {
|
|
336
|
-
this.isSecret = isSecret;
|
|
337
|
-
return this;
|
|
338
|
-
};
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
Flag.prototype.set = function(input) {
|
|
342
|
-
if (this.isSet) {
|
|
343
|
-
throw Error('Flag already set');
|
|
344
|
-
}
|
|
345
|
-
if (this.validator) this.validator.call(null, input);
|
|
346
|
-
this.currentValue = this.parseInput(input);
|
|
347
|
-
this.isSet = true;
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
Flag.prototype.get = function() {
|
|
352
|
-
return this.isSet ? this.currentValue : this.defaultValue;
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
Flag.prototype.parseInput = function(inp) {
|
|
357
|
-
return inp;
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
Flag.prototype.toHelpString = function() {
|
|
362
|
-
return wrapText(' --' + this.name + ': ' + this.description, 70) + '\n' +
|
|
363
|
-
' (default: ' + JSON.stringify(this.defaultValue) + ')';
|
|
364
|
-
};
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* @constructor
|
|
369
|
-
*/
|
|
370
|
-
function BooleanFlag() {
|
|
371
|
-
BooleanFlag.super_.apply(this, arguments);
|
|
372
|
-
}
|
|
373
|
-
util.inherits(BooleanFlag, Flag);
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
BooleanFlag.prototype.parseInput = function(inp) {
|
|
377
|
-
if (inp === null) {
|
|
378
|
-
return true;
|
|
379
|
-
}
|
|
380
|
-
inp = inp.toLowerCase();
|
|
381
|
-
if (inp === '1' || inp === 'true' || inp == 't') {
|
|
382
|
-
return true;
|
|
383
|
-
} else if (inp === '0' || inp === 'false' || inp == 'f') {
|
|
384
|
-
return false;
|
|
385
|
-
} else {
|
|
386
|
-
throw Error('Invalid Boolean flag "' + inp + '"')
|
|
387
|
-
}
|
|
388
|
-
};
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
BooleanFlag.prototype.get = function() {
|
|
392
|
-
return !!BooleanFlag.super_.prototype.get.call(this);
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
BooleanFlag.prototype.toHelpString = function() {
|
|
397
|
-
return wrapText(' --[no]' + this.name + ': ' + this.description, 70) + '\n' +
|
|
398
|
-
' (default: ' + JSON.stringify(this.defaultValue) + ')';
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* @constructor
|
|
405
|
-
*/
|
|
406
|
-
function IntegerFlag() {
|
|
407
|
-
IntegerFlag.super_.apply(this, arguments);
|
|
408
|
-
}
|
|
409
|
-
util.inherits(IntegerFlag, Flag);
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
IntegerFlag.prototype.parseInput = function(inp) {
|
|
413
|
-
if (isNaN(inp) || parseInt(inp, 10) !== Number(inp)) {
|
|
414
|
-
throw Error('Invalid Integer flag "' + inp + '"');
|
|
415
|
-
}
|
|
416
|
-
return Number(inp);
|
|
417
|
-
};
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
IntegerFlag.prototype.toHelpString = function() {
|
|
421
|
-
return IntegerFlag.super_.prototype.toHelpString.call(this) +
|
|
422
|
-
'\n (an integer)';
|
|
423
|
-
};
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
/**
|
|
427
|
-
* @constructor
|
|
428
|
-
*/
|
|
429
|
-
function NumberFlag() {
|
|
430
|
-
NumberFlag.super_.apply(this, arguments);
|
|
431
|
-
}
|
|
432
|
-
util.inherits(NumberFlag, Flag);
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
NumberFlag.prototype.parseInput = function(inp) {
|
|
436
|
-
if (isNaN(inp)) {
|
|
437
|
-
throw Error('Invalid Number flag "' + inp + '"');
|
|
438
|
-
}
|
|
439
|
-
return Number(inp);
|
|
440
|
-
};
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
NumberFlag.prototype.toHelpString = function() {
|
|
444
|
-
return NumberFlag.super_.prototype.toHelpString.call(this) +
|
|
445
|
-
'\n (a number)';
|
|
446
|
-
};
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
* @constructor
|
|
452
|
-
*/
|
|
453
|
-
function StringListFlag() {
|
|
454
|
-
StringListFlag.super_.apply(this, arguments);
|
|
455
|
-
}
|
|
456
|
-
util.inherits(StringListFlag, Flag);
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
StringListFlag.prototype.parseInput = function(inp) {
|
|
460
|
-
return inp.split(',');
|
|
461
|
-
};
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* @constructor
|
|
467
|
-
*/
|
|
468
|
-
function MultiStringFlag() {
|
|
469
|
-
MultiStringFlag.super_.apply(this, arguments);
|
|
470
|
-
this.currentValue = [];
|
|
471
|
-
}
|
|
472
|
-
util.inherits(MultiStringFlag, Flag);
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
MultiStringFlag.prototype.set = function(input) {
|
|
476
|
-
if (this.validator) this.validator.call(null, input);
|
|
477
|
-
this.currentValue.push(input);
|
|
478
|
-
this.isSet = true;
|
|
479
|
-
};
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
// Internal flags
|
|
484
|
-
//=================
|
|
485
|
-
|
|
486
|
-
function registerInternalFlags() {
|
|
487
|
-
exports.defineBoolean('help').
|
|
488
|
-
setDescription('Shows this help text.').
|
|
489
|
-
setSecret(true);
|
|
490
|
-
// TODO: --flagsfile
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
if (registerFlags) {
|
|
494
|
-
registerInternalFlags();
|
|
495
|
-
}
|
package/lib/flags_test.js
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
var flags = require('flags');
|
|
3
|
-
flags.exitOnError = false;
|
|
4
|
-
|
|
5
|
-
exports.testGlobalFlagsObject = function(test) {
|
|
6
|
-
flags.defineString('one', '111');
|
|
7
|
-
flags.defineString('two', '222');
|
|
8
|
-
test.deepEqual(global.GLOBAL_FLAGS, flags.FLAGS);
|
|
9
|
-
test.done();
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
exports.testStringFlagParsing = function(test) {
|
|
13
|
-
flags.reset();
|
|
14
|
-
flags.defineString('one', '111');
|
|
15
|
-
flags.defineString('two', '222');
|
|
16
|
-
flags.defineString('three', '333');
|
|
17
|
-
flags.parse(['--one', '--two=dos']);
|
|
18
|
-
|
|
19
|
-
test.strictEqual(null, flags.get('one'));
|
|
20
|
-
test.strictEqual('dos', flags.get('two'));
|
|
21
|
-
test.strictEqual('333', flags.get('three'));
|
|
22
|
-
|
|
23
|
-
test.ok(flags.FLAGS.one.isSet === true);
|
|
24
|
-
test.ok(flags.FLAGS.two.isSet === true);
|
|
25
|
-
test.ok(flags.FLAGS.three.isSet === false);
|
|
26
|
-
|
|
27
|
-
test.done();
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
exports.testStringFlagParsingWithSpaces = function(test) {
|
|
31
|
-
flags.reset();
|
|
32
|
-
flags.defineString('one', '111');
|
|
33
|
-
flags.defineString('two', '222');
|
|
34
|
-
flags.parse(['--one', 'aaa', '--two', 'bbb']);
|
|
35
|
-
test.strictEqual('aaa', flags.get('one'));
|
|
36
|
-
test.strictEqual('bbb', flags.get('two'));
|
|
37
|
-
test.done();
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
exports.testIntegerFlagParsing = function(test) {
|
|
41
|
-
flags.reset();
|
|
42
|
-
flags.defineInteger('one', 11);
|
|
43
|
-
flags.defineInteger('two', 22);
|
|
44
|
-
flags.parse(['--one=111']);
|
|
45
|
-
test.strictEqual(111, flags.get('one'));
|
|
46
|
-
test.strictEqual(22, flags.get('two'));
|
|
47
|
-
test.done();
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
exports.testIntegerFlagParsing_stringInput = function(test) {
|
|
51
|
-
flags.reset();
|
|
52
|
-
flags.defineInteger('one', 11);
|
|
53
|
-
test.throws(function() {
|
|
54
|
-
flags.parse(['--one=xxx']);
|
|
55
|
-
});
|
|
56
|
-
test.done();
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
exports.testIntegerFlagParsing_nonIntInput = function(test) {
|
|
60
|
-
flags.reset();
|
|
61
|
-
flags.defineInteger('one', 11);
|
|
62
|
-
test.throws(function() {
|
|
63
|
-
flags.parse(['--one=1.123']);
|
|
64
|
-
});
|
|
65
|
-
test.done();
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
exports.testNumberFlagParsing = function(test) {
|
|
69
|
-
flags.reset();
|
|
70
|
-
flags.defineNumber('one', 1.1);
|
|
71
|
-
flags.defineNumber('two', 2.2);
|
|
72
|
-
flags.parse(['--one=1.234']);
|
|
73
|
-
test.strictEqual(1.234, flags.get('one'));
|
|
74
|
-
test.strictEqual(2.2, flags.get('two'));
|
|
75
|
-
test.done();
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
exports.testNumberFlagParsing_stringInput = function(test) {
|
|
79
|
-
flags.reset();
|
|
80
|
-
flags.defineNumber('one', 1.1);
|
|
81
|
-
test.throws(function() {
|
|
82
|
-
flags.parse(['--one=xxx']);
|
|
83
|
-
});
|
|
84
|
-
test.done();
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
exports.testBooleanFlagParsing = function(test) {
|
|
88
|
-
flags.reset();
|
|
89
|
-
flags.defineBoolean('a');
|
|
90
|
-
flags.defineBoolean('b', false);
|
|
91
|
-
flags.defineBoolean('c').setDefault(false);
|
|
92
|
-
flags.defineBoolean('d', false);
|
|
93
|
-
flags.defineBoolean('e').setDefault(true);
|
|
94
|
-
flags.defineBoolean('f', true);
|
|
95
|
-
flags.defineBoolean('g', true);
|
|
96
|
-
flags.defineBoolean('h', true);
|
|
97
|
-
flags.defineBoolean('i');
|
|
98
|
-
flags.parse(['--a', '--b=true', '--c=t', '--d=1', '--noe', '--f=false',
|
|
99
|
-
'--g=0', '--h=f']);
|
|
100
|
-
test.strictEqual(true, flags.get('a'));
|
|
101
|
-
test.strictEqual(true, flags.get('b'));
|
|
102
|
-
test.strictEqual(true, flags.get('c'));
|
|
103
|
-
test.strictEqual(true, flags.get('d'));
|
|
104
|
-
test.strictEqual(false, flags.get('e'));
|
|
105
|
-
test.strictEqual(false, flags.get('f'));
|
|
106
|
-
test.strictEqual(false, flags.get('g'));
|
|
107
|
-
test.strictEqual(false, flags.get('h'));
|
|
108
|
-
test.strictEqual(false, flags.get('i'));
|
|
109
|
-
test.done();
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
exports.testBooleanFlagParsing_badInput = function(test) {
|
|
113
|
-
flags.reset();
|
|
114
|
-
flags.defineBoolean('a', false);
|
|
115
|
-
test.throws(function() {
|
|
116
|
-
flags.parse(['--a=xxx']);
|
|
117
|
-
});
|
|
118
|
-
test.done();
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
exports.testStringListFlagParsing = function(test) {
|
|
122
|
-
flags.reset();
|
|
123
|
-
flags.defineStringList('one', []);
|
|
124
|
-
flags.parse(['--one=a,b,c,d']);
|
|
125
|
-
test.deepEqual(['a', 'b', 'c', 'd'], flags.get('one'));
|
|
126
|
-
test.done();
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
exports.testMultiStringFlagParsing = function(test) {
|
|
130
|
-
flags.reset();
|
|
131
|
-
flags.defineMultiString('one', []);
|
|
132
|
-
flags.parse(['--one=a', '--one=b', '--one=c', '--one=d']);
|
|
133
|
-
test.deepEqual(['a', 'b', 'c', 'd'], flags.get('one'));
|
|
134
|
-
test.done();
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
exports.testUnrecognizedFlags = function(test) {
|
|
138
|
-
flags.reset();
|
|
139
|
-
test.throws(function() {
|
|
140
|
-
flags.parse(['--one']);
|
|
141
|
-
});
|
|
142
|
-
test.done();
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
exports.testDuplicateFlags = function(test) {
|
|
146
|
-
flags.reset();
|
|
147
|
-
flags.defineString('one', '');
|
|
148
|
-
test.throws(function() {
|
|
149
|
-
flags.defineString('one', '');
|
|
150
|
-
});
|
|
151
|
-
test.done();
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
exports.testThrowIfDefineAfterParse = function(test) {
|
|
155
|
-
flags.reset();
|
|
156
|
-
flags.parse([]);
|
|
157
|
-
test.throws(function() {
|
|
158
|
-
flags.defineString('one', '');
|
|
159
|
-
});
|
|
160
|
-
test.done();
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
exports.testValidators = function(test) {
|
|
164
|
-
function setUp() {
|
|
165
|
-
flags.reset();
|
|
166
|
-
flags.defineString('one').setValidator(function(inp) {
|
|
167
|
-
if (inp.substr(0, 3) != 'xxx') throw Error('Bad Input');
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
setUp();
|
|
171
|
-
flags.parse(['--one=xxxyyy']);
|
|
172
|
-
setUp();
|
|
173
|
-
test.throws(function() {
|
|
174
|
-
flags.parse(['--one=yyyxxx']);
|
|
175
|
-
});
|
|
176
|
-
test.done();
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
exports.testBreakFlag = function(test) {
|
|
180
|
-
flags.reset();
|
|
181
|
-
flags.defineString('one', '');
|
|
182
|
-
flags.defineString('two', '');
|
|
183
|
-
var rv = flags.parse(['--one=2', '--two=3', '--', 'something', 'else']);
|
|
184
|
-
test.deepEqual(['something', 'else'], rv);
|
|
185
|
-
test.done();
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
exports.testBreakFlag_nothingElse = function(test) {
|
|
189
|
-
flags.reset();
|
|
190
|
-
flags.defineString('one', '');
|
|
191
|
-
flags.defineString('two', '');
|
|
192
|
-
var rv = flags.parse(['--one=2', '--two=3', '--']);
|
|
193
|
-
test.deepEqual([], rv);
|
|
194
|
-
test.done();
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
exports.testReturnValue = function(test) {
|
|
198
|
-
flags.reset();
|
|
199
|
-
flags.defineString('one', '');
|
|
200
|
-
flags.defineString('two', '');
|
|
201
|
-
var rv = flags.parse(['--one=2', '--two=3']);
|
|
202
|
-
test.deepEqual([], rv);
|
|
203
|
-
test.done();
|
|
204
|
-
};
|