@vexify-org/yaggs 5.2.0 → 6.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 (3) hide show
  1. package/index.js +4 -0
  2. package/lib/parser.js +157 -0
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -105,6 +105,10 @@ class Yaggs {
105
105
  return this;
106
106
  }
107
107
 
108
+ generateCompletion(shell = 'bash') {
109
+ return this.parser.generateCompletion(shell);
110
+ }
111
+
108
112
  check(fn) {
109
113
  this.middleware(fn);
110
114
  return this;
package/lib/parser.js CHANGED
@@ -394,6 +394,25 @@ class ArgumentParser {
394
394
  if (!ipRegex.test(value)) {
395
395
  this._handleError(`Invalid IP address for option '${name}'`);
396
396
  }
397
+ } else if (opt.type === 'file') {
398
+ const fs = require('fs');
399
+ const path = require('path');
400
+ const fullPath = path.resolve(value);
401
+ if (!fs.existsSync(fullPath)) {
402
+ this._handleError(`File not found: ${fullPath}`);
403
+ }
404
+ result[name] = fullPath;
405
+ } else if (opt.type === 'directory') {
406
+ const fs = require('fs');
407
+ const path = require('path');
408
+ const fullPath = path.resolve(value);
409
+ if (!fs.existsSync(fullPath)) {
410
+ this._handleError(`Directory not found: ${fullPath}`);
411
+ }
412
+ if (!fs.statSync(fullPath).isDirectory()) {
413
+ this._handleError(`Not a directory: ${fullPath}`);
414
+ }
415
+ result[name] = fullPath;
397
416
  } else if (opt.type === 'custom' && opt._customType) {
398
417
  result[name] = opt._customType(value);
399
418
  }
@@ -517,6 +536,144 @@ class ArgumentParser {
517
536
  return null;
518
537
  }
519
538
 
539
+ generateCompletion(shell = 'bash') {
540
+ if (shell === 'bash') {
541
+ return this._generateBashCompletion();
542
+ } else if (shell === 'zsh') {
543
+ return this._generateZshCompletion();
544
+ } else if (shell === 'fish') {
545
+ return this._generateFishCompletion();
546
+ }
547
+ return '';
548
+ }
549
+
550
+ _generateBashCompletion() {
551
+ let completion = `#!/usr/bin/env bash
552
+
553
+ _${this.scriptName}_completions() {
554
+ local cur prev opts
555
+ COMPREPLY=()
556
+ cur="\${COMP_WORDS[COMP_CWORD]}"
557
+ prev="\${COMP_WORDS[COMP_CWORD-1]}"
558
+
559
+ opts=""
560
+
561
+ OPTIONS="`;
562
+
563
+ for (const [name, opt] of Object.entries(this.options)) {
564
+ if (!opt.hidden) {
565
+ completion += `--${name} `;
566
+ if (opt.alias && opt.alias.length > 0) {
567
+ const aliases = Array.isArray(opt.alias) ? opt.alias : [opt.alias];
568
+ for (const alias of aliases) {
569
+ completion += `-${alias} `;
570
+ }
571
+ }
572
+ }
573
+ }
574
+
575
+ completion += `"
576
+
577
+ COMMANDS="`;
578
+
579
+ for (const [name, cmd] of Object.entries(this.commands)) {
580
+ if (!cmd.hidden) {
581
+ completion += `${name} `;
582
+ if (cmd.aliases) {
583
+ completion += `${cmd.aliases.join(' ')} `;
584
+ }
585
+ }
586
+ }
587
+
588
+ completion += `"
589
+
590
+ if [[ "\${cur}" == -* ]]; then
591
+ COMPREPLY=( \$(compgen -W "\${OPTIONS}" -- "\${cur}") )
592
+ else
593
+ COMPREPLY=( \$(compgen -W "\${COMMANDS}" -- "\${cur}") )
594
+ fi
595
+
596
+ return 0
597
+ }
598
+
599
+ complete -F _${this.scriptName}_completions ${this.scriptName}
600
+ `;
601
+
602
+ return completion;
603
+ }
604
+
605
+ _generateZshCompletion() {
606
+ let completion = `#compdef ${this.scriptName}
607
+
608
+ local commands options
609
+
610
+ commands=('`;
611
+
612
+ for (const [name, cmd] of Object.entries(this.commands)) {
613
+ if (!cmd.hidden) {
614
+ completion += `${name}:${cmd.description} `;
615
+ }
616
+ }
617
+
618
+ completion += `')
619
+
620
+ options=('`;
621
+
622
+ for (const [name, opt] of Object.entries(this.options)) {
623
+ if (!opt.hidden) {
624
+ completion += `--${name}[${opt.description}]:`;
625
+ if (opt.choices) {
626
+ completion += ':(' + opt.choices.join(' ') + ') ';
627
+ } else {
628
+ completion += ' ';
629
+ }
630
+ if (opt.alias && opt.alias.length > 0) {
631
+ const aliases = Array.isArray(opt.alias) ? opt.alias : [opt.alias];
632
+ for (const alias of aliases) {
633
+ completion += `-${alias}[${opt.description}]: `;
634
+ }
635
+ }
636
+ }
637
+ }
638
+
639
+ completion += `')
640
+
641
+ _arguments \${options[@]} \\
642
+ '1:command:\${commands}' \\
643
+ '*::args:_normal'
644
+ `;
645
+
646
+ return completion;
647
+ }
648
+
649
+ _generateFishCompletion() {
650
+ let completion = `#!/usr/bin/env fish
651
+
652
+ complete -c ${this.scriptName} `;
653
+
654
+ for (const [name, opt] of Object.entries(this.options)) {
655
+ if (!opt.hidden) {
656
+ completion += `-l ${name} -d "${opt.description}" `;
657
+ if (opt.alias && opt.alias.length > 0) {
658
+ const aliases = Array.isArray(opt.alias) ? opt.alias : [opt.alias];
659
+ for (const alias of aliases) {
660
+ completion += `-s ${alias} `;
661
+ }
662
+ }
663
+ }
664
+ }
665
+
666
+ completion += '\n\n';
667
+
668
+ for (const [name, cmd] of Object.entries(this.commands)) {
669
+ if (!cmd.hidden) {
670
+ completion += `complete -c ${this.scriptName} -a "${name}" -d "${cmd.description}"\n`;
671
+ }
672
+ }
673
+
674
+ return completion;
675
+ }
676
+
520
677
  getHelp() {
521
678
  let help = `\u001b[36m${this.scriptName}\u001b[0m`;
522
679
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vexify-org/yaggs",
3
- "version": "5.2.0",
3
+ "version": "6.0.0",
4
4
  "description": "A powerful CLI argument parser, better than yargs",
5
5
  "main": "index.js",
6
6
  "scripts": {