@vexify-org/yaggs 4.0.0 → 5.0.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/parser.js +37 -15
  2. package/package.json +1 -1
package/lib/parser.js CHANGED
@@ -89,6 +89,7 @@ class ArgumentParser {
89
89
  aliases: config.aliases || [],
90
90
  hidden: config.hidden || false,
91
91
  strict: config.strict || false,
92
+ group: config.group || 'Commands',
92
93
  ...config
93
94
  };
94
95
 
@@ -164,7 +165,7 @@ class ArgumentParser {
164
165
  const arg = argv[i];
165
166
 
166
167
  if (inOption) {
167
- if (arg.startsWith('-') || this.commands[arg]) {
168
+ if (arg.startsWith('-') || this.commands[arg] || this._resolveCommandAlias(arg)) {
168
169
  this._assignOption(result, inOption, optionAccumulator);
169
170
  inOption = null;
170
171
  optionAccumulator = [];
@@ -261,9 +262,10 @@ class ArgumentParser {
261
262
  }
262
263
  }
263
264
  } else {
264
- if (!currentCommand && this.commands[arg]) {
265
- currentCommand = arg;
266
- commandChain.push(arg);
265
+ if (!currentCommand && (this.commands[arg] || this._resolveCommandAlias(arg))) {
266
+ const resolvedCommand = this._resolveCommandAlias(arg) || arg;
267
+ currentCommand = resolvedCommand;
268
+ commandChain.push(resolvedCommand);
267
269
  } else {
268
270
  result._.push(arg);
269
271
  }
@@ -460,6 +462,15 @@ class ArgumentParser {
460
462
  }
461
463
  }
462
464
 
465
+ _resolveCommandAlias(arg) {
466
+ for (const [name, cmd] of Object.entries(this.commands)) {
467
+ if (cmd.aliases && cmd.aliases.includes(arg)) {
468
+ return name;
469
+ }
470
+ }
471
+ return null;
472
+ }
473
+
463
474
  getHelp() {
464
475
  let help = `\u001b[36m${this.scriptName}\u001b[0m`;
465
476
 
@@ -497,21 +508,32 @@ class ArgumentParser {
497
508
 
498
509
  const visibleCommands = Object.entries(this.commands).filter(([, cmd]) => !cmd.hidden);
499
510
  if (visibleCommands.length > 0) {
500
- help += '\n\n\u001b[33mCommands:\u001b[0m';
501
-
502
- const maxLen = Math.max(...visibleCommands.map(([name]) => name.length)) + 4;
503
-
511
+ const groupedCommands = {};
504
512
  for (const [name, cmd] of visibleCommands) {
505
- let line = ` ${name}`;
506
-
507
- if (cmd.aliases && cmd.aliases.length > 0) {
508
- line += ` (${cmd.aliases.join(', ')})`;
513
+ const group = cmd.group || 'Commands';
514
+ if (!groupedCommands[group]) {
515
+ groupedCommands[group] = [];
509
516
  }
517
+ groupedCommands[group].push({ name, cmd });
518
+ }
519
+
520
+ for (const [groupName, commands] of Object.entries(groupedCommands)) {
521
+ help += `\n\n\u001b[33m${groupName}:\u001b[0m`;
510
522
 
511
- line = line.padEnd(maxLen);
512
- line += cmd.description;
523
+ const maxLen = Math.max(...commands.map(({ name }) => name.length)) + 4;
513
524
 
514
- help += `\n${line}`;
525
+ for (const { name, cmd } of commands) {
526
+ let line = ` ${name}`;
527
+
528
+ if (cmd.aliases && cmd.aliases.length > 0) {
529
+ line += ` (${cmd.aliases.join(', ')})`;
530
+ }
531
+
532
+ line = line.padEnd(maxLen);
533
+ line += cmd.description;
534
+
535
+ help += `\n${line}`;
536
+ }
515
537
  }
516
538
  }
517
539
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vexify-org/yaggs",
3
- "version": "4.0.0",
3
+ "version": "5.0.0",
4
4
  "description": "A powerful CLI argument parser, better than yargs",
5
5
  "main": "index.js",
6
6
  "scripts": {