@trenskow/arguments-parser 0.3.139 → 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 = ({
@@ -36,53 +36,6 @@ const autocomplete = () => {
36
36
  process.exit(1);
37
37
  }
38
38
 
39
- const normalize = (items) => items
40
- .map((item) => {
41
-
42
- if (typeof item === 'string') {
43
- return {
44
- type: 'option',
45
- argument: item
46
- };
47
- }
48
-
49
- return item;
50
-
51
- });
52
-
53
- const filter = (items, current = '') => {
54
- return normalize(items)
55
- .filter((item) => {
56
- switch (item.type) {
57
- case 'option':
58
- // fall through
59
- case 'command':
60
- return item.argument !== current && item.argument.startsWith(current);
61
- default:
62
- return true;
63
- }
64
- });
65
- };
66
-
67
- const output = (items) => writer(
68
- normalize(items)
69
- .reduce((grouped, item) => {
70
-
71
- grouped[item.type] = grouped[item.type] || [];
72
-
73
- switch (item.type) {
74
- case 'option':
75
- // fall through
76
- case 'command':
77
- grouped[item.type].push(item);
78
- break;
79
- default:
80
- }
81
-
82
- return grouped;
83
-
84
- }, {}));
85
-
86
39
  const argumentsParser = (
87
40
  {
88
41
  args = process.argv.slice(3),
@@ -126,6 +79,57 @@ const autocomplete = () => {
126
79
 
127
80
  };
128
81
 
82
+ const normalize = (items) => items
83
+ .map((item) => {
84
+
85
+ if (typeof item === 'string') {
86
+ return {
87
+ type: 'option',
88
+ argument: item
89
+ };
90
+ }
91
+
92
+ return item;
93
+
94
+ });
95
+
96
+ const filter = (items) => normalize(items)
97
+ .filter((item) => {
98
+ switch (item.type) {
99
+ case 'option':
100
+ // fall through
101
+ case 'command':
102
+ return item.argument !== args[autocompleteIndex] && item.argument.startsWith(args[autocompleteIndex]);
103
+ default:
104
+ return true;
105
+ }
106
+ });
107
+
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
+ }
124
+
125
+ return grouped;
126
+
127
+ }, {}));
128
+
129
+ process.exit(0);
130
+
131
+ };
132
+
129
133
  const result = {
130
134
  get base() {
131
135
  return base;
@@ -137,7 +141,6 @@ const autocomplete = () => {
137
141
  if (autocompleteIndex === 0) {
138
142
 
139
143
  return output(Object.keys(commands)
140
- .filter((command) => command.startsWith(tool))
141
144
  .map((command) => ({
142
145
  type: 'command',
143
146
  argument: caseit(command, 'kebab'),
@@ -166,8 +169,7 @@ const autocomplete = () => {
166
169
 
167
170
  if (autocompleteIndex === 0) {
168
171
  return output(
169
- filter(
170
- await autocomplete?.(args[0] || '') || [], args[0]));
172
+ await autocomplete?.(args[0] || '') || [], args[0]);
171
173
  }
172
174
 
173
175
  parameters[identifier] = args[0];
@@ -206,9 +208,14 @@ const autocomplete = () => {
206
208
  }]
207
209
  } = options.validationOptions;
208
210
 
209
- const variadicOnly = args
211
+ if (args
210
212
  .slice(0, autocompleteIndex)
211
- .some((arg) => arg === '--');
213
+ .some((arg) => arg === '--')
214
+ ) {
215
+ output([{
216
+ type: 'files'
217
+ }]);
218
+ }
212
219
 
213
220
  const allKeyPaths = keyPaths(options.schema)
214
221
  .all()
@@ -218,114 +225,92 @@ const autocomplete = () => {
218
225
  let schemas = {};
219
226
  let spent = [];
220
227
 
221
- const complete = async (idx, current = '') => {
222
-
223
- let key = current;
228
+ for (idx = 0; idx < autocompleteIndex || idx < args.length; idx++) {
224
229
 
225
- while (key.startsWith('-')) {
226
- key = key.slice(1);
227
- }
230
+ if (!schemas[idx]) {
228
231
 
229
- key = caseit(key);
232
+ if (!(args[idx] || '').startsWith('-')) {
233
+ continue;
234
+ }
230
235
 
231
- if (!variadicOnly && schemas[idx]) {
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] || [];
232
240
 
233
- if (schemas[idx].enum) {
234
- return Object.keys(schemas[idx].enum)
235
- .filter((item) => key !== item && item.startsWith(current))
236
- .map((item) => ({
237
- type: 'option',
238
- argument: caseit(item, 'kebab')
239
- }));
241
+ if (schema && schema.type !== Array) {
242
+ spent.push(keyPath);
240
243
  }
241
244
 
242
- if (schemas[idx].type === String && !schemas[idx].autocomplete) {
243
- return [{
244
- type: 'files'
245
- }];
245
+ if (schema && schema.type !== Boolean) {
246
+ schemas[idx + 1] = schema;
246
247
  }
247
248
 
248
- return filter(await schemas[idx].autocomplete?.(current) || [], current);
249
-
250
- } else {
249
+ }
251
250
 
252
- let items = [];
251
+ }
253
252
 
254
- if (variadic === 'allow') {
255
- items = items.concat(
256
- filter(await autocomplete?.(current) || [], current));
257
- }
253
+ let current = args[autocompleteIndex] || '';
258
254
 
259
- if (!variadicOnly) {
255
+ while (current.startsWith('-')) {
256
+ current = current.slice(1);
257
+ }
260
258
 
261
- if (!current.startsWith('--')) {
259
+ if (schemas[autocompleteIndex]) {
262
260
 
263
- items = items.concat(
264
- allKeyPaths
265
- .filter((keyPath) => !spent.includes(keyPath))
266
- .filter((keyPath) => key !== keyPath && keyPath.startsWith(key) && keyPaths(options.schema).get(keyPath).short)
267
- .map((keyPath) => ({
268
- type: 'option',
269
- argument: `-${caseit(keyPaths(options.schema).get(keyPath).short, 'kebab')}`,
270
- description: keyPaths(options.schema).get(keyPath).description
271
- })));
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
+ }
272
268
 
273
- }
269
+ if (schemas[autocompleteIndex].type === String && !schemas[autocompleteIndex].autocomplete) {
270
+ output([{
271
+ type: 'files'
272
+ }]);
273
+ }
274
274
 
275
+ output(
276
+ await schemas[autocompleteIndex].autocomplete?.(current) || []);
275
277
 
276
- items = items.concat(
277
- allKeyPaths
278
- .filter((keyPath) => !spent.includes(keyPath))
279
- .filter((keyPath) => key !== keyPath && keyPath.startsWith(key))
280
- .map((keyPath) => ({
281
- type: 'option',
282
- argument: `--${caseit(keyPath, 'kebab')}`,
283
- description: keyPaths(options.schema).get(keyPath).description
284
- })));
278
+ } else {
285
279
 
286
- }
287
-
288
- if (variadic === 'allow' && !variadicOnly && current === Array(current.length).fill('-').join('')) {
289
- items = items.concat([{
290
- type: 'option',
291
- argument: current.length ? '--'.slice(0, -current.length) : '--'
292
- }]);
293
- }
294
-
295
- return items;
280
+ let items = [];
296
281
 
282
+ if (variadic === 'allow') {
283
+ items = items.concat(
284
+ await autocomplete?.(current) || []);
297
285
  }
298
286
 
299
- };
287
+ if (!current.startsWith('--')) {
300
288
 
301
- for (idx = 0; idx < autocompleteIndex || idx < args.length; idx++) {
302
-
303
- if (!schemas[idx]) {
289
+ items = items.concat(
290
+ allKeyPaths
291
+ .filter((keyPath) => keyPaths(options.schema).get(keyPath).short)
292
+ .filter((keyPath) => !spent.includes(keyPath))
293
+ .map((keyPath) => ({
294
+ type: 'option',
295
+ argument: `-${caseit(keyPaths(options.schema).get(keyPath).short, 'kebab')}`,
296
+ description: keyPaths(options.schema).get(keyPath).description
297
+ })));
304
298
 
305
- if (variadicOnly || !args[idx].startsWith('-')) {
306
- continue;
307
- }
299
+ }
308
300
 
309
- const [keyPath, schema] = allKeyPaths
301
+ items = items.concat(
302
+ allKeyPaths
310
303
  .filter((keyPath) => !spent.includes(keyPath))
311
- .map((keyPath) => [keyPath, keyPaths(options.schema).get(keyPath)])
312
- .filter(([keyPath, schema]) => keyPath === args[idx] || `--${caseit(keyPath, 'kebab')}` === args[idx] || (schema.short && `-${schema.short}` === args[idx]))[0] || [];
313
-
314
- if (schema && schema.type !== Array) {
315
- spent.push(keyPath);
316
- }
317
-
318
- if (schema && schema.type !== Boolean) {
319
- schemas[idx + 1] = schema;
320
- }
304
+ .map((keyPath) => ({
305
+ type: 'option',
306
+ argument: `--${caseit(keyPath, 'kebab')}`,
307
+ description: keyPaths(options.schema).get(keyPath).description
308
+ })));
321
309
 
322
- }
310
+ output(items);
323
311
 
324
312
  }
325
313
 
326
- return output(
327
- await complete(autocompleteIndex));
328
-
329
314
  })().then(...resultArguments);
330
315
  }
331
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.139",
3
+ "version": "0.3.141",
4
4
  "description": "Yet another arguments parser.",
5
5
  "main": "index.js",
6
6
  "type": "module",