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