@clerc/core 0.26.0 → 0.27.1
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.
- package/dist/index.d.ts +9 -4
- package/dist/index.js +1 -1
- package/package.json +5 -5
- package/dist/index.mjs +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -261,11 +261,15 @@ type CommandAlias<N extends string | RootType = string, O extends CommandOptions
|
|
|
261
261
|
type CommandWithHandler<N extends string | RootType = string, O extends CommandOptions = CommandOptions> = Command<N, O> & {
|
|
262
262
|
handler?: HandlerInCommand<HandlerContext<Record<N, Command<N, O>> & Record<never, never>, N>>;
|
|
263
263
|
};
|
|
264
|
-
type StripBrackets<Parameter extends string> = (Parameter extends `<${infer ParameterName}>` | `[${infer ParameterName}]` ? (ParameterName extends `${infer SpreadName}...` ? SpreadName : ParameterName) : never);
|
|
265
|
-
type ParameterType<Parameter extends string> = (Parameter extends `<${infer _ParameterName}...>` | `[${infer _ParameterName}...]` ? string[] : Parameter extends `<${infer _ParameterName}>` ? string : Parameter extends `[${infer _ParameterName}]` ? string | undefined : never);
|
|
266
264
|
type CommandRecord = Dict<Command> & {
|
|
267
265
|
[Root]?: Command;
|
|
268
266
|
};
|
|
267
|
+
interface ParseOptions {
|
|
268
|
+
argv?: string[];
|
|
269
|
+
run?: boolean;
|
|
270
|
+
}
|
|
271
|
+
type StripBrackets<Parameter extends string> = (Parameter extends `<${infer ParameterName}>` | `[${infer ParameterName}]` ? (ParameterName extends `${infer SpreadName}...` ? SpreadName : ParameterName) : never);
|
|
272
|
+
type ParameterType<Parameter extends string> = (Parameter extends `<${infer _ParameterName}...>` | `[${infer _ParameterName}...]` ? string[] : Parameter extends `<${infer _ParameterName}>` ? string : Parameter extends `[${infer _ParameterName}]` ? string | undefined : never);
|
|
269
273
|
type MakeEventMap<T extends CommandRecord> = {
|
|
270
274
|
[K in keyof T]: [InspectorContext];
|
|
271
275
|
};
|
|
@@ -453,7 +457,8 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
453
457
|
* .parse(process.argv.slice(2)) // Optional
|
|
454
458
|
* ```
|
|
455
459
|
*/
|
|
456
|
-
parse(
|
|
460
|
+
parse(optionsOrArgv?: string[] | ParseOptions): this;
|
|
461
|
+
runMatchedCommand(): this;
|
|
457
462
|
}
|
|
458
463
|
|
|
459
464
|
type MaybeArray<T> = T | T[];
|
|
@@ -504,4 +509,4 @@ declare const isInvalidName: (name: CommandType) => boolean;
|
|
|
504
509
|
declare const withBrackets: (s: string, isOptional?: boolean) => string;
|
|
505
510
|
declare const formatCommandName: (name: string | string[] | RootType) => string;
|
|
506
511
|
|
|
507
|
-
export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandRecord, CommandType, CommandWithHandler, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, Handler, HandlerContext, HandlerInCommand, Inspector, InspectorContext, InspectorFn, InspectorObject, InvalidCommandNameError, MakeEventMap, NameNotSetError, NoCommandGivenError, NoSuchCommandError, Plugin, PossibleInputKind, Root, RootType, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, formatCommandName, isInvalidName, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent, withBrackets };
|
|
512
|
+
export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandRecord, CommandType, CommandWithHandler, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, Handler, HandlerContext, HandlerInCommand, Inspector, InspectorContext, InspectorFn, InspectorObject, InvalidCommandNameError, MakeEventMap, NameNotSetError, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, PossibleInputKind, Root, RootType, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, formatCommandName, isInvalidName, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent, withBrackets };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class
|
|
1
|
+
class me{constructor(){this.listenerMap={},this.wildcardListeners=new Set}on(t,s){return t==="*"?(this.wildcardListeners.add(s),this):(this.listenerMap[t]||(this.listenerMap[t]=new Set),this.listenerMap[t].add(s),this)}emit(t,...s){return this.listenerMap[t]&&(this.wildcardListeners.forEach(r=>r(t,...s)),this.listenerMap[t].forEach(r=>r(...s))),this}off(t,s){var r,n;return t==="**"?(this.listenerMap={},this.wildcardListeners.clear(),this):t==="*"?(s?this.wildcardListeners.delete(s):this.wildcardListeners.clear(),this):(s?(r=this.listenerMap[t])==null||r.delete(s):(n=this.listenerMap[t])==null||n.clear(),this)}}const ge="known-flag",we="unknown-flag",ve="argument",{stringify:S}=JSON,Ee=/\B([A-Z])/g,ye=e=>e.replace(Ee,"-$1").toLowerCase(),{hasOwnProperty:_e}=Object.prototype,k=(e,t)=>_e.call(e,t),Ce=e=>Array.isArray(e),H=e=>typeof e=="function"?[e,!1]:Ce(e)?[e[0],!0]:H(e.type),Me=(e,t)=>e===Boolean?t!=="false":t,Ne=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),$e=/[\s.:=]/,Se=e=>{const t=`Flag name ${S(e)}`;if(e.length===0)throw new Error(`${t} cannot be empty`);if(e.length===1)throw new Error(`${t} must be longer than a character`);const s=e.match($e);if(s)throw new Error(`${t} cannot contain ${S(s==null?void 0:s[0])}`)},ke=e=>{const t={},s=(r,n)=>{if(k(t,r))throw new Error(`Duplicate flags named ${S(r)}`);t[r]=n};for(const r in e){if(!k(e,r))continue;Se(r);const n=e[r],o=[[],...H(n),n];s(r,o);const a=ye(r);if(r!==a&&s(a,o),"alias"in n&&typeof n.alias=="string"){const{alias:i}=n,c=`Flag alias ${S(i)} for flag ${S(r)}`;if(i.length===0)throw new Error(`${c} cannot be empty`);if(i.length>1)throw new Error(`${c} must be a single character`);s(i,o)}}return t},Ae=(e,t)=>{const s={};for(const r in e){if(!k(e,r))continue;const[n,,o,a]=t[r];if(n.length===0&&"default"in a){let{default:i}=a;typeof i=="function"&&(i=i()),s[r]=i}else s[r]=o?n:n.pop()}return s},I="--",be=/[.:=]/,xe=/^-{1,2}\w/,Ie=e=>{if(!xe.test(e))return;const t=!e.startsWith(I);let s=e.slice(t?1:2),r;const n=s.match(be);if(n){const{index:o}=n;r=s.slice(o+1),s=s.slice(0,o)}return[s,r,t]},Oe=(e,{onFlag:t,onArgument:s})=>{let r;const n=(o,a)=>{if(typeof r!="function")return!0;r(o,a),r=void 0};for(let o=0;o<e.length;o+=1){const a=e[o];if(a===I){n();const c=e.slice(o+1);s==null||s(c,[o],!0);break}const i=Ie(a);if(i){if(n(),!t)continue;const[c,l,f]=i;if(f)for(let p=0;p<c.length;p+=1){n();const m=p===c.length-1;r=t(c[p],m?l:void 0,[o,p+1,m])}else r=t(c,l,[o])}else n(a,[o])&&(s==null||s([a],[o]))}n()},We=(e,t)=>{for(const[s,r,n]of t.reverse()){if(r){const o=e[s];let a=o.slice(0,r);if(n||(a+=o.slice(r+1)),a!=="-"){e[s]=a;continue}}e.splice(s,1)}},Re=(e,t=process.argv.slice(2),{ignore:s}={})=>{const r=[],n=ke(e),o={},a=[];return a[I]=[],Oe(t,{onFlag(i,c,l){const f=k(n,i);if(!(s!=null&&s(f?ge:we,i,c))){if(f){const[p,m]=n[i],N=Me(m,c),$=(w,g)=>{r.push(l),g&&r.push(g),p.push(Ne(m,w||""))};return N===void 0?$:$(N)}k(o,i)||(o[i]=[]),o[i].push(c===void 0?!0:c),r.push(l)}},onArgument(i,c,l){s!=null&&s(ve,t[c[0]])||(a.push(...i),l?(a[I]=i,t.splice(c[0])):r.push(c))}}),We(t,r),{flags:Ae(e,n),unknownFlags:o,_:a}},L=e=>Array.isArray(e)?e:[e],Le=e=>e.replace(/-([a-z])/g,(t,s)=>s.toUpperCase()),Fe=(e,t)=>t.length!==e.length?!1:e.every((s,r)=>s===t[r]),J=(e,t)=>t.length>e.length?!1:Fe(e.slice(0,t.length),t);class U extends Error{constructor(t){super(`Command "${t}" exists.`),this.commandName=t}}class z extends Error{constructor(t){super(`No such command: ${t}`),this.commandName=t}}class K extends Error{constructor(){super("No command given.")}}class Z extends Error{constructor(t,s){super(`Command name ${t} conflicts with ${s}. Maybe caused by alias.`),this.n1=t,this.n2=s}}class Q extends Error{constructor(){super("Name not set.")}}class X extends Error{constructor(){super("Description not set.")}}class Y extends Error{constructor(){super("Version not set.")}}class ee extends Error{constructor(t){super(`Bad name format: ${t}`),this.commandName=t}}const te=typeof Deno!="undefined",Pe=typeof process!="undefined"&&!te,Be=process.versions.electron&&!process.defaultApp;function se(e,t,s){if(s.alias){const r=L(s.alias);for(const n of r){if(n in t)throw new Z(t[n].name,s.name);e.set(typeof n=="symbol"?n:n.split(" "),{...s,__isAlias:!0})}}}function re(e){const t=new Map;e[u]&&(t.set(u,e[u]),se(t,e,e[u]));for(const s of Object.values(e))se(t,e,s),t.set(s.name.split(" "),s);return t}function ne(e,t){if(t===u)return e[u];const s=L(t),r=re(e);let n,o;return r.forEach((a,i)=>{if(i===u){n=r.get(u),o=u;return}J(s,i)&&(!o||o===u||i.length>o.length)&&(n=a,o=i)}),n}function oe(e,t,s=1/0){const r=t===""?[]:Array.isArray(t)?t:t.split(" ");return Object.values(e).filter(n=>{const o=n.name.split(" ");return J(o,r)&&o.length-r.length<=s})}const De=e=>oe(e,"",1);function ie(e){const t=[];for(const s of e){if(s.startsWith("-"))break;t.push(s)}return t}const F=()=>Pe?process.argv.slice(Be?1:2):te?Deno.args:[];function ae(e){const t={pre:[],normal:[],post:[]};for(const r of e){const n=typeof r=="object"?r:{fn:r},{enforce:o,fn:a}=n;o==="post"||o==="pre"?t[o].push(a):t.normal.push(a)}const s=[...t.pre,...t.normal,...t.post];return r=>{return n(0);function n(o){const a=s[o];return a(r(),n.bind(null,o+1))}}}const ce=e=>typeof e=="string"&&(e.startsWith(" ")||e.endsWith(" ")),Te=(e,t)=>t?`[${e}]`:`<${e}>`,je="<Root>",le=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:je,{stringify:C}=JSON;function P(e){const t=[];let s,r;for(const n of e){if(r)throw new Error(`Invalid parameter: Spread parameter ${C(r)} must be last`);const o=n[0],a=n[n.length-1];let i;if(o==="<"&&a===">"&&(i=!0,s))throw new Error(`Invalid parameter: Required parameter ${C(n)} cannot come after optional parameter ${C(s)}`);if(o==="["&&a==="]"&&(i=!1,s=n),i===void 0)throw new Error(`Invalid parameter: ${C(n)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let c=n.slice(1,-1);const l=c.slice(-3)==="...";l&&(r=n,c=c.slice(0,-3)),t.push({name:c,required:i,spread:l})}return t}function B(e,t,s){for(let r=0;r<t.length;r+=1){const{name:n,required:o,spread:a}=t[r],i=Le(n);if(i in e)return new Error(`Invalid parameter: ${C(n)} is used more than once.`);const c=a?s.slice(r):s[r];if(a&&(r=t.length),o&&(!c||a&&c.length===0))return new Error(`Error: Missing required parameter ${C(n)}`);e[i]=c}}var D=(e,t,s)=>{if(!t.has(e))throw TypeError("Cannot "+s)},h=(e,t,s)=>(D(e,t,"read from private field"),s?s.call(e):t.get(e)),d=(e,t,s)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,s)},v=(e,t,s,r)=>(D(e,t,"write to private field"),r?r.call(e,s):t.set(e,s),s),qe=(e,t,s)=>(D(e,t,"access private method"),s),E,y,_,A,b,O,M,W,T,he,j,fe,q,ue;const u=Symbol("Root"),pe=class{constructor(e,t,s){d(this,T),d(this,j),d(this,q),d(this,E,""),d(this,y,""),d(this,_,""),d(this,A,[]),d(this,b,{}),d(this,O,new me),d(this,M,new Set),d(this,W,void 0),v(this,E,e||h(this,E)),v(this,y,t||h(this,y)),v(this,_,s||h(this,_))}get _name(){return h(this,E)}get _description(){return h(this,y)}get _version(){return h(this,_)}get _inspectors(){return h(this,A)}get _commands(){return h(this,b)}static create(e,t,s){return new pe(e,t,s)}name(e){return v(this,E,e),this}description(e){return v(this,y,e),this}version(e){return v(this,_,e),this}command(e,t,s={}){const n=(f=>!(typeof f=="string"||f===u))(e),o=n?e.name:e;if(ce(o))throw new ee(o);const{handler:a=void 0,...i}=n?e:{name:o,description:t,...s},c=[i.name],l=i.alias?L(i.alias):[];i.alias&&c.push(...l);for(const f of c)if(h(this,M).has(f))throw new U(le(f));return h(this,b)[o]=i,h(this,M).add(i.name),l.forEach(f=>h(this,M).add(f)),n&&a&&this.on(e.name,a),this}on(e,t){return h(this,O).on(e,t),this}use(e){return e.setup(this)}inspector(e){return h(this,A).push(e),this}parse(e=F()){const{argv:t,run:s}=Array.isArray(e)?{argv:e,run:!0}:{argv:F(),...e};return v(this,W,t),qe(this,q,ue).call(this),s&&this.runMatchedCommand(),this}runMatchedCommand(){const e=h(this,W);if(!e)throw new Error("cli.parse() must be called.");const t=ie(e),s=t.join(" "),r=()=>ne(h(this,b),t),n=[],o=()=>{n.length=0;const l=r(),f=!!l,p=Re((l==null?void 0:l.flags)||{},[...e]),{_:m,flags:N,unknownFlags:$}=p;let w=!f||l.name===u?m:m.slice(l.name.split(" ").length),g=(l==null?void 0:l.parameters)||[];const R=g.indexOf("--"),G=g.slice(R+1)||[],x=Object.create(null);if(R>-1&&G.length>0){g=g.slice(0,R);const V=m["--"];w=w.slice(0,-V.length||void 0),n.push(B(x,P(g),w)),n.push(B(x,P(G),V))}else n.push(B(x,P(g),w));const de={...N,...$};return{name:l==null?void 0:l.name,called:t.length===0&&(l==null?void 0:l.name)?u:s,resolved:f,hasRootOrAlias:h(this,T,he),hasRoot:h(this,j,fe),raw:{...p,parameters:w,mergedFlags:de},parameters:x,flags:N,unknownFlags:$,cli:this}},a={enforce:"post",fn:()=>{const l=r(),f=o(),p=n.filter(Boolean);if(p.length>0)throw p[0];if(!l)throw s?new z(s):new K;h(this,O).emit(l.name,f)}},i=[...h(this,A),a];return ae(i)(o),this}};let Ge=pe;E=new WeakMap,y=new WeakMap,_=new WeakMap,A=new WeakMap,b=new WeakMap,O=new WeakMap,M=new WeakMap,W=new WeakMap,T=new WeakSet,he=function(){return h(this,M).has(u)},j=new WeakSet,fe=function(){return Object.prototype.hasOwnProperty.call(this._commands,u)},q=new WeakSet,ue=function(){if(!h(this,E))throw new Q;if(!h(this,y))throw new X;if(!h(this,_))throw new Y};const Ve=e=>e,He=(e,t,s)=>s,Je=(e,t)=>t,Ue=(e,t)=>({...e,handler:t});export{Ge as Clerc,U as CommandExistsError,Z as CommandNameConflictError,X as DescriptionNotSetError,ee as InvalidCommandNameError,Q as NameNotSetError,K as NoCommandGivenError,z as NoSuchCommandError,u as Root,Y as VersionNotSetError,ae as compose,Ue as defineCommand,He as defineHandler,Je as defineInspector,Ve as definePlugin,le as formatCommandName,ce as isInvalidName,F as resolveArgv,ne as resolveCommand,re as resolveFlattenCommands,ie as resolveParametersBeforeFlag,De as resolveRootCommands,oe as resolveSubcommandsByParent,Te as withBrackets};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.1",
|
|
4
4
|
"author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
|
|
5
5
|
"description": "Clerc core",
|
|
6
6
|
"keywords": [
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
"exports": {
|
|
27
27
|
".": {
|
|
28
28
|
"types": "./dist/index.d.ts",
|
|
29
|
-
"import": "./dist/index.
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
30
|
}
|
|
31
31
|
},
|
|
32
32
|
"main": "dist/index.js",
|
|
33
|
-
"module": "dist/index.
|
|
33
|
+
"module": "dist/index.js",
|
|
34
34
|
"types": "dist/index.d.ts",
|
|
35
35
|
"typesVersions": {
|
|
36
36
|
"*": {
|
|
@@ -47,11 +47,11 @@
|
|
|
47
47
|
"access": "public"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"is-platform": "^0.
|
|
50
|
+
"is-platform": "^1.0.0",
|
|
51
51
|
"lite-emit": "^1.4.0",
|
|
52
52
|
"type-fest": "^3.5.3",
|
|
53
53
|
"type-flag": "^3.0.0",
|
|
54
|
-
"@clerc/utils": "0.
|
|
54
|
+
"@clerc/utils": "0.27.1"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"build": "puild --minify",
|
package/dist/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
class fe{constructor(){this.listenerMap={},this.wildcardListeners=new Set}on(t,s){return t==="*"?(this.wildcardListeners.add(s),this):(this.listenerMap[t]||(this.listenerMap[t]=new Set),this.listenerMap[t].add(s),this)}emit(t,...s){return this.listenerMap[t]&&(this.wildcardListeners.forEach(r=>r(t,...s)),this.listenerMap[t].forEach(r=>r(...s))),this}off(t,s){var r,n;return t==="**"?(this.listenerMap={},this.wildcardListeners.clear(),this):t==="*"?(s?this.wildcardListeners.delete(s):this.wildcardListeners.clear(),this):(s?(r=this.listenerMap[t])==null||r.delete(s):(n=this.listenerMap[t])==null||n.clear(),this)}}const ue="known-flag",pe="unknown-flag",de="argument",{stringify:A}=JSON,me=/\B([A-Z])/g,ge=e=>e.replace(me,"-$1").toLowerCase(),{hasOwnProperty:we}=Object.prototype,k=(e,t)=>we.call(e,t),ve=e=>Array.isArray(e),T=e=>typeof e=="function"?[e,!1]:ve(e)?[e[0],!0]:T(e.type),ye=(e,t)=>e===Boolean?t!=="false":t,Ee=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),_e=/[\s.:=]/,Ce=e=>{const t=`Flag name ${A(e)}`;if(e.length===0)throw new Error(`${t} cannot be empty`);if(e.length===1)throw new Error(`${t} must be longer than a character`);const s=e.match(_e);if(s)throw new Error(`${t} cannot contain ${A(s==null?void 0:s[0])}`)},Ne=e=>{const t={},s=(r,n)=>{if(k(t,r))throw new Error(`Duplicate flags named ${A(r)}`);t[r]=n};for(const r in e){if(!k(e,r))continue;Ce(r);const n=e[r],o=[[],...T(n),n];s(r,o);const a=ge(r);if(r!==a&&s(a,o),"alias"in n&&typeof n.alias=="string"){const{alias:i}=n,c=`Flag alias ${A(i)} for flag ${A(r)}`;if(i.length===0)throw new Error(`${c} cannot be empty`);if(i.length>1)throw new Error(`${c} must be a single character`);s(i,o)}}return t},$e=(e,t)=>{const s={};for(const r in e){if(!k(e,r))continue;const[n,,o,a]=t[r];if(n.length===0&&"default"in a){let{default:i}=a;typeof i=="function"&&(i=i()),s[r]=i}else s[r]=o?n:n.pop()}return s},O="--",Me=/[.:=]/,Ae=/^-{1,2}\w/,ke=e=>{if(!Ae.test(e))return;const t=!e.startsWith(O);let s=e.slice(t?1:2),r;const n=s.match(Me);if(n){const{index:o}=n;r=s.slice(o+1),s=s.slice(0,o)}return[s,r,t]},be=(e,{onFlag:t,onArgument:s})=>{let r;const n=(o,a)=>{if(typeof r!="function")return!0;r(o,a),r=void 0};for(let o=0;o<e.length;o+=1){const a=e[o];if(a===O){n();const c=e.slice(o+1);s==null||s(c,[o],!0);break}const i=ke(a);if(i){if(n(),!t)continue;const[c,l,f]=i;if(f)for(let p=0;p<c.length;p+=1){n();const d=p===c.length-1;r=t(c[p],d?l:void 0,[o,p+1,d])}else r=t(c,l,[o])}else n(a,[o])&&(s==null||s([a],[o]))}n()},Se=(e,t)=>{for(const[s,r,n]of t.reverse()){if(r){const o=e[s];let a=o.slice(0,r);if(n||(a+=o.slice(r+1)),a!=="-"){e[s]=a;continue}}e.splice(s,1)}},xe=(e,t=process.argv.slice(2),{ignore:s}={})=>{const r=[],n=Ne(e),o={},a=[];return a[O]=[],be(t,{onFlag(i,c,l){const f=k(n,i);if(!(s!=null&&s(f?ue:pe,i,c))){if(f){const[p,d]=n[i],$=ye(d,c),M=(w,m)=>{r.push(l),m&&r.push(m),p.push(Ee(d,w||""))};return $===void 0?M:M($)}k(o,i)||(o[i]=[]),o[i].push(c===void 0?!0:c),r.push(l)}},onArgument(i,c,l){s!=null&&s(de,t[c[0]])||(a.push(...i),l?(a[O]=i,t.splice(c[0])):r.push(c))}}),Se(t,r),{flags:$e(e,n),unknownFlags:o,_:a}},W=e=>Array.isArray(e)?e:[e],Oe=e=>e.replace(/-([a-z])/g,(t,s)=>s.toUpperCase()),Ie=(e,t)=>t.length!==e.length?!1:e.every((s,r)=>s===t[r]),q=(e,t)=>t.length>e.length?!1:Ie(e.slice(0,t.length),t);class G extends Error{constructor(t){super(`Command "${t}" exists.`),this.commandName=t}}class V extends Error{constructor(t){super(`No such command: ${t}`),this.commandName=t}}class H extends Error{constructor(){super("No command given.")}}class J extends Error{constructor(t,s){super(`Command name ${t} conflicts with ${s}. Maybe caused by alias.`),this.n1=t,this.n2=s}}class U extends Error{constructor(){super("Name not set.")}}class z extends Error{constructor(){super("Description not set.")}}class K extends Error{constructor(){super("Version not set.")}}class Z extends Error{constructor(t){super(`Bad name format: ${t}`),this.commandName=t}}function Re(){return typeof process!="undefined"}function We(){return typeof Deno!="undefined"}function Q(e,t,s){if(s.alias){const r=W(s.alias);for(const n of r){if(n in t)throw new J(t[n].name,s.name);e.set(typeof n=="symbol"?n:n.split(" "),{...s,__isAlias:!0})}}}function X(e){const t=new Map;e[u]&&(t.set(u,e[u]),Q(t,e,e[u]));for(const s of Object.values(e))Q(t,e,s),t.set(s.name.split(" "),s);return t}function Y(e,t){if(t===u)return e[u];const s=W(t),r=X(e);let n,o;return r.forEach((a,i)=>{if(i===u){n=r.get(u),o=u;return}q(s,i)&&(!o||o===u||i.length>o.length)&&(n=a,o=i)}),n}function ee(e,t,s=1/0){const r=t===""?[]:Array.isArray(t)?t:t.split(" ");return Object.values(e).filter(n=>{const o=n.name.split(" ");return q(o,r)&&o.length-r.length<=s})}const Fe=e=>ee(e,"",1);function te(e){const t=[];for(const s of e){if(s.startsWith("-"))break;t.push(s)}return t}const se=()=>Re()?process.argv.slice(2):We()?Deno.args:[];function re(e){const t={pre:[],normal:[],post:[]};for(const r of e){const n=typeof r=="object"?r:{fn:r},{enforce:o,fn:a}=n;o==="post"||o==="pre"?t[o].push(a):t.normal.push(a)}const s=[...t.pre,...t.normal,...t.post];return r=>{return n(0);function n(o){const a=s[o];return a(r(),n.bind(null,o+1))}}}const ne=e=>typeof e=="string"&&(e.startsWith(" ")||e.endsWith(" ")),Le=(e,t)=>t?`[${e}]`:`<${e}>`,Pe="<Root>",oe=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:Pe,{stringify:_}=JSON;function F(e){const t=[];let s,r;for(const n of e){if(r)throw new Error(`Invalid parameter: Spread parameter ${_(r)} must be last`);const o=n[0],a=n[n.length-1];let i;if(o==="<"&&a===">"&&(i=!0,s))throw new Error(`Invalid parameter: Required parameter ${_(n)} cannot come after optional parameter ${_(s)}`);if(o==="["&&a==="]"&&(i=!1,s=n),i===void 0)throw new Error(`Invalid parameter: ${_(n)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let c=n.slice(1,-1);const l=c.slice(-3)==="...";l&&(r=n,c=c.slice(0,-3)),t.push({name:c,required:i,spread:l})}return t}function L(e,t,s){for(let r=0;r<t.length;r+=1){const{name:n,required:o,spread:a}=t[r],i=Oe(n);if(i in e)return new Error(`Invalid parameter: ${_(n)} is used more than once.`);const c=a?s.slice(r):s[r];if(a&&(r=t.length),o&&(!c||a&&c.length===0))return new Error(`Error: Missing required parameter ${_(n)}`);e[i]=c}}var ie=(e,t,s)=>{if(!t.has(e))throw TypeError("Cannot "+s)},h=(e,t,s)=>(ie(e,t,"read from private field"),s?s.call(e):t.get(e)),g=(e,t,s)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,s)},C=(e,t,s,r)=>(ie(e,t,"write to private field"),r?r.call(e,s):t.set(e,s),s),v,y,E,b,S,I,N,P,ae,B,ce;const u=Symbol("Root"),le=class{constructor(e,t,s){g(this,P),g(this,B),g(this,v,""),g(this,y,""),g(this,E,""),g(this,b,[]),g(this,S,{}),g(this,I,new fe),g(this,N,new Set),C(this,v,e||h(this,v)),C(this,y,t||h(this,y)),C(this,E,s||h(this,E))}get _name(){return h(this,v)}get _description(){return h(this,y)}get _version(){return h(this,E)}get _inspectors(){return h(this,b)}get _commands(){return h(this,S)}static create(e,t,s){return new le(e,t,s)}name(e){return C(this,v,e),this}description(e){return C(this,y,e),this}version(e){return C(this,E,e),this}command(e,t,s={}){const n=(f=>!(typeof f=="string"||f===u))(e),o=n?e.name:e;if(ne(o))throw new Z(o);const{handler:a=void 0,...i}=n?e:{name:o,description:t,...s},c=[i.name],l=i.alias?W(i.alias):[];i.alias&&c.push(...l);for(const f of c)if(h(this,N).has(f))throw new G(oe(f));return h(this,S)[o]=i,h(this,N).add(i.name),l.forEach(f=>h(this,N).add(f)),n&&a&&this.on(e.name,a),this}on(e,t){return h(this,I).on(e,t),this}use(e){return e.setup(this)}inspector(e){return h(this,b).push(e),this}parse(e=se()){if(!h(this,v))throw new U;if(!h(this,y))throw new z;if(!h(this,E))throw new K;const t=te(e),s=t.join(" "),r=()=>Y(h(this,S),t),n=[],o=()=>{n.length=0;const l=r(),f=!!l,p=xe((l==null?void 0:l.flags)||{},[...e]),{_:d,flags:$,unknownFlags:M}=p;let w=!f||l.name===u?d:d.slice(l.name.split(" ").length),m=(l==null?void 0:l.parameters)||[];const R=m.indexOf("--"),D=m.slice(R+1)||[],x=Object.create(null);if(R>-1&&D.length>0){m=m.slice(0,R);const j=d["--"];w=w.slice(0,-j.length||void 0),n.push(L(x,F(m),w)),n.push(L(x,F(D),j))}else n.push(L(x,F(m),w));const he={...$,...M};return{name:l==null?void 0:l.name,called:t.length===0&&(l==null?void 0:l.name)?u:s,resolved:f,hasRootOrAlias:h(this,P,ae),hasRoot:h(this,B,ce),raw:{...p,parameters:w,mergedFlags:he},parameters:x,flags:$,unknownFlags:M,cli:this}},a={enforce:"post",fn:()=>{const l=r(),f=o(),p=n.filter(Boolean);if(p.length>0)throw p[0];if(!l)throw s?new V(s):new H;h(this,I).emit(l.name,f)}},i=[...h(this,b),a];return re(i)(o),this}};let Be=le;v=new WeakMap,y=new WeakMap,E=new WeakMap,b=new WeakMap,S=new WeakMap,I=new WeakMap,N=new WeakMap,P=new WeakSet,ae=function(){return h(this,N).has(u)},B=new WeakSet,ce=function(){return Object.prototype.hasOwnProperty.call(this._commands,u)};const De=e=>e,je=(e,t,s)=>s,Te=(e,t)=>t,qe=(e,t)=>({...e,handler:t});export{Be as Clerc,G as CommandExistsError,J as CommandNameConflictError,z as DescriptionNotSetError,Z as InvalidCommandNameError,U as NameNotSetError,H as NoCommandGivenError,V as NoSuchCommandError,u as Root,K as VersionNotSetError,re as compose,qe as defineCommand,je as defineHandler,Te as defineInspector,De as definePlugin,oe as formatCommandName,ne as isInvalidName,se as resolveArgv,Y as resolveCommand,X as resolveFlattenCommands,te as resolveParametersBeforeFlag,Fe as resolveRootCommands,ee as resolveSubcommandsByParent,Le as withBrackets};
|