@trenskow/arguments-parser 0.3.140 → 0.3.141

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() {
@@ -202,9 +208,14 @@ const autocomplete = () => {
202
208
  }]
203
209
  } = options.validationOptions;
204
210
 
205
- const nonOptions = args
211
+ if (args
206
212
  .slice(0, autocompleteIndex)
207
- .some((arg) => arg === '--');
213
+ .some((arg) => arg === '--')
214
+ ) {
215
+ output([{
216
+ type: 'files'
217
+ }]);
218
+ }
208
219
 
209
220
  const allKeyPaths = keyPaths(options.schema)
210
221
  .all()
@@ -214,107 +225,92 @@ const autocomplete = () => {
214
225
  let schemas = {};
215
226
  let spent = [];
216
227
 
217
- const complete = async (current = args[autocompleteIndex] || '') => {
218
-
219
- if (nonOptions) {
220
- return [{
221
- type: 'files'
222
- }];
223
- }
224
-
225
- let key = current;
228
+ for (idx = 0; idx < autocompleteIndex || idx < args.length; idx++) {
226
229
 
227
- while (key.startsWith('-')) {
228
- key = key.slice(1);
229
- }
230
+ if (!schemas[idx]) {
230
231
 
231
- key = caseit(key);
232
+ if (!(args[idx] || '').startsWith('-')) {
233
+ continue;
234
+ }
232
235
 
233
- if (schemas[autocompleteIndex]) {
236
+ const [keyPath, schema] = allKeyPaths
237
+ .filter((keyPath) => !spent.includes(keyPath))
238
+ .map((keyPath) => [keyPath, keyPaths(options.schema).get(keyPath)])
239
+ .filter(([keyPath, schema]) => keyPath === args[idx] || `--${caseit(keyPath, 'kebab')}` === args[idx] || (schema.short && `-${schema.short}` === args[idx]))[0] || [];
234
240
 
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
+ if (schema && schema.type !== Array) {
242
+ spent.push(keyPath);
241
243
  }
242
244
 
243
- if (schemas[autocompleteIndex].type === String && !schemas[autocompleteIndex].autocomplete) {
244
- return [{
245
- type: 'files'
246
- }];
245
+ if (schema && schema.type !== Boolean) {
246
+ schemas[idx + 1] = schema;
247
247
  }
248
248
 
249
- return await schemas[autocompleteIndex].autocomplete?.(current) || [];
249
+ }
250
+
251
+ }
250
252
 
251
- } else {
253
+ let current = args[autocompleteIndex] || '';
252
254
 
253
- let items = [];
255
+ while (current.startsWith('-')) {
256
+ current = current.slice(1);
257
+ }
254
258
 
255
- if (variadic === 'allow') {
256
- items = items.concat(
257
- await autocomplete?.(current) || []);
258
- }
259
+ if (schemas[autocompleteIndex]) {
259
260
 
260
- if (!current.startsWith('--')) {
261
+ if (schemas[autocompleteIndex].enum) {
262
+ output(Object.keys(schemas[autocompleteIndex].enum)
263
+ .map((item) => ({
264
+ type: 'option',
265
+ argument: caseit(item, 'kebab')
266
+ })));
267
+ }
261
268
 
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
- })));
269
+ if (schemas[autocompleteIndex].type === String && !schemas[autocompleteIndex].autocomplete) {
270
+ output([{
271
+ type: 'files'
272
+ }]);
273
+ }
271
274
 
272
- }
275
+ output(
276
+ await schemas[autocompleteIndex].autocomplete?.(current) || []);
277
+
278
+ } else {
279
+
280
+ let items = [];
281
+
282
+ if (variadic === 'allow') {
283
+ items = items.concat(
284
+ await autocomplete?.(current) || []);
285
+ }
273
286
 
287
+ if (!current.startsWith('--')) {
274
288
 
275
289
  items = items.concat(
276
290
  allKeyPaths
291
+ .filter((keyPath) => keyPaths(options.schema).get(keyPath).short)
277
292
  .filter((keyPath) => !spent.includes(keyPath))
278
293
  .map((keyPath) => ({
279
294
  type: 'option',
280
- argument: `--${caseit(keyPath, 'kebab')}`,
295
+ argument: `-${caseit(keyPaths(options.schema).get(keyPath).short, 'kebab')}`,
281
296
  description: keyPaths(options.schema).get(keyPath).description
282
297
  })));
283
298
 
284
- return items;
285
-
286
299
  }
287
300
 
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
301
+ items = items.concat(
302
+ allKeyPaths
299
303
  .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
- }
304
+ .map((keyPath) => ({
305
+ type: 'option',
306
+ argument: `--${caseit(keyPath, 'kebab')}`,
307
+ description: keyPaths(options.schema).get(keyPath).description
308
+ })));
310
309
 
311
- }
310
+ output(items);
312
311
 
313
312
  }
314
313
 
315
- return output(
316
- await complete());
317
-
318
314
  })().then(...resultArguments);
319
315
  }
320
316
  };
@@ -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 = ({
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.141",
4
4
  "description": "Yet another arguments parser.",
5
5
  "main": "index.js",
6
6
  "type": "module",