@hammerhq/cli-tool
Version:
✨ A simple CLI commander system
23 lines (18 loc) • 1.3 kB
JavaScript
;
var hargs = require('@hammerhq/hargs');
require('colors');
var r=class{commands=[];commandExecuted=!1;handleHelp(e){return console.info(` ${e.name.blue} - Help:
${"-".repeat(e.name.length+9)}
${"Command Name".green}: ${e.name.yellow}
${"Usage".green}: ${e.name.yellow} ${e.usage.yellow}
${"Aliases".green}: ${e.aliases.map(t=>t.yellow).join(", ")}
${"Category".green}: ${e.category.yellow}
${"Description".green}: ${e.description.yellow}
${"Example".green}:
${e.example.map(t=>`${e.name.blue} ${t.yellow}`).join(`
`)}`),this}createCommand(e,t){if(e.example.unshift("--help"),this.commands.push(e),e.name!==process.argv[2]&&!e.aliases.includes(process.argv[2]))return this;if(this.commandExecuted)return this;this.commandExecuted=!0;let n=hargs.hargs(e.argDefinitions,process.argv.slice(3));return n["--help"]||n._unknown.includes("--help")||n._unknown.includes("-h")?this.handleHelp(e):(t(e.name,n),this)}help(){if(this.commandExecuted)return;let e={};for(let n of this.commands){let s=n.category;e[s]||(e[s]=[]),e[s].push(n);}let t="";for(let n in e){let s=(n.charAt(0).toUpperCase()+n.slice(1)).green,i=e[n].map(o=>` ${o.name.yellow} ${o.usage.yellow}`).join(`
`);t+=` ${s} Commands:
${"-".repeat(s.length+1)}
${i}
`;}console.info(t);}},h=new r;
exports.tool = h;