argue-cli 1.1.0 → 2.0.0-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/lib/lib.js DELETED
@@ -1,416 +0,0 @@
1
- "use strict";
2
-
3
- var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.setArguments = setArguments;
9
- exports.expect = expect;
10
- exports.read = read;
11
- exports.end = end;
12
- exports.strictOptions = strictOptions;
13
- exports.strictOptionsEqual = strictOptionsEqual;
14
- exports.options = options;
15
- exports.optionsEqual = optionsEqual;
16
-
17
- var argv = process.argv.slice(2, process.argv.length);
18
-
19
- function findName(name, names) {
20
-
21
- var findedName = false,
22
- array = false;
23
-
24
- names.some(function (fullName) {
25
-
26
- var keys, shirtName;
27
-
28
- if (typeof fullName == "string") {
29
-
30
- if (fullName == name) {
31
- findedName = fullName;
32
- return true;
33
- }
34
-
35
- return false;
36
- }
37
-
38
- var proto = Object.getPrototypeOf(fullName);
39
-
40
- if (proto == Object.prototype) {
41
-
42
- keys = fullName;
43
- fullName = Object.keys(keys)[0];
44
- shirtName = keys[fullName];
45
-
46
- if (fullName == name || shirtName == name) {
47
- findedName = fullName;
48
- return true;
49
- }
50
-
51
- return false;
52
- }
53
-
54
- if (proto == Array.prototype) {
55
- var _fullName = fullName;
56
-
57
- var _fullName2 = _slicedToArray(_fullName, 2);
58
-
59
- fullName = _fullName2[0];
60
- shirtName = _fullName2[1];
61
-
62
- if (fullName == name || shirtName == name) {
63
- array = true;
64
- findedName = fullName;
65
- return true;
66
- }
67
-
68
- return false;
69
- }
70
-
71
- return false;
72
- });
73
-
74
- return findedName ? { name: findedName, isArray: array } : false;
75
- }
76
-
77
- function setArguments() {
78
- argv.splice(0, argv.length);
79
- argv.push.apply(argv, arguments);
80
- }
81
-
82
- /**
83
- * Strict expectation one of given commands.
84
- *
85
- * command-line-app install
86
- *
87
- * expect(
88
- * {"install": "i"}, - fullname and shirtname
89
- * ["update", "u"], - also fullname and shirtname
90
- * "info" - only one variant of name
91
- * )
92
- *
93
- * @param {...Object} names
94
- * @return {String} fullname
95
- */
96
- function expect() {
97
-
98
- if (!argv.length) {
99
- throw new Error("Unexpected end of arguments.");
100
- }
101
-
102
- for (var _len = arguments.length, names = Array(_len), _key = 0; _key < _len; _key++) {
103
- names[_key] = arguments[_key];
104
- }
105
-
106
- var sourceKey = argv.shift(),
107
- argument = findName(sourceKey, names);
108
-
109
- if (!argument) {
110
- throw new Error("Unexpected argument \"" + sourceKey + "\".");
111
- }
112
-
113
- return argument.name;
114
- }
115
-
116
- /**
117
- * Strict reading of argument.
118
- *
119
- * command-line-app some-value
120
- *
121
- * @return {String} argument
122
- */
123
- function read() {
124
-
125
- if (!argv.length) {
126
- throw new Error("Unexpected end of arguments.");
127
- }
128
-
129
- return argv.shift();
130
- }
131
-
132
- /**
133
- * Strict expectation of end.
134
- *
135
- */
136
- function end() {
137
-
138
- if (argv.length) {
139
- throw new Error("Unexpected argument \"" + argv[0] + "\".");
140
- }
141
- }
142
-
143
- /**
144
- * Strict reading of flags and options.
145
- *
146
- * command-line-app --output test -p es2015,react --verbose
147
- *
148
- * strictOptions([
149
- * ["another"] - for flags array is same as object notation
150
- * "verbose" - only one variant of name
151
- * ], [
152
- * {"output": "o"}, - fullname and shirtname
153
- * ["plugins", "p"] - fullname and shirtname for array
154
- * ])
155
- *
156
- * @param {...Object} flagsNames
157
- * @param {...Object} optionsNames
158
- * @return {Object} fullname-value pairs
159
- */
160
- function strictOptions(flagsNames, optionsNames) {
161
-
162
- if (!argv.length) {
163
- return {};
164
- }
165
-
166
- var options = {},
167
- argument = argv[0];
168
-
169
- while (argv.length && (argument.indexOf("--") == 0 || argument.indexOf("-") == 0 && argument.length == 2)) {
170
-
171
- var sourceKey = argv.shift().replace(/^(--|-)/, ""),
172
- flagKey = findName(sourceKey, flagsNames),
173
- optionKey = findName(sourceKey, optionsNames);
174
-
175
- if (!flagKey && !optionKey) {
176
- throw new Error("Unexpected key \"" + sourceKey + "\".");
177
- }
178
-
179
- if (!flagKey && optionKey && !argv.length) {
180
- throw new Error("Unexpected end of arguments.");
181
- }
182
-
183
- var value = argv[0];
184
-
185
- if (optionKey && value.indexOf("--") != 0 && value.indexOf("-") != 0 && value.length != 2) {
186
-
187
- argv.shift();
188
-
189
- if (optionKey.isArray) {
190
- value = value.split(",").map(function (element) {
191
- return element.replace(/^['"]|["']$/g, "");
192
- });
193
- } else {
194
- value = value.replace(/^['"]|["']$/g, "");
195
- }
196
-
197
- options[optionKey.name] = value;
198
- } else if (flagKey) {
199
-
200
- options[flagKey.name] = true;
201
- } else {
202
- throw new Error("Unexpected key \"" + value + "\".");
203
- }
204
-
205
- argument = argv[0];
206
- }
207
-
208
- return options;
209
- }
210
-
211
- /**
212
- * Strict reading of options with equal sign.
213
- * If option is provided without value it will interpreted as `true`.
214
- *
215
- * command-line-app --output=test -p=es2015,react --verbose
216
- *
217
- * strictOptionsEqual(
218
- * {"output": "o"}, - fullname and shirtname
219
- * ["plugins", "p"], - fullname and shirtname for array
220
- * "verbose" - only one variant of name
221
- * )
222
- *
223
- * @param {...Object} names
224
- * @return {Object} fullname-value pairs
225
- */
226
- function strictOptionsEqual() {
227
-
228
- if (!argv.length) {
229
- return {};
230
- }
231
-
232
- var options = {},
233
- argument = argv[0];
234
-
235
- for (var _len2 = arguments.length, names = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
236
- names[_key2] = arguments[_key2];
237
- }
238
-
239
- while (argv.length && (argument.indexOf("--") == 0 || argument.indexOf("-") == 0)) {
240
- var _argv$shift$replace$s = argv.shift().replace(/^(--|-)/, "").split("=");
241
-
242
- var _argv$shift$replace$s2 = _slicedToArray(_argv$shift$replace$s, 2);
243
-
244
- var sourceKey = _argv$shift$replace$s2[0];
245
- var value = _argv$shift$replace$s2[1];
246
- var key = findName(sourceKey, names);
247
-
248
- if (!key) {
249
- throw new Error("Unexpected key \"" + sourceKey + "\".");
250
- }
251
-
252
- if (typeof value == "undefined") {
253
- value = true;
254
- } else if (key.isArray) {
255
- value = value.split(",").map(function (element) {
256
- return element.replace(/^['"]|["']$/g, "");
257
- });
258
- } else {
259
- value = value.replace(/^['"]|["']$/g, "");
260
- }
261
-
262
- options[key.name] = value;
263
-
264
- argument = argv[0];
265
- }
266
-
267
- return options;
268
- }
269
-
270
- /**
271
- * Unlimited reading of flags and options.
272
- *
273
- * command-line-app --output test install -p es2015,react babel --verbose
274
- *
275
- * options([
276
- * ["another"] - for flags array is same as object notation
277
- * "verbose" - only one variant of name
278
- * ], [
279
- * {"output": "o"}, - fullname and shirtname
280
- * ["plugins", "p"] - fullname and shirtname for array
281
- * ])
282
- *
283
- * @param {...Object} flagsNames
284
- * @param {...Object} optionsNames
285
- * @return {Object} fullname-value pairs
286
- */
287
- function options(flagsNames, optionsNames) {
288
-
289
- if (!argv.length) {
290
- return {};
291
- }
292
-
293
- var options = {},
294
- argvc = argv.slice(),
295
- argc = argvc.length,
296
- remove = [];
297
-
298
- for (var i = 0, argument = argvc[i]; i < argc; argument = argvc[++i]) {
299
-
300
- if (argument.indexOf("--") != 0 && (argument.indexOf("-") != 0 || argument.length != 2)) {
301
- continue;
302
- }
303
-
304
- var sourceKey = argument.replace(/^(--|-)/, ""),
305
- flagKey = findName(sourceKey, flagsNames),
306
- optionKey = findName(sourceKey, optionsNames);
307
-
308
- if (!flagKey && !optionKey) {
309
- throw new Error("Unexpected key \"" + sourceKey + "\".");
310
- }
311
-
312
- if (!flagKey && optionKey && i == argc - 1) {
313
- throw new Error("Unexpected end of arguments.");
314
- }
315
-
316
- var value = argvc[i + 1];
317
-
318
- if (optionKey && value.indexOf("--") != 0 && value.indexOf("-") != 0 && value.length != 2) {
319
-
320
- remove.unshift(i++);
321
-
322
- if (optionKey.isArray) {
323
- value = value.split(",").map(function (element) {
324
- return element.replace(/^['"]|["']$/g, "");
325
- });
326
- } else {
327
- value = value.replace(/^['"]|["']$/g, "");
328
- }
329
-
330
- options[optionKey.name] = value;
331
- } else if (flagKey) {
332
-
333
- options[flagKey.name] = true;
334
- } else {
335
- throw new Error("Unexpected key \"" + value + "\".");
336
- }
337
-
338
- remove.unshift(i);
339
- }
340
-
341
- remove.forEach(function (index) {
342
- return argv.splice(index, 1);
343
- });
344
-
345
- return options;
346
- }
347
-
348
- /**
349
- * Unlimited reading of with equal sign.
350
- * If option is provided without value it will interpreted as `true`.
351
- *
352
- * command-line-app --output=test install -p=es2015,react babel --verbose
353
- *
354
- * optionsEqual(
355
- * {"output": "o"}, - fullname and shirtname
356
- * ["plugins", "p"], - fullname and shirtname for array
357
- * "verbose" - only one variant of name
358
- * )
359
- *
360
- * @param {...Object} names
361
- * @return {Object} fullname-value pairs
362
- */
363
- function optionsEqual() {
364
-
365
- if (!argv.length) {
366
- return {};
367
- }
368
-
369
- var options = {},
370
- argvc = argv.slice(),
371
- argc = argvc.length,
372
- remove = [];
373
-
374
- for (var _len3 = arguments.length, names = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
375
- names[_key3] = arguments[_key3];
376
- }
377
-
378
- for (var i = 0, argument = argvc[i]; i < argc; argument = argvc[++i]) {
379
-
380
- if (argument.indexOf("-") != 0) {
381
- continue;
382
- }
383
-
384
- var _argument$replace$spl = argument.replace(/^(--|-)/, "").split("=");
385
-
386
- var _argument$replace$spl2 = _slicedToArray(_argument$replace$spl, 2);
387
-
388
- var sourceKey = _argument$replace$spl2[0];
389
- var value = _argument$replace$spl2[1];
390
- var key = findName(sourceKey, names);
391
-
392
- remove.unshift(i);
393
-
394
- if (!key) {
395
- throw new Error("Unexpected key \"" + sourceKey + "\".");
396
- }
397
-
398
- if (typeof value == "undefined") {
399
- value = true;
400
- } else if (key.isArray) {
401
- value = value.split(",").map(function (element) {
402
- return element.replace(/^['"]|["']$/g, "");
403
- });
404
- } else {
405
- value = value.replace(/^['"]|["']$/g, "");
406
- }
407
-
408
- options[key.name] = value;
409
- }
410
-
411
- remove.forEach(function (index) {
412
- return argv.splice(index, 1);
413
- });
414
-
415
- return options;
416
- }