@trenskow/arguments-parser 0.3.139 → 0.3.140

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.
@@ -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,51 @@ 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) => 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
+ }
122
+
123
+ return grouped;
124
+
125
+ }, {}));
126
+
129
127
  const result = {
130
128
  get base() {
131
129
  return base;
@@ -137,7 +135,6 @@ const autocomplete = () => {
137
135
  if (autocompleteIndex === 0) {
138
136
 
139
137
  return output(Object.keys(commands)
140
- .filter((command) => command.startsWith(tool))
141
138
  .map((command) => ({
142
139
  type: 'command',
143
140
  argument: caseit(command, 'kebab'),
@@ -166,8 +163,7 @@ const autocomplete = () => {
166
163
 
167
164
  if (autocompleteIndex === 0) {
168
165
  return output(
169
- filter(
170
- await autocomplete?.(args[0] || '') || [], args[0]));
166
+ await autocomplete?.(args[0] || '') || [], args[0]);
171
167
  }
172
168
 
173
169
  parameters[identifier] = args[0];
@@ -206,7 +202,7 @@ const autocomplete = () => {
206
202
  }]
207
203
  } = options.validationOptions;
208
204
 
209
- const variadicOnly = args
205
+ const nonOptions = args
210
206
  .slice(0, autocompleteIndex)
211
207
  .some((arg) => arg === '--');
212
208
 
@@ -218,7 +214,13 @@ const autocomplete = () => {
218
214
  let schemas = {};
219
215
  let spent = [];
220
216
 
221
- const complete = async (idx, current = '') => {
217
+ const complete = async (current = args[autocompleteIndex] || '') => {
218
+
219
+ if (nonOptions) {
220
+ return [{
221
+ type: 'files'
222
+ }];
223
+ }
222
224
 
223
225
  let key = current;
224
226
 
@@ -228,24 +230,23 @@ const autocomplete = () => {
228
230
 
229
231
  key = caseit(key);
230
232
 
231
- if (!variadicOnly && schemas[idx]) {
233
+ if (schemas[autocompleteIndex]) {
232
234
 
233
- if (schemas[idx].enum) {
234
- return Object.keys(schemas[idx].enum)
235
- .filter((item) => key !== item && item.startsWith(current))
235
+ if (schemas[autocompleteIndex].enum) {
236
+ return Object.keys(schemas[autocompleteIndex].enum)
236
237
  .map((item) => ({
237
238
  type: 'option',
238
239
  argument: caseit(item, 'kebab')
239
240
  }));
240
241
  }
241
242
 
242
- if (schemas[idx].type === String && !schemas[idx].autocomplete) {
243
+ if (schemas[autocompleteIndex].type === String && !schemas[autocompleteIndex].autocomplete) {
243
244
  return [{
244
245
  type: 'files'
245
246
  }];
246
247
  }
247
248
 
248
- return filter(await schemas[idx].autocomplete?.(current) || [], current);
249
+ return await schemas[autocompleteIndex].autocomplete?.(current) || [];
249
250
 
250
251
  } else {
251
252
 
@@ -253,44 +254,32 @@ const autocomplete = () => {
253
254
 
254
255
  if (variadic === 'allow') {
255
256
  items = items.concat(
256
- filter(await autocomplete?.(current) || [], current));
257
+ await autocomplete?.(current) || []);
257
258
  }
258
259
 
259
- if (!variadicOnly) {
260
-
261
- if (!current.startsWith('--')) {
262
-
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
- })));
272
-
273
- }
274
-
260
+ if (!current.startsWith('--')) {
275
261
 
276
262
  items = items.concat(
277
263
  allKeyPaths
264
+ .filter((keyPath) => keyPaths(options.schema).get(keyPath).short)
278
265
  .filter((keyPath) => !spent.includes(keyPath))
279
- .filter((keyPath) => key !== keyPath && keyPath.startsWith(key))
280
266
  .map((keyPath) => ({
281
267
  type: 'option',
282
- argument: `--${caseit(keyPath, 'kebab')}`,
268
+ argument: `-${caseit(keyPaths(options.schema).get(keyPath).short, 'kebab')}`,
283
269
  description: keyPaths(options.schema).get(keyPath).description
284
270
  })));
285
271
 
286
272
  }
287
273
 
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
- }
274
+
275
+ items = items.concat(
276
+ allKeyPaths
277
+ .filter((keyPath) => !spent.includes(keyPath))
278
+ .map((keyPath) => ({
279
+ type: 'option',
280
+ argument: `--${caseit(keyPath, 'kebab')}`,
281
+ description: keyPaths(options.schema).get(keyPath).description
282
+ })));
294
283
 
295
284
  return items;
296
285
 
@@ -302,7 +291,7 @@ const autocomplete = () => {
302
291
 
303
292
  if (!schemas[idx]) {
304
293
 
305
- if (variadicOnly || !args[idx].startsWith('-')) {
294
+ if (nonOptions || !(args[idx] || '').startsWith('-')) {
306
295
  continue;
307
296
  }
308
297
 
@@ -324,7 +313,7 @@ const autocomplete = () => {
324
313
  }
325
314
 
326
315
  return output(
327
- await complete(autocompleteIndex));
316
+ await complete());
328
317
 
329
318
  })().then(...resultArguments);
330
319
  }
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.140",
4
4
  "description": "Yet another arguments parser.",
5
5
  "main": "index.js",
6
6
  "type": "module",