@trenskow/arguments-parser 0.2.20 → 0.2.21
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/index.js +32 -21
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -43,15 +43,31 @@ const argumentsParser = (
|
|
|
43
43
|
|
|
44
44
|
const base = `${process.argv.slice(1, argvLevel + 2).map(((arg) => basename(arg))).join(' ')}`;
|
|
45
45
|
|
|
46
|
-
const list = (print, items) => {
|
|
46
|
+
const list = (print, items, spacing = false) => {
|
|
47
47
|
|
|
48
48
|
const maxLength = Object.keys(items)
|
|
49
49
|
.reduce((length, key) => Math.max(length, key.length), 0);
|
|
50
50
|
|
|
51
51
|
Object.entries(items)
|
|
52
|
-
.forEach(([key, description]) => {
|
|
52
|
+
.forEach(([key, description], idx) => {
|
|
53
|
+
|
|
54
|
+
if (spacing && idx > 0) print();
|
|
55
|
+
|
|
53
56
|
print.nn(` ${key}`, { minimumLength: maxLength + 4 });
|
|
54
|
-
|
|
57
|
+
|
|
58
|
+
if (!Array.isArray(description)) description = [description];
|
|
59
|
+
|
|
60
|
+
description
|
|
61
|
+
.forEach((description, idx) => {
|
|
62
|
+
|
|
63
|
+
if (idx > 0) {
|
|
64
|
+
print.nn('', { minimumLength: maxLength + 4 });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
print.sentence(description);
|
|
68
|
+
|
|
69
|
+
});
|
|
70
|
+
|
|
55
71
|
});
|
|
56
72
|
|
|
57
73
|
};
|
|
@@ -70,7 +86,7 @@ const argumentsParser = (
|
|
|
70
86
|
print(helpUsage.join(' '));
|
|
71
87
|
|
|
72
88
|
if (helpOptions) {
|
|
73
|
-
print.nn(helpOptions);
|
|
89
|
+
print.nn(helpOptions());
|
|
74
90
|
}
|
|
75
91
|
|
|
76
92
|
if (error) {
|
|
@@ -97,7 +113,7 @@ const argumentsParser = (
|
|
|
97
113
|
|
|
98
114
|
helpUsage.push(`${opening}${strings?.commands?.help?.placeholder || 'command'}${closing}`);
|
|
99
115
|
|
|
100
|
-
helpOptions =
|
|
116
|
+
helpOptions = () => {
|
|
101
117
|
|
|
102
118
|
print();
|
|
103
119
|
print(strings?.commands?.help?.available || 'Available commands:');
|
|
@@ -113,7 +129,7 @@ const argumentsParser = (
|
|
|
113
129
|
list(print, Object.fromEntries(tools
|
|
114
130
|
.map((tool) => [tool.name, tool.description || (strings?.commands?.help?.noDescription || 'No description')])));
|
|
115
131
|
|
|
116
|
-
}
|
|
132
|
+
};
|
|
117
133
|
|
|
118
134
|
if (args.length === 0) printHelp();
|
|
119
135
|
|
|
@@ -188,10 +204,11 @@ const argumentsParser = (
|
|
|
188
204
|
|
|
189
205
|
if (allKeyPaths.length) {
|
|
190
206
|
|
|
191
|
-
helpOptions =
|
|
207
|
+
helpOptions = () => {
|
|
192
208
|
|
|
193
209
|
print();
|
|
194
210
|
print(strings?.options?.help?.title || 'Options:');
|
|
211
|
+
print();
|
|
195
212
|
|
|
196
213
|
list(print, Object.fromEntries(allKeyPaths
|
|
197
214
|
.map((keyPath) => {
|
|
@@ -199,22 +216,20 @@ const argumentsParser = (
|
|
|
199
216
|
const keyPathSchema = keyPaths(schema).get(keyPath);
|
|
200
217
|
|
|
201
218
|
let optionKeys = [`--${caseit(keyPath, 'kebab')}`];
|
|
202
|
-
let description = keyPathSchema.description || 'No description';
|
|
219
|
+
let description = [keyPathSchema.description || 'No description'];
|
|
203
220
|
|
|
204
221
|
if (shortOptions[keyPath]) {
|
|
205
222
|
optionKeys.push(`-${shortOptions[keyPath]}`);
|
|
206
223
|
}
|
|
207
224
|
|
|
208
225
|
if (typeof keyPathSchema.enum !== 'undefined') {
|
|
209
|
-
description
|
|
226
|
+
description.push(`(${opening}${keyPath}${closing}: ${Object.keys(keyPathSchema.enum).map((value) => `\`${value}\``).join(', ')})`);
|
|
210
227
|
}
|
|
211
228
|
|
|
212
|
-
let addition;
|
|
213
|
-
|
|
214
229
|
if (keyPathSchema.type === Array) {
|
|
215
|
-
|
|
230
|
+
description.push(`${strings?.options?.help?.allowsMultple || '(allows multiple)'}`);
|
|
216
231
|
} else if (keyPathSchema.required === true) {
|
|
217
|
-
|
|
232
|
+
description.push(`${strings?.options?.help?.required || '(required)'}`);
|
|
218
233
|
} else if (typeof keyPathSchema.default !== 'undefined') {
|
|
219
234
|
|
|
220
235
|
let defaultDescription;
|
|
@@ -231,15 +246,11 @@ const argumentsParser = (
|
|
|
231
246
|
|
|
232
247
|
}
|
|
233
248
|
|
|
234
|
-
|
|
235
|
-
.replace('<default>', defaultDescription)}
|
|
249
|
+
description.push(`${(strings?.options?.help?.default || '(default: <default>)')
|
|
250
|
+
.replace('<default>', defaultDescription)}`);
|
|
236
251
|
|
|
237
252
|
}
|
|
238
253
|
|
|
239
|
-
if (addition) description += addition;
|
|
240
|
-
|
|
241
|
-
description += '.';
|
|
242
|
-
|
|
243
254
|
let option = optionKeys.reverse().join(', ');
|
|
244
255
|
|
|
245
256
|
if (keyPathSchema.type !== Boolean) {
|
|
@@ -248,9 +259,9 @@ const argumentsParser = (
|
|
|
248
259
|
|
|
249
260
|
return [option, description];
|
|
250
261
|
|
|
251
|
-
})));
|
|
262
|
+
})), true);
|
|
252
263
|
|
|
253
|
-
}
|
|
264
|
+
};
|
|
254
265
|
}
|
|
255
266
|
|
|
256
267
|
checkHelp();
|