@trenskow/arguments-parser 0.2.20 → 0.3.0

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.
Files changed (2) hide show
  1. package/lib/index.js +53 -26
  2. 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
- print.sentence(description);
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 = print.toString((print) => {
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 = print.toString((print) => {
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 += ` (${Object.keys(keyPathSchema.enum).map((value) => `\`${value}\``).join(', ')})`;
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
- addition = ` ${strings?.options?.help?.allowsMultple || '(allows multiple)'}`;
230
+ description.push(`${strings?.options?.help?.allowsMultple || '(allows multiple)'}`);
216
231
  } else if (keyPathSchema.required === true) {
217
- addition = ` ${strings?.options?.help?.required || '(required)'}`;
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
- addition = ` ${(strings?.options?.help?.default || '(default: <default>)')
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,23 +259,21 @@ 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();
257
268
 
258
269
  let data = {};
259
- let rest = [];
260
270
 
261
271
  let idx;
262
272
 
263
273
  for (idx = 0 ; idx < args.length ; idx++) {
264
274
 
265
275
  if (args[idx].slice(0, 1) !== '-') {
266
- rest.push(args[idx]);
267
- continue;
276
+ break;
268
277
  }
269
278
 
270
279
  shortOptions = Object.fromEntries(
@@ -310,6 +319,13 @@ const argumentsParser = (
310
319
 
311
320
  }
312
321
 
322
+ if (idx < args.length) {
323
+ printHelp(
324
+ new Error(
325
+ (strings?.empty?.help?.errors?.unexpected || 'Unexpected argument: <argument>.')
326
+ .replace('<argument>', args[0])));
327
+ }
328
+
313
329
  try {
314
330
  data = await isvalid(data, schema, {
315
331
  aggregatedErrors: 'flatten'
@@ -327,10 +343,21 @@ const argumentsParser = (
327
343
  print.err(error.message);
328
344
  process.exit(1);
329
345
  },
330
- nonOptions,
331
- rest
346
+ nonOptions
332
347
  });
333
348
 
349
+ },
350
+ empty: async () => {
351
+
352
+ checkHelp();
353
+
354
+ if (args.length > 0) {
355
+ printHelp(
356
+ new Error(
357
+ (strings?.empty?.help?.errors?.unexpected || 'Unexpected argument: <argument>.')
358
+ .replace('<argument>', args[0])));
359
+ }
360
+
334
361
  },
335
362
  values: async (schema) => {
336
363
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/arguments-parser",
3
- "version": "0.2.20",
3
+ "version": "0.3.0",
4
4
  "description": "Yet another arguments parser.",
5
5
  "main": "index.js",
6
6
  "type": "module",