@vexify-org/yaggs 5.3.0 → 6.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.
Files changed (3) hide show
  1. package/index.js +4 -0
  2. package/lib/parser.js +151 -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
@@ -41,6 +41,7 @@ class ArgumentParser {
41
41
  hidden: config.hidden || false,
42
42
  conflicts: config.conflicts || [],
43
43
  implies: config.implies || {},
44
+ global: config.global !== undefined ? config.global : true,
44
45
  ...config
45
46
  };
46
47
 
@@ -413,6 +414,18 @@ class ArgumentParser {
413
414
  this._handleError(`Not a directory: ${fullPath}`);
414
415
  }
415
416
  result[name] = fullPath;
417
+ } else if (opt.type === 'date') {
418
+ const date = new Date(value);
419
+ if (isNaN(date.getTime())) {
420
+ this._handleError(`Invalid date for option '${name}'`);
421
+ }
422
+ result[name] = date;
423
+ } else if (opt.type === 'regex') {
424
+ try {
425
+ result[name] = new RegExp(value);
426
+ } catch {
427
+ this._handleError(`Invalid regex for option '${name}'`);
428
+ }
416
429
  } else if (opt.type === 'custom' && opt._customType) {
417
430
  result[name] = opt._customType(value);
418
431
  }
@@ -536,6 +549,144 @@ class ArgumentParser {
536
549
  return null;
537
550
  }
538
551
 
552
+ generateCompletion(shell = 'bash') {
553
+ if (shell === 'bash') {
554
+ return this._generateBashCompletion();
555
+ } else if (shell === 'zsh') {
556
+ return this._generateZshCompletion();
557
+ } else if (shell === 'fish') {
558
+ return this._generateFishCompletion();
559
+ }
560
+ return '';
561
+ }
562
+
563
+ _generateBashCompletion() {
564
+ let completion = `#!/usr/bin/env bash
565
+
566
+ _${this.scriptName}_completions() {
567
+ local cur prev opts
568
+ COMPREPLY=()
569
+ cur="\${COMP_WORDS[COMP_CWORD]}"
570
+ prev="\${COMP_WORDS[COMP_CWORD-1]}"
571
+
572
+ opts=""
573
+
574
+ OPTIONS="`;
575
+
576
+ for (const [name, opt] of Object.entries(this.options)) {
577
+ if (!opt.hidden) {
578
+ completion += `--${name} `;
579
+ if (opt.alias && opt.alias.length > 0) {
580
+ const aliases = Array.isArray(opt.alias) ? opt.alias : [opt.alias];
581
+ for (const alias of aliases) {
582
+ completion += `-${alias} `;
583
+ }
584
+ }
585
+ }
586
+ }
587
+
588
+ completion += `"
589
+
590
+ COMMANDS="`;
591
+
592
+ for (const [name, cmd] of Object.entries(this.commands)) {
593
+ if (!cmd.hidden) {
594
+ completion += `${name} `;
595
+ if (cmd.aliases) {
596
+ completion += `${cmd.aliases.join(' ')} `;
597
+ }
598
+ }
599
+ }
600
+
601
+ completion += `"
602
+
603
+ if [[ "\${cur}" == -* ]]; then
604
+ COMPREPLY=( \$(compgen -W "\${OPTIONS}" -- "\${cur}") )
605
+ else
606
+ COMPREPLY=( \$(compgen -W "\${COMMANDS}" -- "\${cur}") )
607
+ fi
608
+
609
+ return 0
610
+ }
611
+
612
+ complete -F _${this.scriptName}_completions ${this.scriptName}
613
+ `;
614
+
615
+ return completion;
616
+ }
617
+
618
+ _generateZshCompletion() {
619
+ let completion = `#compdef ${this.scriptName}
620
+
621
+ local commands options
622
+
623
+ commands=('`;
624
+
625
+ for (const [name, cmd] of Object.entries(this.commands)) {
626
+ if (!cmd.hidden) {
627
+ completion += `${name}:${cmd.description} `;
628
+ }
629
+ }
630
+
631
+ completion += `')
632
+
633
+ options=('`;
634
+
635
+ for (const [name, opt] of Object.entries(this.options)) {
636
+ if (!opt.hidden) {
637
+ completion += `--${name}[${opt.description}]:`;
638
+ if (opt.choices) {
639
+ completion += ':(' + opt.choices.join(' ') + ') ';
640
+ } else {
641
+ completion += ' ';
642
+ }
643
+ if (opt.alias && opt.alias.length > 0) {
644
+ const aliases = Array.isArray(opt.alias) ? opt.alias : [opt.alias];
645
+ for (const alias of aliases) {
646
+ completion += `-${alias}[${opt.description}]: `;
647
+ }
648
+ }
649
+ }
650
+ }
651
+
652
+ completion += `')
653
+
654
+ _arguments \${options[@]} \\
655
+ '1:command:\${commands}' \\
656
+ '*::args:_normal'
657
+ `;
658
+
659
+ return completion;
660
+ }
661
+
662
+ _generateFishCompletion() {
663
+ let completion = `#!/usr/bin/env fish
664
+
665
+ complete -c ${this.scriptName} `;
666
+
667
+ for (const [name, opt] of Object.entries(this.options)) {
668
+ if (!opt.hidden) {
669
+ completion += `-l ${name} -d "${opt.description}" `;
670
+ if (opt.alias && opt.alias.length > 0) {
671
+ const aliases = Array.isArray(opt.alias) ? opt.alias : [opt.alias];
672
+ for (const alias of aliases) {
673
+ completion += `-s ${alias} `;
674
+ }
675
+ }
676
+ }
677
+ }
678
+
679
+ completion += '\n\n';
680
+
681
+ for (const [name, cmd] of Object.entries(this.commands)) {
682
+ if (!cmd.hidden) {
683
+ completion += `complete -c ${this.scriptName} -a "${name}" -d "${cmd.description}"\n`;
684
+ }
685
+ }
686
+
687
+ return completion;
688
+ }
689
+
539
690
  getHelp() {
540
691
  let help = `\u001b[36m${this.scriptName}\u001b[0m`;
541
692
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vexify-org/yaggs",
3
- "version": "5.3.0",
3
+ "version": "6.1.0",
4
4
  "description": "A powerful CLI argument parser, better than yargs",
5
5
  "main": "index.js",
6
6
  "scripts": {