@trenskow/arguments-parser 0.3.138 → 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.
- package/lib/autocomplete/index.js +74 -90
- package/package.json +1 -1
|
@@ -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
|
-
|
|
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
|
|
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 (
|
|
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 (
|
|
233
|
+
if (schemas[autocompleteIndex]) {
|
|
232
234
|
|
|
233
|
-
if (schemas[
|
|
234
|
-
return Object.keys(schemas[
|
|
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[
|
|
243
|
+
if (schemas[autocompleteIndex].type === String && !schemas[autocompleteIndex].autocomplete) {
|
|
243
244
|
return [{
|
|
244
245
|
type: 'files'
|
|
245
246
|
}];
|
|
246
247
|
}
|
|
247
248
|
|
|
248
|
-
return
|
|
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
|
-
|
|
257
|
+
await autocomplete?.(current) || []);
|
|
257
258
|
}
|
|
258
259
|
|
|
259
|
-
if (!
|
|
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:
|
|
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
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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
|
|
|
@@ -298,16 +287,11 @@ const autocomplete = () => {
|
|
|
298
287
|
|
|
299
288
|
};
|
|
300
289
|
|
|
301
|
-
if (args.length === 0) {
|
|
302
|
-
return output(
|
|
303
|
-
await complete(-1));
|
|
304
|
-
}
|
|
305
|
-
|
|
306
290
|
for (idx = 0; idx < autocompleteIndex || idx < args.length; idx++) {
|
|
307
291
|
|
|
308
292
|
if (!schemas[idx]) {
|
|
309
293
|
|
|
310
|
-
if (
|
|
294
|
+
if (nonOptions || !(args[idx] || '').startsWith('-')) {
|
|
311
295
|
continue;
|
|
312
296
|
}
|
|
313
297
|
|
|
@@ -329,7 +313,7 @@ const autocomplete = () => {
|
|
|
329
313
|
}
|
|
330
314
|
|
|
331
315
|
return output(
|
|
332
|
-
await complete(
|
|
316
|
+
await complete());
|
|
333
317
|
|
|
334
318
|
})().then(...resultArguments);
|
|
335
319
|
}
|