@vexify-org/yaggs 4.0.0 → 5.1.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 +65 -15
- 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
|
|
|
@@ -89,6 +91,7 @@ class ArgumentParser {
|
|
|
89
91
|
aliases: config.aliases || [],
|
|
90
92
|
hidden: config.hidden || false,
|
|
91
93
|
strict: config.strict || false,
|
|
94
|
+
group: config.group || 'Commands',
|
|
92
95
|
...config
|
|
93
96
|
};
|
|
94
97
|
|
|
@@ -164,7 +167,7 @@ class ArgumentParser {
|
|
|
164
167
|
const arg = argv[i];
|
|
165
168
|
|
|
166
169
|
if (inOption) {
|
|
167
|
-
if (arg.startsWith('-') || this.commands[arg]) {
|
|
170
|
+
if (arg.startsWith('-') || this.commands[arg] || this._resolveCommandAlias(arg)) {
|
|
168
171
|
this._assignOption(result, inOption, optionAccumulator);
|
|
169
172
|
inOption = null;
|
|
170
173
|
optionAccumulator = [];
|
|
@@ -261,9 +264,10 @@ class ArgumentParser {
|
|
|
261
264
|
}
|
|
262
265
|
}
|
|
263
266
|
} else {
|
|
264
|
-
if (!currentCommand && this.commands[arg]) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
+
if (!currentCommand && (this.commands[arg] || this._resolveCommandAlias(arg))) {
|
|
268
|
+
const resolvedCommand = this._resolveCommandAlias(arg) || arg;
|
|
269
|
+
currentCommand = resolvedCommand;
|
|
270
|
+
commandChain.push(resolvedCommand);
|
|
267
271
|
} else {
|
|
268
272
|
result._.push(arg);
|
|
269
273
|
}
|
|
@@ -311,6 +315,8 @@ class ArgumentParser {
|
|
|
311
315
|
this._applyTypeConversion(result);
|
|
312
316
|
this._validateOptions(result);
|
|
313
317
|
this._validatePositionals(result);
|
|
318
|
+
this._validateConflicts(result);
|
|
319
|
+
this._applyImplies(result);
|
|
314
320
|
|
|
315
321
|
if (currentCommand && this.commands[currentCommand]) {
|
|
316
322
|
result.command = currentCommand;
|
|
@@ -451,6 +457,30 @@ class ArgumentParser {
|
|
|
451
457
|
}
|
|
452
458
|
}
|
|
453
459
|
|
|
460
|
+
_validateConflicts(result) {
|
|
461
|
+
for (const [name, opt] of Object.entries(this.options)) {
|
|
462
|
+
if (result[name] !== undefined && opt.conflicts.length > 0) {
|
|
463
|
+
for (const conflict of opt.conflicts) {
|
|
464
|
+
if (result[conflict] !== undefined) {
|
|
465
|
+
this._handleError(`Option '${name}' conflicts with '${conflict}'`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
_applyImplies(result) {
|
|
473
|
+
for (const [name, opt] of Object.entries(this.options)) {
|
|
474
|
+
if (result[name] !== undefined && Object.keys(opt.implies).length > 0) {
|
|
475
|
+
for (const [impliedName, impliedValue] of Object.entries(opt.implies)) {
|
|
476
|
+
if (result[impliedName] === undefined) {
|
|
477
|
+
result[impliedName] = impliedValue;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
454
484
|
_handleError(message) {
|
|
455
485
|
if (this.errorHandler) {
|
|
456
486
|
this.errorHandler(message);
|
|
@@ -460,6 +490,15 @@ class ArgumentParser {
|
|
|
460
490
|
}
|
|
461
491
|
}
|
|
462
492
|
|
|
493
|
+
_resolveCommandAlias(arg) {
|
|
494
|
+
for (const [name, cmd] of Object.entries(this.commands)) {
|
|
495
|
+
if (cmd.aliases && cmd.aliases.includes(arg)) {
|
|
496
|
+
return name;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return null;
|
|
500
|
+
}
|
|
501
|
+
|
|
463
502
|
getHelp() {
|
|
464
503
|
let help = `\u001b[36m${this.scriptName}\u001b[0m`;
|
|
465
504
|
|
|
@@ -497,21 +536,32 @@ class ArgumentParser {
|
|
|
497
536
|
|
|
498
537
|
const visibleCommands = Object.entries(this.commands).filter(([, cmd]) => !cmd.hidden);
|
|
499
538
|
if (visibleCommands.length > 0) {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
const maxLen = Math.max(...visibleCommands.map(([name]) => name.length)) + 4;
|
|
503
|
-
|
|
539
|
+
const groupedCommands = {};
|
|
504
540
|
for (const [name, cmd] of visibleCommands) {
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
line += ` (${cmd.aliases.join(', ')})`;
|
|
541
|
+
const group = cmd.group || 'Commands';
|
|
542
|
+
if (!groupedCommands[group]) {
|
|
543
|
+
groupedCommands[group] = [];
|
|
509
544
|
}
|
|
545
|
+
groupedCommands[group].push({ name, cmd });
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
for (const [groupName, commands] of Object.entries(groupedCommands)) {
|
|
549
|
+
help += `\n\n\u001b[33m${groupName}:\u001b[0m`;
|
|
510
550
|
|
|
511
|
-
|
|
512
|
-
line += cmd.description;
|
|
551
|
+
const maxLen = Math.max(...commands.map(({ name }) => name.length)) + 4;
|
|
513
552
|
|
|
514
|
-
|
|
553
|
+
for (const { name, cmd } of commands) {
|
|
554
|
+
let line = ` ${name}`;
|
|
555
|
+
|
|
556
|
+
if (cmd.aliases && cmd.aliases.length > 0) {
|
|
557
|
+
line += ` (${cmd.aliases.join(', ')})`;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
line = line.padEnd(maxLen);
|
|
561
|
+
line += cmd.description;
|
|
562
|
+
|
|
563
|
+
help += `\n${line}`;
|
|
564
|
+
}
|
|
515
565
|
}
|
|
516
566
|
}
|
|
517
567
|
|