@trenskow/arguments-parser 0.3.140 → 0.3.142

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.
@@ -59,8 +59,6 @@ const bash = (items) => {
59
59
  process.stdout.write('COMPREPLY+=($(compgen -f -- "${COMP_WORDS[COMP_CWORD]}"))\n');
60
60
  }
61
61
 
62
- process.exit(0);
63
-
64
62
  };
65
63
 
66
64
  bash.script = ({
@@ -105,24 +105,30 @@ const autocomplete = () => {
105
105
  }
106
106
  });
107
107
 
108
- const output = (items) => writer(
109
- filter(items)
110
- .reduce((grouped, item) => {
111
-
112
- grouped[item.type] = grouped[item.type] || [];
113
-
114
- switch (item.type) {
115
- case 'option':
116
- // fall through
117
- case 'command':
118
- grouped[item.type].push(item);
119
- break;
120
- default:
121
- }
108
+ const output = (items) => {
109
+
110
+ writer(
111
+ filter(items)
112
+ .reduce((grouped, item) => {
113
+
114
+ grouped[item.type] = grouped[item.type] || [];
115
+
116
+ switch (item.type) {
117
+ case 'option':
118
+ // fall through
119
+ case 'command':
120
+ grouped[item.type].push(item);
121
+ break;
122
+ default:
123
+ }
122
124
 
123
- return grouped;
125
+ return grouped;
124
126
 
125
- }, {}));
127
+ }, {}));
128
+
129
+ process.exit(0);
130
+
131
+ };
126
132
 
