@trenskow/arguments-parser 0.2.13 → 0.2.15

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 +59 -9
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -17,7 +17,9 @@ plugins.use(() => ({
17
17
  supportsType: () => true,
18
18
  validatorsForType: () => ({
19
19
  description: ['string'],
20
- defaultDescription: ['string']
20
+ defaultDescription: ['string'],
21
+ secret: ['boolean'],
22
+ short: ['string'],
21
23
  }),
22
24
  validate: (data) => data,
23
25
  formalize: (schema) => schema
@@ -165,6 +167,25 @@ const argumentsParser = (
165
167
 
166
168
  helpUsage.push(`${opening}${strings?.options?.help?.placeholder || 'options'}${closing}`);
167
169
 
170
+ let shortOptions = {};
171
+
172
+ allKeyPaths
173
+ .forEach((keyPath) => {
174
+
175
+ const keyPathSchema = keyPaths(schema).get(keyPath);
176
+
177
+ if (keyPathSchema.short) {
178
+
179
+ if (shortOptions[keyPathSchema.short]) {
180
+ throw new Error(`Short option "${keyPathSchema.short}" already used.`);
181
+ }
182
+
183
+ shortOptions[keyPath] = keyPathSchema.short;
184
+
185
+ }
186
+
187
+ });
188
+
168
189
  if (allKeyPaths.length) {
169
190
 
170
191
  helpOptions = print.toString((print) => {
@@ -177,8 +198,13 @@ const argumentsParser = (
177
198
 
178
199
  const keyPathSchema = keyPaths(schema).get(keyPath);
179
200
 
201
+ let optionKeys = [`--${caseit(keyPath, 'kebab')}`];
180
202
  let description = keyPathSchema.description || 'No description';
181
203
 
204
+ if (shortOptions[keyPath]) {
205
+ optionKeys.push(`-${shortOptions[keyPath]}`);
206
+ }
207
+
182
208
  if (typeof keyPathSchema.enum !== 'undefined') {
183
209
  description += ` (${Object.keys(keyPathSchema.enum).map((value) => `\`${value}\``).join(', ')})`;
184
210
  }
@@ -193,10 +219,16 @@ const argumentsParser = (
193
219
 
194
220
  let defaultDescription;
195
221
 
196
- if (keyPathSchema.type === Boolean) {
197
- defaultDescription = keyPathSchema.default === true ? 'enabled' : 'disabled';
222
+ if (keyPathSchema.secret === true) {
223
+ defaultDescription = '`********`';
198
224
  } else {
199
- defaultDescription = `\`${keyPathSchema.defaultDescription ?? keyPathSchema.default}\``;
225
+
226
+ if (keyPathSchema.type === Boolean) {
227
+ defaultDescription = keyPathSchema.default === true ? 'enabled' : 'disabled';
228
+ } else {
229
+ defaultDescription = `\`${keyPathSchema.defaultDescription ?? keyPathSchema.default}\``;
230
+ }
231
+
200
232
  }
201
233
 
202
234
  addition = ` ${(strings?.options?.help?.default || '(default: <default>)')
@@ -208,7 +240,13 @@ const argumentsParser = (
208
240
 
209
241
  description += '.';
210
242
 
211
- return [`--${caseit(keyPath, 'kebab')}`, description];
243
+ let option = optionKeys.reverse().join(', ');
244
+
245
+ if (keyPathSchema.type !== Boolean) {
246
+ option += ` ${strings?.options?.help?.argument || `${opening}${caseit(keyPath, 'kebab')}${closing}`}`;
247
+ }
248
+
249
+ return [option, description];
212
250
 
213
251
  })));
214
252
 
@@ -224,14 +262,22 @@ const argumentsParser = (
224
262
 
225
263
  for (idx = 0 ; idx < args.length ; idx++) {
226
264
 
227
- if (args[idx].slice(0, 2) !== '--') {
265
+ if (args[idx].slice(0, 1) !== '-') {
228
266
  rest.push(args[idx]);
229
267
  continue;
230
268
  }
231
269
 
232
- const arg = args[idx].slice(2);
270
+ shortOptions = Object.fromEntries(
271
+ Object.entries(shortOptions)
272
+ .map(([key, value]) => [value, key]));
233
273
 
234
- const key = caseit(arg);
274
+ let key = args[idx].slice(1);
275
+
276
+ if (args[idx].slice(1, 2) === '-') {
277
+ key = caseit(args[idx].slice(2));
278
+ } else if (shortOptions[key]) {
279
+ key = caseit(shortOptions[key]);
280
+ }
235
281
 
236
282
  if (!keyPaths(schema).all().includes(key)) {
237
283
  printHelp(
@@ -269,7 +315,11 @@ const argumentsParser = (
269
315
  aggregatedErrors: 'flatten'
270
316
  });
271
317
  } catch (error) {
272
- printHelp(error);
318
+ let message = error.message;
319
+ if (error.keyPath?.[0]) {
320
+ message = `${caseit(error.keyPath[0], 'kebab')}: ${message}`;
321
+ }
322
+ printHelp(new Error(message));
273
323
  }
274
324
 
275
325
  return Object.assign({}, data, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/arguments-parser",
3
- "version": "0.2.13",
3
+ "version": "0.2.15",
4
4
  "description": "Yet another arguments parser.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -25,7 +25,7 @@
25
25
  "homepage": "https://github.com/trenskow/arguments-parser#readme",
26
26
  "dependencies": {
27
27
  "@trenskow/caseit": "^1.4.6",
28
- "@trenskow/print": "^0.1.7",
28
+ "@trenskow/print": "^0.1.8",
29
29
  "isvalid": "^4.1.27"
30
30
  },
31
31
  "devDependencies": {