@vexify-org/yaggs 5.0.0 → 5.2.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.
- package/lib/parser.js +46 -0
- package/package.json +1 -1
package/lib/parser.js
CHANGED
|
@@ -39,6 +39,8 @@ class ArgumentParser {
|
|
|
39
39
|
demandOption: config.demandOption || config.required || false,
|
|
40
40
|
normalize: config.normalize,
|
|
41
41
|
hidden: config.hidden || false,
|
|
42
|
+
conflicts: config.conflicts || [],
|
|
43
|
+
implies: config.implies || {},
|
|
42
44
|
...config
|
|
43
45
|
};
|
|
44
46
|
|
|
@@ -313,6 +315,8 @@ class ArgumentParser {
|
|
|
313
315
|
this._applyTypeConversion(result);
|
|
314
316
|
this._validateOptions(result);
|
|
315
317
|
this._validatePositionals(result);
|
|
318
|
+
this._validateConflicts(result);
|
|
319
|
+
this._applyImplies(result);
|
|
316
320
|
|
|
317
321
|
if (currentCommand && this.commands[currentCommand]) {
|
|
318
322
|
result.command = currentCommand;
|
|
@@ -374,6 +378,24 @@ class ArgumentParser {
|
|
|
374
378
|
} catch {
|
|
375
379
|
this._handleError(`Invalid JSON for option '${name}'`);
|
|
376
380
|
}
|
|
381
|
+
} else if (opt.type === 'url') {
|
|
382
|
+
try {
|
|
383
|
+
result[name] = new URL(value);
|
|
384
|
+
} catch {
|
|
385
|
+
this._handleError(`Invalid URL for option '${name}'`);
|
|
386
|
+
}
|
|
387
|
+
} else if (opt.type === 'email') {
|
|
388
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
389
|
+
if (!emailRegex.test(value)) {
|
|
390
|
+
this._handleError(`Invalid email for option '${name}'`);
|
|
391
|
+
}
|
|
392
|
+
} else if (opt.type === 'ip') {
|
|
393
|
+
const ipRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
394
|
+
if (!ipRegex.test(value)) {
|
|
395
|
+
this._handleError(`Invalid IP address for option '${name}'`);
|
|
396
|
+
}
|
|
397
|
+
} else if (opt.type === 'custom' && opt._customType) {
|
|
398
|
+
result[name] = opt._customType(value);
|
|
377
399
|
}
|
|
378
400
|
}
|
|
379
401
|
}
|
|
@@ -453,6 +475,30 @@ class ArgumentParser {
|
|
|
453
475
|
}
|
|
454
476
|
}
|
|
455
477
|
|
|
478
|
+
_validateConflicts(result) {
|
|
479
|
+
for (const [name, opt] of Object.entries(this.options)) {
|
|
480
|
+
if (result[name] !== undefined && opt.conflicts.length > 0) {
|
|
481
|
+
for (const conflict of opt.conflicts) {
|
|
482
|
+
if (result[conflict] !== undefined) {
|
|
483
|
+
this._handleError(`Option '${name}' conflicts with '${conflict}'`);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
_applyImplies(result) {
|
|
491
|
+
for (const [name, opt] of Object.entries(this.options)) {
|
|
492
|
+
if (result[name] !== undefined && Object.keys(opt.implies).length > 0) {
|
|
493
|
+
for (const [impliedName, impliedValue] of Object.entries(opt.implies)) {
|
|
494
|
+
if (result[impliedName] === undefined) {
|
|
495
|
+
result[impliedName] = impliedValue;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
456
502
|
_handleError(message) {
|
|
457
503
|
if (this.errorHandler) {
|
|
458
504
|
this.errorHandler(message);
|