text: !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("colorette")):"function"==typeof define&&define.amd?define(["colorette"],e):(t="undefined"!=typeof globalThis?globalThis:t||self).index=e(t.colorette)}(this,(function(t){"use strict";function e(t){for(const e in t)return!1;return!0}function n(t,e){const n=[];let r=-e.length,s=0;if(!t)return n;for(;s<t.length;)t[s]==e?(n.push(t.substring(r+e.length,s)),r=s,s+=e.length):e?s++:n.push(t[s++]);return e&&n.push(t.substring(r+e.length,t.length)),n}const r=e=>`Found argument '${t.yellow(`-${e}`)}' which wasn't expected, or isn't valid in this context.`,s=e=>`Empty argument '${t.yellow(e)}' which wasn't expected.`,o=(e,n)=>`Invalid arugments for flag '${t.yellow(e)}', ${n}`,a=`Invalid formating flag, need to be '${t.yellow("flag")}=${t.yellow("value")}(${t.yellow(",value...")})'`,i=`need a valid boolean, choice are '${t.italic(t.yellow("true"))}' or '${t.italic(t.yellow("false"))}'`,l=`need a valid '${t.italic(t.yellow("number"))}'.`,h=`need a valid '${t.italic(t.yellow("date"))}'`;return class{constructor(t,e,n={}){this.name=t,this.description=e,this.commands={},this.arguments={},this.errors=[],this.argv=[],this.options=Object.assign({defaultArg:!0},n),this.options.defaultArg&&(this.addArgument("help",{alias:"h",description:"Prints help information",call({parser:t}){t.usage()}}),this.options.version&&this.addArgument("version",{alias:"v",description:"Print version info and exit",call({options:t,name:e}){console.info(`${e} ${t.version}`)}}))}checkArguments(e,n){if(n.name in this.arguments)throw new Error(`duplicate options '${n.name}'`);if(n.name.length<2)throw new Error(`name arguments '${n.name}' need to be upper than one char`);if(this.options.inline&&function(t,e){for(let n=0;n<t.length;n++)if(t[n]===e)return!0;return!1}(n.name,"="))throw new Error(`name arguments '${n.name}' can't contains '${t.yellow("=")}' in here name`);n.params?n.params.forEach((t=>{if(void 0===t.type)throw new Error(`arguments '${n.name}' not have 'type'`)})):n.params=[];const r=n.alias;if(r){if(void 0!==e[r])throw new Error(`duplicate alias options '${r}`);if(1!=r.length)throw new Error(`alias options '${r}' need to be only one char`);e[r]=n}}addArgument(t,e={}){const n=e;n.name=t,this.checkArguments(this.arguments,n),this.arguments[t]=n}addCommand(t,e,n={}){if(t in this.commands)throw new Error(`Command '${t}' already set`);if(t.length<=1)throw new Error(`Command '${t}' need to be more length`);const r=n;if(r.name=t,r.description=e,n.arguments){const t=Object.assign({},r.arguments);for(const e in n.arguments)r.arguments[e].name=e,this.checkArguments(t,r.arguments[e]);r.arguments=t}else r.arguments={};this.commands[t]=r}checkDefault(e){if(void 0!==e.default)return e.default;throw new Error((n=e.type,`need '${t.yellow(n.constructor.name.toLowerCase())}' arguments.`));var n}convertValue(t,e,n){if(e.validator)return e.validator(n,t);if(e.type==Number){const t=Number(n);if(Number.isNaN(t))throw new Error(l);return t}if(e.type===Boolean)switch(n){case"true":case"yes":return!0;case"false":case"no":return!1;default:throw new Error(i)}else if(e.type==Date){const t=new Date(n);if(isNaN(t.getTime()))throw new Error(h);return t}return n}advFlagInline(e,r,s,i,l){const h=s[l[0]],c=[];if(i[h.name]=c,h.alias&&(i[h.alias]=c),2!=l.length)return this.addError(a,r,2),r+1;const m=n(l[1],",");if(m.length>h.params.length)return this.addError((d=h.name,u=h.params.length,`Need ${t.yellow(u)} arguments after flag '${t.yellow(`--${d}`)}'.`),r,l[0].length+2),r+1;var d,u;let g=2+h.name.length+1;for(let t=0;t<h.params.length;t++){const e=h.params[t],n=t>=m.length;try{n?c.push(this.checkDefault(e)):c.push(this.convertValue(c,e,m[t]))}catch(e){this.addError(o(h.name,e.message),r,g,n?0:m[t].length)}finally{n||(g+=m[t].length+1)}}return r+1}advFlag(t,e,n,r,s){const a=[];if(n[r.name]=a,r.alias&&(n[r.alias]=a),!r.params.length)return e;let i=0;for(let n=0;n<r.params.length;n++){const l=r.params[n],h=e+n>=t.length,c=!h&&l.type!==Number&&"-"===t[e+n][0]||!1;try{c?(i++,a.push(this.checkDefault(l))):h?a.push(this.checkDefault(l)):a.push(this.convertValue(a,l,t[e+n]))}catch(t){return this.addError(o(s,t.message),e),e}}return e+r.params.length-i}parseMulti(t,e,o,a,i){if(2==o.length)return this.addError(s("--"),i,2,o.length-2),i+1;const l=o.substring(2),h=this.options.inline?n(l,"="):[l];return void 0===a[h[0]]?(this.addError(r(l),i,2,o.length-2),i+1):this.options.inline&&1!=h.length?this.advFlagInline(e,i,a,t,h):this.advFlag(e,i+1,t,a[l],l)}parseSimple(t,e,n,s,o){let a=o;for(let i=1;i<=n.length-1;i++){const l=s[n[i]];void 0!==l?a=this.advFlag(e,a+1,t,l,n[i]):this.addError(r(n[i]),o,i,1)}return Math.max(o+1,a)}parseFlags(t,e,n=0){const r={},o=[];for(;n<t.length&&t[n]!==this.options.stopFlags;){const a=t[n];"-"!=a[0]?(o.push(a),n++):"-"==a[1]?n=this.parseMulti(r,t,a,e,n):1!=a.length?n=this.parseSimple(r,t,a,e,n):this.addError(s("-"),n++)}for(t[n]===this.options.stopFlags&&n++;n<t.length;)o.push(t[n++]);return[r,o]}parse(n){if(this.argv=n,0==n.length)return this.usage(),!0;let r,s=this.arguments;if(!e(this.commands)){if(!(n[0]in this.commands))return this.addError("-"==n[0][0]?(e=>`first argument '${t.yellow(e)}' need to be a command, not flags`)(n[0]):(e=>`No Such command '${t.yellow(e)}'`)(n[0]),0),!1;r=this.commands[n[0]],s=Object.assign(Object.assign({},s),r.arguments),s.help&&(s.help.call=({parser:t,cmd:e})=>{e&&t.commandUsage(e)})}const[o,a]=this.parseFlags(n,s,r?1:0),i=this.createContext(o,a,r);if(this.errors.length)return!1;const l=this.getCall(o,s,r);return l&&l(i),!0}getCall(t,e,n){for(const n in t){const t=e[n].call;if(t)return t}if(n&&n.call)return n.call}get context(){if(void 0===this.ctx)throw new Error("You need to call 'parse' before access context");return this.ctx}createContext(t,e,n){return this.ctx={flags:t,anyArgs:e,parser:this,name:this.name,description:this.description,options:this.options,argv:this.argv},n&&(this.ctx.cmd=n),this.ctx}addError(t,e,n,r){const s=this.errors.find((t=>t.argvi===e&&t.start===n&&t.end===r));s?s.text.push(t):this.errors.push({text:[t],argvi:e,start:n,end:r})}printError(e){const n=`${this.name} ${this.argv.join(" ")}\n`,r=void 0===e?this.errors:[...this.errors].splice(0,e);let s="";if(r.forEach((e=>{var r,o,a;s+=`${t.red(t.bold("error"))}: ${e.text.join("\n"+" ".repeat(7))}\n`;let i=e.argvi+this.name.length+1;for(let t=0;t<e.argvi;t++)i+=this.argv[t].length;s+=`${n}${" ".repeat(i)}`;const l=(null===(r=this.argv[e.argvi])||void 0===r?void 0:r.length)||1;e.start=null!==(o=e.start)&&void 0!==o?o:0,e.end=null!==(a=e.end)&&void 0!==a?a:l-e.start,s+=t.red("~".repeat(e.start))+t.red(t.bold("^".repeat(e.end)));const h=l-(e.start+e.end);h>0&&(s+=t.red("~".repeat(h))),s+="\n"})),this.errors.length>=5){const e=this.errors.reduce(((t,e)=>e.text.length+t),0);s+=`total errors: ${t.red(t.bold(e))}`}console.error(s.trim())}formatOptions(e,n="Options:"){var r;const s={};for(const t in e)s[e[t].name]=e[t];const o={};let a=0;for(const t in s){const e=s[t];o[t]=t.length,e.params&&e.params.length&&(o[t]+=e.params.reduce(((t,e)=>t+e.type.name.length+3),0)),a=Math.max(a,o[t])}let i=n+"\n";for(const e in s){const n=s[e];i+=`${n.alias?`-${n.alias}, `:"    "}--${e}`,n.params&&(i+=n.params.reduce(((t,e)=>`${t}<${e.type.constructor.name.toLowerCase()}> `),"")),i+=`${" ".repeat(a-o[e]+1)}${null!==(r=n.description)&&void 0!==r?r:t.italic("no information.")}\n`}return i}formatCommands(t){let e=0;for(const n in t)e=Math.max(e,n.length);let n="Commands:\n";for(const r in t)n+=`  ${r}${" ".repeat(e-r.length)} ${t[r].description}\n`;return n}commandUsage(t){var e;if("string"==typeof t){const e=this.commands[t];if(!e)throw new Error(`not found '${t}' command.`);t=e}let n="";n+=`Usage: ${this.name} ${t.name} `,n+=null!==(e=this.options.info)&&void 0!==e?e:"[OPTIONS]\n\n",n+=t.description,this.arguments&&(n+="\n\n"+this.formatOptions(this.arguments,"Global options:")),t.arguments&&(n+="\n\n"+this.formatOptions(t.arguments,"Command options:")),this.options.footer&&(n+=`\n\n${this.options.footer}`),console.log(n.trim())}usage(){var t;let n="";n+=`Usage: ${this.name} `,n+=null!==(t=this.options.info)&&void 0!==t?t:(e(this.commands)?"":"COMMAND")+" [OPTIONS]\n\n",n+=this.description,e(this.arguments)||(n+="\n\n"+this.formatOptions(this.arguments)),e(this.commands)||(n+="\n\n"+this.formatCommands(this.commands)),this.options.footer&&(n+=`\n\n${this.options.footer}`),console.info(n.trim())}}}));
  