argue-cli 1.1.1 → 2.0.0-1

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