127
133
  const result = {
128
134
  get base() {
@@ -196,15 +202,17 @@ const autocomplete = () => {
196
202
  return (async () => {
197
203
 
198
204
  const {
199
- variadic,
200
- autocomplete = () => [{
201
- type: 'files'
202
- }]
205
+ variadic
203
206
  } = options.validationOptions;
204
207
 
205
- const nonOptions = args
208
+ if (args
206
209
  .slice(0, autocompleteIndex)
207
- .some((arg) => arg === '--');
210
+ .some((arg) => arg === '--')
211
+ ) {
212
+ output([{
213
+ type: 'files'
214
+ }]);
215
+ }
208
216
 
209
217
  const allKeyPaths = keyPaths(options.schema)
210
218
  .all()
@@ -214,107 +222,101 @@ const autocomplete = () => {
214
222
  let schemas = {};
215
223
  let spent = [];
216
224
 
217
- const complete = async (current = args[autocompleteIndex] || '') => {
225
+ for (idx = 0; idx < autocompleteIndex || idx < args.length; idx++) {
218
226
 
219
- if (nonOptions) {
220
- return [{
221
- type: 'files'
222
- }];
223
- }
227
+ if (!schemas[idx]) {
228
+
229
+ if (!(args[idx] || '').startsWith('-')) {
230
+ continue;
231
+ }
224
232
 
225
- let key = current;
233
+ const [keyPath, schema] = allKeyPaths
234
+ .filter((keyPath) => !spent.includes(keyPath))
235
+ .map((keyPath) => [keyPath, keyPaths(options.schema).get(keyPath)])
236
+ .filter(([keyPath, schema]) => keyPath === args[idx] || `--${caseit(keyPath, 'kebab')}` === args[idx] || (schema.short && `-${schema.short}` === args[idx]))[0] || [];
237
+
238
+ if (schema && schema.type !== Array) {
239
+ spent.push(keyPath);
240
+ }
241
+
242
+ if (schema && schema.type !== Boolean) {
243
+ schemas[idx + 1] = schema;
244
+ }
226
245
 
227
- while (key.startsWith('-')) {
228
- key = key.slice(1);
229
246
  }
230
247
 
231
- key = caseit(key);
248
+ }
232
249
 
233
- if (schemas[autocompleteIndex]) {
250
+ let current = args[autocompleteIndex] || '';
234
251
 
235
- if (schemas[autocompleteIndex].enum) {
236
- return Object.keys(schemas[autocompleteIndex].enum)
237
- .map((item) => ({
238
- type: 'option',
239
- argument: caseit(item, 'kebab')
240
- }));
241
- }
252
+ while (current.startsWith('-')) {
253
+ current = current.slice(1);
254
+ }
242
255
 
243
- if (schemas[autocompleteIndex].type === String && !schemas[autocompleteIndex].autocomplete) {
244
- return [{
245
- type: 'files'
246
- }];
247
- }
256
+ if (schemas[autocompleteIndex]) {
248
257
 
249
- return await schemas[autocompleteIndex].autocomplete?.(current) || [];
258
+ if (schemas[autocompleteIndex].enum) {
259
+ output(Object.keys(schemas[autocompleteIndex].enum)
260
+ .map((item) => ({
261
+ type: 'option',
262
+ argument: caseit(item, 'kebab')
263
+ })));
264
+ }
250
265
 
251
- } else {
266
+ if (schemas[autocompleteIndex].type === String && !schemas[autocompleteIndex].autocomplete) {
267
+ output([{
268
+ type: 'files'
269
+ }]);
270
+ }
252
271
 
253
- let items = [];
272
+ let autocomplete = [];
254
273
 
255
- if (variadic === 'allow') {
256
- items = items.concat(
257
- await autocomplete?.(current) || []);
274
+ if (typeof schemas[autocompleteIndex].autocomplete !== 'undefined') {
275
+ autocomplete = schemas[autocompleteIndex].autocomplete;
276
+ if (typeof autocomplete === 'function') {
277
+ autocomplete = await autocomplete(current);
258
278
  }
279
+ }
259
280
 
260
- if (!current.startsWith('--')) {
281
+ output(
282
+ autocomplete);
261
283
 
262
- items = items.concat(
263
- allKeyPaths
264
- .filter((keyPath) => keyPaths(options.schema).get(keyPath).short)
265
- .filter((keyPath) => !spent.includes(keyPath))
266
- .map((keyPath) => ({
267
- type: 'option',
268
- argument: `-${caseit(keyPaths(options.schema).get(keyPath).short, 'kebab')}`,
269
- description: keyPaths(options.schema).get(keyPath).description
270
- })));
284
+ } else {
271
285
 
272
- }
286
+ let items = [];
287
+
288
+ if (variadic === 'allow') {
289
+ items = items.concat(
290
+ await autocomplete?.(current) || []);
291
+ }
273
292
 
293
+ if (!current.startsWith('--')) {
274
294
 
275
295
  items = items.concat(
276
296
  allKeyPaths
297
+ .filter((keyPath) => keyPaths(options.schema).get(keyPath).short)
277
298
  .filter((keyPath) => !spent.includes(keyPath))
278
299
  .map((keyPath) => ({
279
300
  type: 'option',
280
- argument: `--${caseit(keyPath, 'kebab')}`,
301
+ argument: `-${caseit(keyPaths(options.schema).get(keyPath).short, 'kebab')}`,
281
302
  description: keyPaths(options.schema).get(keyPath).description
282
303
  })));
283
304
 
284
- return items;
285
-
286
305
  }
287
306
 
288
- };
289
-
290
- for (idx = 0; idx < autocompleteIndex || idx < args.length; idx++) {
291
-
292
- if (!schemas[idx]) {
293
-
294
- if (nonOptions || !(args[idx] || '').startsWith('-')) {
295
- continue;
296
- }
297
-
298
- const [keyPath, schema] = allKeyPaths
307
+ items = items.concat(
308
+ allKeyPaths
299
309
  .filter((keyPath) => !spent.includes(keyPath))
300
- .map((keyPath) => [keyPath, keyPaths(options.schema).get(keyPath)])
301
- .filter(([keyPath, schema]) => keyPath === args[idx] || `--${caseit(keyPath, 'kebab')}` === args[idx] || (schema.short && `-${schema.short}` === args[idx]))[0] || [];
302
-
303
- if (schema && schema.type !== Array) {
304
- spent.push(keyPath);
305
- }
306
-
307
- if (schema && schema.type !== Boolean) {
308
- schemas[idx + 1] = schema;
309
- }
310
+ .map((keyPath) => ({
311
+ type: 'option',
312
+ argument: `--${caseit(keyPath, 'kebab')}`,
313
+ description: keyPaths(options.schema).get(keyPath).description
314
+ })));
310
315
 
311
- }
316
+ output(items);
312
317
 
313
318
  }
314
319
 
315
- return output(
316
- await complete());
317
-
318
320
  })().then(...resultArguments);
319
321
  }
320
322
  };
@@ -46,8 +46,6 @@ const zsh = (items) => {
46
46
 
47
47
  });
48
48
 
49
- process.exit(0);
50
-
51
49
  };
52
50
 
53
51
  zsh.script = ({
@@ -15,9 +15,41 @@ export default () => ({
15
15
  validate: (data) => data,
16
16
  formalize: (data) => {
17
17
 
18
- if (!(Array.isArray(data) && data.every((data) => typeof data === 'string')) && typeof data !== 'function') {
19
- throw new Error('Must be a string or a function.');
20
- }
18
+ if (!Array.isArray(data)) data = [data];
19
+
20
+ data
21
+ .forEach((item) => {
22
+
23
+ if (typeof item === 'string') {
24
+ item = {
25
+ type: 'option',
26
+ argument: item
27
+ };
28
+ }
29
+
30
+ if (typeof item?.type !== 'string') {
31
+ throw new Error('Autocomplete item must have a type.');
32
+ }
33
+
34
+ if (!['command', 'option', 'files'].includes(item.type)) {
35
+ throw new Error('Autocomplete item type must be one of: command, option, files.');
36
+ }
37
+
38
+ if (item.type !== 'files') {
39
+
40
+ if (typeof item?.argument !== 'string') {
41
+ throw new Error('Autocomplete item must have an argument.');
42
+ }
43
+
44
+ if (typeof item?.description !== 'undefined' && typeof item?.description !== 'string') {
45
+ throw new Error('Autocomplete item description must be a string.');
46
+ }
47
+
48
+ }
49
+
50
+ return item;
51
+
52
+ });
21
53
 
22
54
  return data;
23
55
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/arguments-parser",
3
- "version": "0.3.140",
3
+ "version": "0.3.142",
4
4
  "description": "Yet another arguments parser.",
5
5
  "main": "index.js",
6
6
  "type": "module",