@clerc/core 0.32.3 → 0.34.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.
package/dist/index.d.ts CHANGED
@@ -305,13 +305,13 @@ type TransformParameters<C extends Command> = {
305
305
  type MakeEventMap<T extends Commands> = {
306
306
  [K in keyof T]: [InspectorContext];
307
307
  };
308
- type FallbackFlags<C extends Command> = Equals<NonNullableFlag<C>["flags"], {}> extends true ? Dict<any> : NonNullableFlag<C>["flags"];
309
- type NonNullableFlag<C extends Command> = TypeFlag<NonNullable<C["flags"]>>;
310
- type ParseFlag<C extends Commands, N extends keyof C> = N extends keyof C ? OmitIndexSignature<NonNullableFlag<C[N]>["flags"]> : FallbackFlags<C[N]>["flags"];
311
- type ParseRaw<C extends Command> = NonNullableFlag<C> & {
312
- flags: FallbackFlags<C>;
308
+ type FallbackFlags<F extends Flags | undefined> = Equals<NonNullableFlag<F>["flags"], {}> extends true ? Dict<any> : NonNullableFlag<F>["flags"];
309
+ type NonNullableFlag<F extends Flags | undefined> = TypeFlag<NonNullable<F>>;
310
+ type ParseFlag<C extends Commands, N extends keyof C, GF extends FlagsWithoutDescription = {}> = N extends keyof C ? OmitIndexSignature<NonNullableFlag<C[N]["flags"] & GF>["flags"]> : FallbackFlags<C[N]["flags"] & GF>["flags"];
311
+ type ParseRaw<C extends Command, GF extends FlagsWithoutDescription = {}> = NonNullableFlag<C["flags"] & GF> & {
312
+ flags: FallbackFlags<C["flags"] & GF>;
313
313
  parameters: string[];
314
- mergedFlags: FallbackFlags<C> & NonNullableFlag<C>["unknownFlags"];
314
+ mergedFlags: FallbackFlags<C["flags"] & GF> & NonNullableFlag<C["flags"] & GF>["unknownFlags"];
315
315
  };
316
316
  type ParseParameters<C extends Commands = Commands, N extends keyof C = keyof C> = Equals<TransformParameters<C[N]>, {}> extends true ? N extends keyof C ? TransformParameters<C[N]> : Dict<string | string[] | undefined> : TransformParameters<C[N]>;
317
317
 
@@ -328,12 +328,14 @@ interface I18N {
328
328
 
329
329
  type CommandType = RootType | string;
330
330
  type FlagOptions = FlagSchema & {
331
- description?: string;
331
+ description: string;
332
332
  };
333
333
  type Flag = FlagOptions & {
334
334
  name: string;
335
335
  };
336
+ type FlagWithoutDescription = Omit<Flag, "description">;
336
337
  type Flags = Dict<FlagOptions>;
338
+ type FlagsWithoutDescription = Dict<FlagWithoutDescription>;
337
339
  declare interface CommandCustomProperties {
338
340
  }
339
341
  interface CommandOptions<P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags> extends CommandCustomProperties {
@@ -360,19 +362,19 @@ interface ParseOptions {
360
362
  argv?: string[];
361
363
  run?: boolean;
362
364
  }
363
- interface HandlerContext<C extends Commands = Commands, N extends keyof C = keyof C> {
365
+ interface HandlerContext<C extends Commands = Commands, N extends keyof C = keyof C, GF extends FlagsWithoutDescription = {}> {
364
366
  name?: LiteralUnion<N, string>;
365
367
  called?: string | RootType;
366
368
  resolved: boolean;
367
369
  hasRootOrAlias: boolean;
368
370
  hasRoot: boolean;
369
- raw: Simplify<ParseRaw<C[N]>>;
371
+ raw: Simplify<ParseRaw<C[N], GF>>;
370
372
  parameters: Simplify<ParseParameters<C, N>>;
371
373
  unknownFlags: ParsedFlags["unknownFlags"];
372
- flags: Simplify<ParseFlag<C, N>>;
373
- cli: Clerc<C>;
374
+ flags: Simplify<ParseFlag<C, N, GF> & Record<string, any>>;
375
+ cli: Clerc<C, GF>;
374
376
  }
375
- type Handler<C extends Commands = Commands, K extends keyof C = keyof C> = (ctx: HandlerContext<C, K>) => void;
377
+ type Handler<C extends Commands = Commands, K extends keyof C = keyof C, GF extends FlagsWithoutDescription = {}> = (ctx: HandlerContext<C, K, GF>) => void;
376
378
  type HandlerInCommand<C extends HandlerContext> = (ctx: {
377
379
  [K in keyof C]: C[K];
378
380
  }) => void;
@@ -389,7 +391,7 @@ interface InspectorObject<C extends Commands = Commands> {
389
391
 
390
392
  declare const Root: unique symbol;
391
393
  type RootType = typeof Root;
392
- declare class Clerc<C extends Commands = {}> {
394
+ declare class Clerc<C extends Commands = {}, GF extends FlagsWithoutDescription = {}> {
393
395
  #private;
394
396
  i18n: I18N;
395
397
  private constructor();
@@ -398,6 +400,7 @@ declare class Clerc<C extends Commands = {}> {
398
400
  get _version(): string;
399
401
  get _inspectors(): Inspector<Commands>[];
400
402
  get _commands(): C;
403
+ get _flags(): GF;
401
404
  /**
402
405
  * Create a new cli
403
406
  * @returns
@@ -406,7 +409,7 @@ declare class Clerc<C extends Commands = {}> {
406
409
  * const cli = Clerc.create()
407
410
  * ```
408
411
  */
409
- static create(name?: string, description?: string, version?: string): Clerc<{}>;
412
+ static create(name?: string, description?: string, version?: string): Clerc<{}, {}>;
410
413
  /**
411
414
  * Set the name of the cli
412
415
  * @param name
@@ -509,8 +512,23 @@ declare class Clerc<C extends Commands = {}> {
509
512
  * })
510
513
  * ```
511
514
  */
512
- command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(c: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>): this & Clerc<C & Record<N, Command<N, O>>>;
513
- command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(name: N, description: string, options?: O & CommandOptions<[...P], A, F>): this & Clerc<C & Record<N, Command<N, O>>>;
515
+ command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(c: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>): this & Clerc<C & Record<N, Command<N, O>>, GF>;
516
+ command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(name: N, description: string, options?: O & CommandOptions<[...P], A, F>): this & Clerc<C & Record<N, Command<N, O>>, GF>;
517
+ /**
518
+ * Register a global flag
519
+ * @param name
520
+ * @param options
521
+ * @returns
522
+ * @example
523
+ * ```ts
524
+ * Clerc.create()
525
+ * .flag("help", {
526
+ * alias: "h",
527
+ * description: "help",
528
+ * })
529
+ * ```
530
+ */
531
+ flag<N extends string, O extends FlagWithoutDescription>(name: N, description: string, options: O): this & Clerc<C, GF & Record<N, O>>;
514
532
  /**
515
533
  * Register a handler
516
534
  * @param name
@@ -525,7 +543,7 @@ declare class Clerc<C extends Commands = {}> {
525
543
  * })
526
544
  * ```
527
545
  */
528
- on<K extends LiteralUnion<keyof CM, string | RootType>, CM extends this["_commands"] = this["_commands"]>(name: K, handler: Handler<CM, K>): this;
546
+ on<K extends LiteralUnion<keyof CM, string | RootType>, CM extends this["_commands"] = this["_commands"]>(name: K, handler: Handler<CM, K, this["_flags"]>): this;
529
547
  /**
530
548
  * Use a plugin
531
549
  * @param plugin
@@ -577,10 +595,10 @@ declare class Clerc<C extends Commands = {}> {
577
595
 
578
596
  type MaybeArray<T> = T | T[];
579
597
 
580
- declare const definePlugin: <T extends Clerc<{}>, U extends Clerc<{}>>(p: Plugin<T, U>) => Plugin<T, U>;
581
- declare const defineHandler: <C extends Clerc<{}>, K extends keyof C["_commands"]>(_cli: C, _key: K, handler: Handler<C["_commands"], K>) => Handler<C["_commands"], K>;
582
- declare const defineInspector: <C extends Clerc<{}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
583
- declare const defineCommand: <N extends string | typeof Root, O extends CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>, P extends string[]>(command: Command<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>, handler?: HandlerInCommand<HandlerContext<Record<N, Command<N, O>> & Record<never, never>, N>> | undefined) => CommandWithHandler<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>;
598
+ declare const definePlugin: <T extends Clerc<{}, {}>, U extends Clerc<{}, {}>>(p: Plugin<T, U>) => Plugin<T, U>;
599
+ declare const defineHandler: <C extends Clerc<{}, {}>, K extends keyof C["_commands"]>(_cli: C, _key: K, handler: Handler<C["_commands"], K, {}>) => Handler<C["_commands"], K, {}>;
600
+ declare const defineInspector: <C extends Clerc<{}, {}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
601
+ declare const defineCommand: <N extends string | typeof Root, O extends CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>, P extends string[]>(command: Command<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>, handler?: HandlerInCommand<HandlerContext<Record<N, Command<N, O>> & Record<never, never>, N, {}>> | undefined) => CommandWithHandler<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>;
584
602
 
585
603
  declare class CommandExistsError extends Error {
586
604
  commandName: string;
@@ -616,16 +634,16 @@ declare class LocaleNotCalledFirstError extends Error {
616
634
  }
617
635
 
618
636
  declare function resolveFlattenCommands(commands: Commands, t: TranslateFn): Map<string[] | typeof Root, CommandAlias<string, CommandOptions<string[], MaybeArray$1<string | typeof Root>, Flags>>>;
619
- declare function resolveCommand(commands: Commands, name: CommandType | string[], t: TranslateFn): [Command<string | RootType> | undefined, string[] | RootType | undefined];
637
+ declare function resolveCommand(commands: Commands, argv: string[], t: TranslateFn): [Command<string | RootType> | undefined, string[] | RootType | undefined];
620
638
  declare function resolveCommandStrict(commands: Commands, name: CommandType | string[], t: TranslateFn): [Command<string | RootType> | undefined, string[] | RootType | undefined];
621
639
  declare function resolveSubcommandsByParent(commands: Commands, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], MaybeArray$1<string | typeof Root>, Flags>>[];
622
640
  declare const resolveRootCommands: (commands: Commands) => Command<string, CommandOptions<string[], MaybeArray$1<string | typeof Root>, Flags>>[];
623
- declare function resolveParametersBeforeFlag(argv: string[]): string[];
624
641
  declare const resolveArgv: () => string[];
625
- declare function compose(inspectors: Inspector[]): (getCtx: () => InspectorContext) => void;
642
+ declare function compose(inspectors: Inspector[]): (ctx: InspectorContext) => void;
626
643
  declare const isValidName: (name: CommandType) => boolean;
627
644
  declare const withBrackets: (s: string, isOptional?: boolean) => string;
628
645
  declare const formatCommandName: (name: string | string[] | RootType) => string;
629
- declare const detectLocale: () => string;
646
+ declare const detectLocale: () => string;
647
+ declare const stripFlags: (argv: string[]) => string[];
630
648
 
631
- export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandType, CommandWithHandler, Commands, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, Handler, HandlerContext, HandlerInCommand, I18N, Inspector, InspectorContext, InspectorFn, InspectorObject, InvalidCommandNameError, LocaleNotCalledFirstError, Locales, MakeEventMap, NameNotSetError, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, Root, RootType, TranslateFn, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, detectLocale, formatCommandName, isValidName, resolveArgv, resolveCommand, resolveCommandStrict, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent, withBrackets };
649
+ export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandType, CommandWithHandler, Commands, DescriptionNotSetError, FallbackType, Flag, FlagOptions, FlagWithoutDescription, Flags, FlagsWithoutDescription, Handler, HandlerContext, HandlerInCommand, I18N, Inspector, InspectorContext, InspectorFn, InspectorObject, InvalidCommandNameError, LocaleNotCalledFirstError, Locales, MakeEventMap, NameNotSetError, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, Root, RootType, TranslateFn, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, detectLocale, formatCommandName, isValidName, resolveArgv, resolveCommand, resolveCommandStrict, resolveFlattenCommands, resolveRootCommands, resolveSubcommandsByParent, stripFlags, withBrackets };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{format as ie}from"node:util";class Ie{constructor(){this.listenerMap={},this.wildcardListeners=new Set}on(t,r){return t==="*"?(this.wildcardListeners.add(r),this):(this.listenerMap[t]||(this.listenerMap[t]=new Set),this.listenerMap[t].add(r),this)}emit(t,...r){return this.listenerMap[t]&&(this.wildcardListeners.forEach(s=>s(t,...r)),this.listenerMap[t].forEach(s=>s(...r))),this}off(t,r){var s,n;return t==="**"?(this.listenerMap={},this.wildcardListeners.clear(),this):t==="*"?(r?this.wildcardListeners.delete(r):this.wildcardListeners.clear(),this):(r?(s=this.listenerMap[t])==null||s.delete(r):(n=this.listenerMap[t])==null||n.clear(),this)}}const xe="known-flag",Re="unknown-flag",$e="argument",{stringify:S}=JSON,je=/\B([A-Z])/g,qe=e=>e.replace(je,"-$1").toLowerCase(),{hasOwnProperty:Te}=Object.prototype,k=(e,t)=>Te.call(e,t),Ge=e=>Array.isArray(e),ce=e=>typeof e=="function"?[e,!1]:Ge(e)?[e[0],!0]:ce(e.type),He=(e,t)=>e===Boolean?t!=="false":t,Ue=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),Ve=/[\s.:=]/,Je=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 r=e.match(Ve);if(r)throw new Error(`${t} cannot contain ${S(r==null?void 0:r[0])}`)},ze=e=>{const t={},r=(s,n)=>{if(k(t,s))throw new Error(`Duplicate flags named ${S(s)}`);t[s]=n};for(const s in e){if(!k(e,s))continue;Je(s);const n=e[s],o=[[],...ce(n),n];r(s,o);const a=qe(s);if(s!==a&&r(a,o),"alias"in n&&typeof n.alias=="string"){const{alias:i}=n,c=`Flag alias ${S(i)} for flag ${S(s)}`;if(i.length===0)throw new Error(`${c} cannot be empty`);if(i.length>1)throw new Error(`${c} must be a single character`);r(i,o)}}return t},Ke=(e,t)=>{const r={};for(const s in e){if(!k(e,s))continue;const[n,,o,a]=t[s];if(n.length===0&&"default"in a){let{default:i}=a;typeof i=="function"&&(i=i()),r[s]=i}else r[s]=o?n:n.pop()}return r},R="--",Ze=/[.:=]/,Qe=/^-{1,2}\w/,Xe=e=>{if(!Qe.test(e))return;const t=!e.startsWith(R);let r=e.slice(t?1:2),s;const n=r.match(Ze);if(n){const{index:o}=n;s=r.slice(o+1),r=r.slice(0,o)}return[r,s,t]},Ye=(e,{onFlag:t,onArgument:r})=>{let s;const n=(o,a)=>{if(typeof s!="function")return!0;s(o,a),s=void 0};for(let o=0;o<e.length;o+=1){const a=e[o];if(a===R){n();const c=e.slice(o+1);r==null||r(c,[o],!0);break}const i=Xe(a);if(i){if(n(),!t)continue;const[c,l,m]=i;if(m)for(let h=0;h<c.length;h+=1){n();const E=h===c.length-1;s=t(c[h],E?l:void 0,[o,h+1,E])}else s=t(c,l,[o])}else n(a,[o])&&(r==null||r([a],[o]))}n()},et=(e,t)=>{for(const[r,s,n]of t.reverse()){if(s){const o=e[r];let a=o.slice(0,s);if(n||(a+=o.slice(s+1)),a!=="-"){e[r]=a;continue}}e.splice(r,1)}},tt=(e,t=process.argv.slice(2),{ignore:r}={})=>{const s=[],n=ze(e),o={},a=[];return a[R]=[],Ye(t,{onFlag(i,c,l){const m=k(n,i);if(!(r!=null&&r(m?xe:Re,i,c))){if(m){const[h,E]=n[i],A=He(E,c),v=(T,G)=>{s.push(l),G&&s.push(G),h.push(Ue(E,T||""))};return A===void 0?v:v(A)}k(o,i)||(o[i]=[]),o[i].push(c===void 0?!0:c),s.push(l)}},onArgument(i,c,l){r!=null&&r($e,t[c[0]])||(a.push(...i),l?(a[R]=i,t.splice(c[0])):s.push(c))}}),et(t,s),{flags:Ke(e,n),unknownFlags:o,_:a}};function H(e){return e!==null&&typeof e=="object"}function U(e,t,r=".",s){if(!H(t))return U(e,{},r,s);const n=Object.assign({},t);for(const o in e){if(o==="__proto__"||o==="constructor")continue;const a=e[o];a!=null&&(s&&s(n,o,a,r)||(Array.isArray(a)&&Array.isArray(n[o])?n[o]=[...a,...n[o]]:H(a)&&H(n[o])?n[o]=U(a,n[o],(r?`${r}.`:"")+o.toString(),s):n[o]=a))}return n}function rt(e){return(...t)=>t.reduce((r,s)=>U(r,s,"",e),{})}const st=rt(),$=e=>Array.isArray(e)?e:[e],ot=e=>e.replace(/[\W_]([a-z\d])?/gi,(t,r)=>r?r.toUpperCase():""),ue=(e,t)=>t.length!==e.length?!1:e.every((r,s)=>r===t[s]),le=(e,t)=>t.length>e.length?!1:ue(e.slice(0,t.length),t),L=JSON.stringify;class he extends Error{constructor(t,r){super(r("core.commandExists",L(t))),this.commandName=t}}class de extends Error{constructor(t,r){super(r("core.noSuchCommand",L(t))),this.commandName=t}}class fe extends Error{constructor(t){super(t("core.noCommandGiven"))}}class me extends Error{constructor(t,r,s){super(s("core.commandNameConflict",L(t),L(r))),this.n1=t,this.n2=r}}class pe extends Error{constructor(t){super(t("core.nameNotSet"))}}class Ce extends Error{constructor(t){super(t("core.descriptionNotSet"))}}class we extends Error{constructor(t){super(t("core.versionNotSet"))}}class Ee extends Error{constructor(t,r){super(r("core.badNameFormat",L(t))),this.commandName=t}}class V extends Error{constructor(t){super(t("core.localeMustBeCalledFirst"))}}const ge=typeof Deno!="undefined",nt=typeof process!="undefined"&&!ge,at=process.versions.electron&&!process.defaultApp;function _e(e,t,r,s){if(r.alias){const n=$(r.alias);for(const o of n){if(o in t)throw new me(t[o].name,r.name,s);e.set(typeof o=="symbol"?o:o.split(" "),{...r,__isAlias:!0})}}}function J(e,t){const r=new Map;e[f]&&(r.set(f,e[f]),_e(r,e,e[f],t));for(const s of Object.values(e))_e(r,e,s,t),r.set(s.name.split(" "),s);return r}function ve(e,t,r){if(t===f)return[e[f],f];const s=$(t),n=J(e,r);let o,a;return n.forEach((i,c)=>{if(c===f){o=e[f],a=f;return}le(s,c)&&(!a||a===f||c.length>a.length)&&(o=i,a=c)}),[o,a]}function it(e,t,r){if(t===f)return[e[f],f];const s=$(t),n=J(e,r);let o,a;return n.forEach((i,c)=>{c===f||a===f||ue(s,c)&&(o=i,a=c)}),[o,a]}function Me(e,t,r=1/0){const s=t===""?[]:Array.isArray(t)?t:t.split(" ");return Object.values(e).filter(n=>{const o=n.name.split(" ");return le(o,s)&&o.length-s.length<=r})}const ct=e=>Me(e,"",1);function Fe(e){const t=[];for(const r of e){if(r.startsWith("-"))break;t.push(r)}return t}const z=()=>nt?process.argv.slice(at?1:2):ge?Deno.args:[];function ye(e){const t={pre:[],normal:[],post:[]};for(const s of e){const n=typeof s=="object"?s:{fn:s},{enforce:o,fn:a}=n;o==="post"||o==="pre"?t[o].push(a):t.normal.push(a)}const r=[...t.pre,...t.normal,...t.post];return s=>{const n=[];let o=0;const a=i=>{o=i;const c=r[i],l=c(s(),a.bind(null,i+1));l&&n.push(l)};if(a(0),o+1===r.length)for(const i of n)i()}}const ut=/\s\s+/,Be=e=>e===f?!0:!(e.startsWith(" ")||e.endsWith(" "))&&!ut.test(e),lt=(e,t)=>t?`[${e}]`:`<${e}>`,ht="<Root>",Ne=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:ht,Ae=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,{stringify:B}=JSON;function K(e,t){const r=[];let s,n;for(const o of e){if(n)throw new Error(t("core.spreadParameterMustBeLast",B(n)));const a=o[0],i=o[o.length-1];let c;if(a==="<"&&i===">"&&(c=!0,s))throw new Error(t("core.requiredParameterMustBeBeforeOptional",B(o),B(s)));if(a==="["&&i==="]"&&(c=!1,s=o),c===void 0)throw new Error(t("core.parameterMustBeWrappedInBrackets",B(o)));let l=o.slice(1,-1);const m=l.slice(-3)==="...";m&&(n=o,l=l.slice(0,-3)),r.push({name:l,required:c,spread:m})}return r}function Z(e,t,r,s){for(let n=0;n<t.length;n+=1){const{name:o,required:a,spread:i}=t[n],c=ot(o);if(c in e)throw new Error(s("core.parameterIsUsedMoreThanOnce",B(o)));const l=i?r.slice(n):r[n];if(i&&(n=t.length),a&&(!l||i&&l.length===0))throw new Error(s("core.missingRequiredParameter",B(o)));e[c]=l}}const dt={en:{"core.commandExists":'Command "%s" exists.',"core.noSuchCommand":"No such command: %s.","core.noCommandGiven":"No command given.","core.commandNameConflict":"Command name %s conflicts with %s. Maybe caused by alias.","core.nameNotSet":"Name not set.","core.descriptionNotSet":"Description not set.","core.versionNotSet":"Version not set.","core.badNameFormat":"Bad name format: %s.","core.localeMustBeCalledFirst":"locale() or fallbackLocale() must be called at first.","core.cliParseMustBeCalled":"cli.parse() must be called.","core.spreadParameterMustBeLast":"Invalid Parameter: Spread parameter %s must be last.","core.requiredParameterCannotComeAfterOptionalParameter":"Invalid Parameter: Required parameter %s cannot come after optional parameter %s.","core.parameterMustBeWrappedInBrackets":"Invalid Parameter: Parameter %s must be wrapped in <> (required parameter) or [] (optional parameter).","core.parameterIsUsedMoreThanOnce":"Invalid Parameter: Parameter %s is used more than once.","core.missingRequiredParameter":"Missing required parameter %s."},"zh-CN":{"core.commandExists":'\u547D\u4EE4 "%s" \u5DF2\u5B58\u5728\u3002',"core.noSuchCommand":"\u627E\u4E0D\u5230\u547D\u4EE4: %s\u3002","core.noCommandGiven":"\u6CA1\u6709\u8F93\u5165\u547D\u4EE4\u3002","core.commandNameConflict":"\u547D\u4EE4\u540D\u79F0 %s \u548C %s \u51B2\u7A81\u3002 \u53EF\u80FD\u662F\u7531\u4E8E\u522B\u540D\u5BFC\u81F4\u7684\u3002","core.nameNotSet":"\u672A\u8BBE\u7F6ECLI\u540D\u79F0\u3002","core.descriptionNotSet":"\u672A\u8BBE\u7F6ECLI\u63CF\u8FF0\u3002","core.versionNotSet":"\u672A\u8BBE\u7F6ECLI\u7248\u672C\u3002","core.badNameFormat":"\u9519\u8BEF\u7684\u547D\u4EE4\u540D\u5B57\u683C\u5F0F: %s\u3002","core.localeMustBeCalledFirst":"locale() \u6216 fallbackLocale() \u5FC5\u987B\u5728\u6700\u5F00\u59CB\u8C03\u7528\u3002","core.cliParseMustBeCalled":"cli.parse() \u5FC5\u987B\u88AB\u8C03\u7528\u3002","core.spreadParameterMustBeLast":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5C55\u5F00\u53C2\u6570 %s \u5FC5\u987B\u5728\u6700\u540E\u3002","core.requiredParameterCannotComeAfterOptionalParameter":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5FC5\u586B\u53C2\u6570 %s \u4E0D\u80FD\u5728\u53EF\u9009\u53C2\u6570 %s \u4E4B\u540E\u3002","core.parameterMustBeWrappedInBrackets":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u5FC5\u987B\u88AB <> (\u5FC5\u586B\u53C2\u6570) \u6216 [] (\u53EF\u9009\u53C2\u6570) \u5305\u88F9\u3002","core.parameterIsUsedMoreThanOnce":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u88AB\u4F7F\u7528\u4E86\u591A\u6B21\u3002","core.missingRequiredParameter":"\u7F3A\u5C11\u5FC5\u586B\u53C2\u6570 %s\u3002"}};var Q=(e,t,r)=>{if(!t.has(e))throw TypeError("Cannot "+r)},u=(e,t,r)=>(Q(e,t,"read from private field"),r?r.call(e):t.get(e)),d=(e,t,r)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,r)},C=(e,t,r,s)=>(Q(e,t,"write to private field"),s?s.call(e,r):t.set(e,r),r),p=(e,t,r)=>(Q(e,t,"access private method"),r),_,M,F,D,W,j,N,b,P,O,I,x,y,X,Se,Y,ke,ee,Le,w,g,te,De,re,We,se,be,q,oe,ne,Pe;const f=Symbol.for("Clerc.Root"),Oe=class{constructor(e,t,r){d(this,X),d(this,Y),d(this,ee),d(this,w),d(this,te),d(this,re),d(this,se),d(this,q),d(this,ne),d(this,_,""),d(this,M,""),d(this,F,""),d(this,D,[]),d(this,W,{}),d(this,j,new Ie),d(this,N,new Set),d(this,b,void 0),d(this,P,[]),d(this,O,!1),d(this,I,"en"),d(this,x,"en"),d(this,y,{}),this.i18n={add:s=>{C(this,y,st(u(this,y),s))},t:(s,...n)=>{const o=u(this,y)[u(this,x)]||u(this,y)[u(this,I)],a=u(this,y)[u(this,I)];return o[s]?ie(o[s],...n):a[s]?ie(a[s],...n):void 0}},C(this,_,e||u(this,_)),C(this,M,t||u(this,M)),C(this,F,r||u(this,F)),C(this,x,Ae()),p(this,ee,Le).call(this)}get _name(){return u(this,_)}get _description(){return u(this,M)}get _version(){return u(this,F)}get _inspectors(){return u(this,D)}get _commands(){return u(this,W)}static create(e,t,r){return new Oe(e,t,r)}name(e){return p(this,w,g).call(this),C(this,_,e),this}description(e){return p(this,w,g).call(this),C(this,M,e),this}version(e){return p(this,w,g).call(this),C(this,F,e),this}locale(e){if(u(this,O))throw new V(this.i18n.t);return C(this,x,e),this}fallbackLocale(e){if(u(this,O))throw new V(this.i18n.t);return C(this,I,e),this}errorHandler(e){return u(this,P).push(e),this}command(e,t,r={}){return p(this,q,oe).call(this,()=>p(this,te,De).call(this,e,t,r)),this}on(e,t){return u(this,j).on(e,t),this}use(e){return p(this,w,g).call(this),e.setup(this)}inspector(e){return p(this,w,g).call(this),u(this,D).push(e),this}parse(e=z()){p(this,w,g).call(this);const{argv:t,run:r}=Array.isArray(e)?{argv:e,run:!0}:{argv:z(),...e};return C(this,b,[...t]),p(this,re,We).call(this),r&&this.runMatchedCommand(),this}runMatchedCommand(){return p(this,q,oe).call(this,()=>p(this,ne,Pe).call(this)),process.title=u(this,_),this}};let ft=Oe;_=new WeakMap,M=new WeakMap,F=new WeakMap,D=new WeakMap,W=new WeakMap,j=new WeakMap,N=new WeakMap,b=new WeakMap,P=new WeakMap,O=new WeakMap,I=new WeakMap,x=new WeakMap,y=new WeakMap,X=new WeakSet,Se=function(){return u(this,N).has(f)},Y=new WeakSet,ke=function(){return Object.prototype.hasOwnProperty.call(this._commands,f)},ee=new WeakSet,Le=function(){this.i18n.add(dt)},w=new WeakSet,g=function(){C(this,O,!0)},te=new WeakSet,De=function(e,t,r={}){p(this,w,g).call(this);const{t:s}=this.i18n,o=(h=>!(typeof h=="string"||h===f))(e),a=o?e.name:e;if(!Be(a))throw new Ee(a,s);const{handler:i=void 0,...c}=o?e:{name:a,description:t,...r},l=[c.name],m=c.alias?$(c.alias):[];c.alias&&l.push(...m);for(const h of l)if(u(this,N).has(h))throw new he(Ne(h),s);return u(this,W)[a]=c,u(this,N).add(c.name),m.forEach(h=>u(this,N).add(h)),o&&i&&this.on(e.name,i),this},re=new WeakSet,We=function(){const{t:e}=this.i18n;if(!u(this,_))throw new pe(e);if(!u(this,M))throw new Ce(e);if(!u(this,F))throw new we(e)},se=new WeakSet,be=function(e){const t=u(this,b),{t:r}=this.i18n,[s,n]=e(),o=!!s,a=tt((s==null?void 0:s.flags)||{},[...t]),{_:i,flags:c,unknownFlags:l}=a;let m=!o||s.name===f?i:i.slice(s.name.split(" ").length),h=(s==null?void 0:s.parameters)||[];const E=h.indexOf("--"),A=h.slice(E+1)||[],v=Object.create(null);if(E>-1&&A.length>0){h=h.slice(0,E);const ae=i["--"];m=m.slice(0,-ae.length||void 0),Z(v,K(h,r),m,r),Z(v,K(A,r),ae,r)}else Z(v,K(h,r),m,r);const T={...c,...l};return{name:s==null?void 0:s.name,called:Array.isArray(n)?n.join(" "):n,resolved:o,hasRootOrAlias:u(this,X,Se),hasRoot:u(this,Y,ke),raw:{...a,parameters:m,mergedFlags:T},parameters:v,flags:c,unknownFlags:l,cli:this}},q=new WeakSet,oe=function(e){try{e()}catch(t){if(u(this,P).length>0)u(this,P).forEach(r=>r(t));else throw t}},ne=new WeakSet,Pe=function(){p(this,w,g).call(this);const{t:e}=this.i18n,t=u(this,b);if(!t)throw new Error(e("core.cliParseMustBeCalled"));const r=Fe(t),s=r.join(" "),n=()=>ve(u(this,W),r,e),o=()=>p(this,se,be).call(this,n),a={enforce:"post",fn:()=>{const[l]=n(),m=o();if(!l)throw s?new de(s,e):new fe(e);u(this,j).emit(l.name,m)}},i=[...u(this,D),a];ye(i)(o)};const mt=e=>e,pt=(e,t,r)=>r,Ct=(e,t)=>t,wt=(e,t)=>({...e,handler:t});export{ft as Clerc,he as CommandExistsError,me as CommandNameConflictError,Ce as DescriptionNotSetError,Ee as InvalidCommandNameError,V as LocaleNotCalledFirstError,pe as NameNotSetError,fe as NoCommandGivenError,de as NoSuchCommandError,f as Root,we as VersionNotSetError,ye as compose,wt as defineCommand,pt as defineHandler,Ct as defineInspector,mt as definePlugin,Ae as detectLocale,Ne as formatCommandName,Be as isValidName,z as resolveArgv,ve as resolveCommand,it as resolveCommandStrict,J as resolveFlattenCommands,Fe as resolveParametersBeforeFlag,ct as resolveRootCommands,Me as resolveSubcommandsByParent,lt as withBrackets};
1
+ import{format as ce}from"node:util";class Re{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 $e="known-flag",je="unknown-flag",qe="argument",{stringify:A}=JSON,Te=/\B([A-Z])/g,Ge=e=>e.replace(Te,"-$1").toLowerCase(),{hasOwnProperty:He}=Object.prototype,k=(e,t)=>He.call(e,t),Ue=e=>Array.isArray(e),ue=e=>typeof e=="function"?[e,!1]:Ue(e)?[e[0],!0]:ue(e.type),Ve=(e,t)=>e===Boolean?t!=="false":t,Je=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),ze=/[\s.:=]/,Ke=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(ze);if(s)throw new Error(`${t} cannot contain ${A(s==null?void 0:s[0])}`)},Ze=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;Ke(r);const n=e[r],o=[[],...ue(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},Qe=(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},R="--",Xe=/[.:=]/,Ye=/^-{1,2}\w/,et=e=>{if(!Ye.test(e))return;const t=!e.startsWith(R);let s=e.slice(t?1:2),r;const n=s.match(Xe);if(n){const{index:o}=n;r=s.slice(o+1),s=s.slice(0,o)}return[s,r,t]},tt=(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===R){n();const c=e.slice(o+1);s==null||s(c,[o],!0);break}const i=et(a);if(i){if(n(),!t)continue;const[c,l,m]=i;if(m)for(let d=0;d<c.length;d+=1){n();const g=d===c.length-1;r=t(c[d],g?l:void 0,[o,d+1,g])}else r=t(c,l,[o])}else n(a,[o])&&(s==null||s([a],[o]))}n()},st=(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)}},le=(e,t=process.argv.slice(2),{ignore:s}={})=>{const r=[],n=Ze(e),o={},a=[];return a[R]=[],tt(t,{onFlag(i,c,l){const m=k(n,i);if(!(s!=null&&s(m?$e:je,i,c))){if(m){const[d,g]=n[i],S=Ve(g,c),v=(T,G)=>{r.push(l),G&&r.push(G),d.push(Je(g,T||""))};return S===void 0?v:v(S)}k(o,i)||(o[i]=[]),o[i].push(c===void 0?!0:c),r.push(l)}},onArgument(i,c,l){s!=null&&s(qe,t[c[0]])||(a.push(...i),l?(a[R]=i,t.splice(c[0])):r.push(c))}}),st(t,r),{flags:Qe(e,n),unknownFlags:o,_:a}};function H(e){return e!==null&&typeof e=="object"}function U(e,t,s=".",r){if(!H(t))return U(e,{},s,r);const n=Object.assign({},t);for(const o in e){if(o==="__proto__"||o==="constructor")continue;const a=e[o];a!=null&&(r&&r(n,o,a,s)||(Array.isArray(a)&&Array.isArray(n[o])?n[o]=[...a,...n[o]]:H(a)&&H(n[o])?n[o]=U(a,n[o],(s?`${s}.`:"")+o.toString(),r):n[o]=a))}return n}function rt(e){return(...t)=>t.reduce((s,r)=>U(s,r,"",e),{})}const ot=rt(),V=e=>Array.isArray(e)?e:[e],nt=e=>e.replace(/[\W_]([a-z\d])?/gi,(t,s)=>s?s.toUpperCase():""),he=(e,t)=>t.length!==e.length?!1:e.every((s,r)=>s===t[r]),de=(e,t)=>t.length>e.length?!1:he(e.slice(0,t.length),t),L=JSON.stringify;class fe extends Error{constructor(t,s){super(s("core.commandExists",L(t))),this.commandName=t}}class me extends Error{constructor(t,s){super(s("core.noSuchCommand",L(t))),this.commandName=t}}class pe extends Error{constructor(t){super(t("core.noCommandGiven"))}}class Ce extends Error{constructor(t,s,r){super(r("core.commandNameConflict",L(t),L(s))),this.n1=t,this.n2=s}}class we extends Error{constructor(t){super(t("core.nameNotSet"))}}class ge extends Error{constructor(t){super(t("core.descriptionNotSet"))}}class Ee extends Error{constructor(t){super(t("core.versionNotSet"))}}class _e extends Error{constructor(t,s){super(s("core.badNameFormat",L(t))),this.commandName=t}}class J extends Error{constructor(t){super(t("core.localeMustBeCalledFirst"))}}const ve=typeof Deno!="undefined",at=typeof process!="undefined"&&!ve,it=process.versions.electron&&!process.defaultApp;function Me(e,t,s,r){if(s.alias){const n=V(s.alias);for(const o of n){if(o in t)throw new Ce(t[o].name,s.name,r);e.set(typeof o=="symbol"?o:o.split(" "),{...s,__isAlias:!0})}}}function z(e,t){const s=new Map;e[f]&&(s.set(f,e[f]),Me(s,e,e[f],t));for(const r of Object.values(e))Me(s,e,r,t),s.set(r.name.split(" "),r);return s}function Fe(e,t,s){const r=z(e,s);for(const[n,o]of r.entries()){const a=le((o==null?void 0:o.flags)||{},[...t]),{_:i}=a;if(n!==f&&de(i,n))return[o,n]}return r.has(f)?[r.get(f),f]:[void 0,void 0]}function ct(e,t,s){if(t===f)return[e[f],f];const r=V(t),n=z(e,s);let o,a;return n.forEach((i,c)=>{c===f||a===f||he(r,c)&&(o=i,a=c)}),[o,a]}function ye(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 de(o,r)&&o.length-r.length<=s})}const ut=e=>ye(e,"",1),K=()=>at?process.argv.slice(it?1:2):ve?Deno.args:[];function Be(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=>{const n=[];let o=0;const a=i=>{o=i;const c=s[i],l=c(r,a.bind(null,i+1));l&&n.push(l)};if(a(0),o+1===s.length)for(const i of n)i()}}const lt=/\s\s+/,Ne=e=>e===f?!0:!(e.startsWith(" ")||e.endsWith(" "))&&!lt.test(e),ht=(e,t)=>t?`[${e}]`:`<${e}>`,dt="<Root>",Se=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:dt,Ae=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,ke=e=>e.filter(t=>!t.startsWith("-")),{stringify:B}=JSON;function Z(e,t){const s=[];let r,n;for(const o of e){if(n)throw new Error(t("core.spreadParameterMustBeLast",B(n)));const a=o[0],i=o[o.length-1];let c;if(a==="<"&&i===">"&&(c=!0,r))throw new Error(t("core.requiredParameterMustBeBeforeOptional",B(o),B(r)));if(a==="["&&i==="]"&&(c=!1,r=o),c===void 0)throw new Error(t("core.parameterMustBeWrappedInBrackets",B(o)));let l=o.slice(1,-1);const m=l.slice(-3)==="...";m&&(n=o,l=l.slice(0,-3)),s.push({name:l,required:c,spread:m})}return s}function Q(e,t,s,r){for(let n=0;n<t.length;n+=1){const{name:o,required:a,spread:i}=t[n],c=nt(o);if(c in e)throw new Error(r("core.parameterIsUsedMoreThanOnce",B(o)));const l=i?s.slice(n):s[n];if(i&&(n=t.length),a&&(!l||i&&l.length===0))throw new Error(r("core.missingRequiredParameter",B(o)));e[c]=l}}const ft={en:{"core.commandExists":'Command "%s" exists.',"core.noSuchCommand":"No such command: %s.","core.noCommandGiven":"No command given.","core.commandNameConflict":"Command name %s conflicts with %s. Maybe caused by alias.","core.nameNotSet":"Name not set.","core.descriptionNotSet":"Description not set.","core.versionNotSet":"Version not set.","core.badNameFormat":"Bad name format: %s.","core.localeMustBeCalledFirst":"locale() or fallbackLocale() must be called at first.","core.cliParseMustBeCalled":"cli.parse() must be called.","core.spreadParameterMustBeLast":"Invalid Parameter: Spread parameter %s must be last.","core.requiredParameterCannotComeAfterOptionalParameter":"Invalid Parameter: Required parameter %s cannot come after optional parameter %s.","core.parameterMustBeWrappedInBrackets":"Invalid Parameter: Parameter %s must be wrapped in <> (required parameter) or [] (optional parameter).","core.parameterIsUsedMoreThanOnce":"Invalid Parameter: Parameter %s is used more than once.","core.missingRequiredParameter":"Missing required parameter %s."},"zh-CN":{"core.commandExists":'\u547D\u4EE4 "%s" \u5DF2\u5B58\u5728\u3002',"core.noSuchCommand":"\u627E\u4E0D\u5230\u547D\u4EE4: %s\u3002","core.noCommandGiven":"\u6CA1\u6709\u8F93\u5165\u547D\u4EE4\u3002","core.commandNameConflict":"\u547D\u4EE4\u540D\u79F0 %s \u548C %s \u51B2\u7A81\u3002 \u53EF\u80FD\u662F\u7531\u4E8E\u522B\u540D\u5BFC\u81F4\u7684\u3002","core.nameNotSet":"\u672A\u8BBE\u7F6ECLI\u540D\u79F0\u3002","core.descriptionNotSet":"\u672A\u8BBE\u7F6ECLI\u63CF\u8FF0\u3002","core.versionNotSet":"\u672A\u8BBE\u7F6ECLI\u7248\u672C\u3002","core.badNameFormat":"\u9519\u8BEF\u7684\u547D\u4EE4\u540D\u5B57\u683C\u5F0F: %s\u3002","core.localeMustBeCalledFirst":"locale() \u6216 fallbackLocale() \u5FC5\u987B\u5728\u6700\u5F00\u59CB\u8C03\u7528\u3002","core.cliParseMustBeCalled":"cli.parse() \u5FC5\u987B\u88AB\u8C03\u7528\u3002","core.spreadParameterMustBeLast":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5C55\u5F00\u53C2\u6570 %s \u5FC5\u987B\u5728\u6700\u540E\u3002","core.requiredParameterCannotComeAfterOptionalParameter":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5FC5\u586B\u53C2\u6570 %s \u4E0D\u80FD\u5728\u53EF\u9009\u53C2\u6570 %s \u4E4B\u540E\u3002","core.parameterMustBeWrappedInBrackets":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u5FC5\u987B\u88AB <> (\u5FC5\u586B\u53C2\u6570) \u6216 [] (\u53EF\u9009\u53C2\u6570) \u5305\u88F9\u3002","core.parameterIsUsedMoreThanOnce":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u88AB\u4F7F\u7528\u4E86\u591A\u6B21\u3002","core.missingRequiredParameter":"\u7F3A\u5C11\u5FC5\u586B\u53C2\u6570 %s\u3002"}};var X=(e,t,s)=>{if(!t.has(e))throw TypeError("Cannot "+s)},u=(e,t,s)=>(X(e,t,"read from private field"),s?s.call(e):t.get(e)),h=(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)=>(X(e,t,"write to private field"),r?r.call(e,s):t.set(e,s),s),p=(e,t,s)=>(X(e,t,"access private method"),s),_,M,F,W,D,$,j,N,b,O,P,I,x,y,Y,Le,ee,We,te,De,w,E,se,be,re,Oe,oe,Pe,q,ne,ae,Ie;const f=Symbol.for("Clerc.Root"),xe=class{constructor(e,t,s){h(this,Y),h(this,ee),h(this,te),h(this,w),h(this,se),h(this,re),h(this,oe),h(this,q),h(this,ae),h(this,_,""),h(this,M,""),h(this,F,""),h(this,W,[]),h(this,D,{}),h(this,$,new Re),h(this,j,{}),h(this,N,new Set),h(this,b,void 0),h(this,O,[]),h(this,P,!1),h(this,I,"en"),h(this,x,"en"),h(this,y,{}),this.i18n={add:r=>{C(this,y,ot(u(this,y),r))},t:(r,...n)=>{const o=u(this,y)[u(this,x)]||u(this,y)[u(this,I)],a=u(this,y)[u(this,I)];return o[r]?ce(o[r],...n):a[r]?ce(a[r],...n):void 0}},C(this,_,e||u(this,_)),C(this,M,t||u(this,M)),C(this,F,s||u(this,F)),C(this,x,Ae()),p(this,te,De).call(this)}get _name(){return u(this,_)}get _description(){return u(this,M)}get _version(){return u(this,F)}get _inspectors(){return u(this,W)}get _commands(){return u(this,D)}get _flags(){return u(this,j)}static create(e,t,s){return new xe(e,t,s)}name(e){return p(this,w,E).call(this),C(this,_,e),this}description(e){return p(this,w,E).call(this),C(this,M,e),this}version(e){return p(this,w,E).call(this),C(this,F,e),this}locale(e){if(u(this,P))throw new J(this.i18n.t);return C(this,x,e),this}fallbackLocale(e){if(u(this,P))throw new J(this.i18n.t);return C(this,I,e),this}errorHandler(e){return u(this,O).push(e),this}command(e,t,s={}){return p(this,q,ne).call(this,()=>p(this,se,be).call(this,e,t,s)),this}flag(e,t,s){return u(this,j)[e]={description:t,...s},this}on(e,t){return u(this,$).on(e,t),this}use(e){return p(this,w,E).call(this),e.setup(this)}inspector(e){return p(this,w,E).call(this),u(this,W).push(e),this}parse(e=K()){p(this,w,E).call(this);const{argv:t,run:s}=Array.isArray(e)?{argv:e,run:!0}:{argv:K(),...e};return C(this,b,[...t]),p(this,re,Oe).call(this),s&&this.runMatchedCommand(),this}runMatchedCommand(){return p(this,q,ne).call(this,()=>p(this,ae,Ie).call(this)),process.title=u(this,_),this}};let mt=xe;_=new WeakMap,M=new WeakMap,F=new WeakMap,W=new WeakMap,D=new WeakMap,$=new WeakMap,j=new WeakMap,N=new WeakMap,b=new WeakMap,O=new WeakMap,P=new WeakMap,I=new WeakMap,x=new WeakMap,y=new WeakMap,Y=new WeakSet,Le=function(){return u(this,N).has(f)},ee=new WeakSet,We=function(){return Object.prototype.hasOwnProperty.call(this._commands,f)},te=new WeakSet,De=function(){this.i18n.add(ft)},w=new WeakSet,E=function(){C(this,P,!0)},se=new WeakSet,be=function(e,t,s={}){p(this,w,E).call(this);const{t:r}=this.i18n,o=(d=>!(typeof d=="string"||d===f))(e),a=o?e.name:e;if(!Ne(a))throw new _e(a,r);const{handler:i=void 0,...c}=o?e:{name:a,description:t,...s},l=[c.name],m=c.alias?V(c.alias):[];c.alias&&l.push(...m);for(const d of l)if(u(this,N).has(d))throw new fe(Se(d),r);return u(this,D)[a]=c,u(this,N).add(c.name),m.forEach(d=>u(this,N).add(d)),o&&i&&this.on(e.name,i),this},re=new WeakSet,Oe=function(){const{t:e}=this.i18n;if(!u(this,_))throw new we(e);if(!u(this,M))throw new ge(e);if(!u(this,F))throw new Ee(e)},oe=new WeakSet,Pe=function(e){const t=u(this,b),{t:s}=this.i18n,[r,n]=e(),o=!!r,a=le((r==null?void 0:r.flags)||{},[...t]),{_:i,flags:c,unknownFlags:l}=a;let m=!o||r.name===f?i:i.slice(r.name.split(" ").length),d=(r==null?void 0:r.parameters)||[];const g=d.indexOf("--"),S=d.slice(g+1)||[],v=Object.create(null);if(g>-1&&S.length>0){d=d.slice(0,g);const ie=i["--"];m=m.slice(0,-ie.length||void 0),Q(v,Z(d,s),m,s),Q(v,Z(S,s),ie,s)}else Q(v,Z(d,s),m,s);const T={...c,...l};return{name:r==null?void 0:r.name,called:Array.isArray(n)?n.join(" "):n,resolved:o,hasRootOrAlias:u(this,Y,Le),hasRoot:u(this,ee,We),raw:{...a,parameters:m,mergedFlags:T},parameters:v,flags:c,unknownFlags:l,cli:this}},q=new WeakSet,ne=function(e){try{e()}catch(t){if(u(this,O).length>0)u(this,O).forEach(s=>s(t));else throw t}},ae=new WeakSet,Ie=function(){p(this,w,E).call(this);const{t:e}=this.i18n,t=u(this,b);if(!t)throw new Error(e("core.cliParseMustBeCalled"));const s=()=>Fe(u(this,D),t,e),r=()=>p(this,oe,Pe).call(this,s),n={enforce:"post",fn:i=>{const[c]=s(),l=ke(t).join(" ");if(!c)throw l?new me(l,e):new pe(e);u(this,$).emit(c.name,i)}},o=[...u(this,W),n];Be(o)(r())};const pt=e=>e,Ct=(e,t,s)=>s,wt=(e,t)=>t,gt=(e,t)=>({...e,handler:t});export{mt as Clerc,fe as CommandExistsError,Ce as CommandNameConflictError,ge as DescriptionNotSetError,_e as InvalidCommandNameError,J as LocaleNotCalledFirstError,we as NameNotSetError,pe as NoCommandGivenError,me as NoSuchCommandError,f as Root,Ee as VersionNotSetError,Be as compose,gt as defineCommand,Ct as defineHandler,wt as defineInspector,pt as definePlugin,Ae as detectLocale,Se as formatCommandName,Ne as isValidName,K as resolveArgv,Fe as resolveCommand,ct as resolveCommandStrict,z as resolveFlattenCommands,ut as resolveRootCommands,ye as resolveSubcommandsByParent,ke as stripFlags,ht as withBrackets};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/core",
3
- "version": "0.32.3",
3
+ "version": "0.34.0",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
5
  "description": "Clerc core",
6
6
  "keywords": [
@@ -52,7 +52,7 @@
52
52
  "lite-emit": "^1.4.0",
53
53
  "type-fest": "^3.6.0",
54
54
  "type-flag": "^3.0.0",
55
- "@clerc/utils": "0.32.3"
55
+ "@clerc/utils": "0.34.0"
56
56
  },
57
57
  "scripts": {
58
58
  "build": "puild --minify",