@clerc/core 0.43.0 → 1.0.0-beta.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.
Files changed (3) hide show
  1. package/dist/index.d.ts +243 -472
  2. package/dist/index.js +308 -805
  3. package/package.json +12 -15
package/dist/index.d.ts CHANGED
@@ -1,481 +1,252 @@
1
- import { Equals, Dict, CamelCase, MaybeArray as MaybeArray$1 } from '@clerc/utils';
2
- import { OmitIndexSignature, LiteralUnion, Simplify } from 'type-fest';
1
+ import { ErrorHandler as ErrorHandler$1 } from "lite-emit";
3
2
 
4
- declare const DOUBLE_DASH = "--";
5
- type TypeFunction<ReturnType = any> = (value: any) => ReturnType;
6
- type TypeFunctionArray<ReturnType> = readonly [TypeFunction<ReturnType>];
7
- type FlagType<ReturnType = any> = TypeFunction<ReturnType> | TypeFunctionArray<ReturnType>;
8
- type FlagSchemaBase<TF> = {
9
- /**
10
- * Type of the flag as a function that parses the argv string and returns the
11
- * parsed value.
12
- *
13
- * @example
14
- *
15
- * ```
16
- * type: String;
17
- * ```
18
- *
19
- * @example Wrap in an array to accept multiple values. `type: [Boolean]`
20
- *
21
- * @example Custom function type that uses moment.js to parse string as date.
22
- * `type: function CustomDate(value: string) { return moment(value).toDate();
23
- * }`
24
- */
25
- type: TF;
26
- /**
27
- * A single-character alias for the flag.
28
- *
29
- * @example
30
- *
31
- * ```
32
- * alias: "s";
33
- * ```
34
- */
35
- alias?: string;
36
- } & Record<PropertyKey, unknown>;
37
- type FlagSchemaDefault<TF, DefaultType = any> = FlagSchemaBase<TF> & {
38
- /**
39
- * Default value of the flag. Also accepts a function that returns the default
40
- * value. [Default: undefined]
41
- *
42
- * @example
43
- *
44
- * ```
45
- * default: 'hello'
46
- * ```
47
- *
48
- * @example
49
- *
50
- * ```
51
- * default: () => [1, 2, 3]
52
- * ```
53
- */
54
- default: DefaultType | (() => DefaultType);
55
- };
56
- type FlagSchema<TF = FlagType> = FlagSchemaBase<TF> | FlagSchemaDefault<TF>;
57
- type FlagTypeOrSchema<ExtraOptions = Record<string, unknown>> = FlagType | (FlagSchema & ExtraOptions);
58
- type Flags$1<ExtraOptions = Record<string, unknown>> = Record<string, FlagTypeOrSchema<ExtraOptions>>;
59
- type InferFlagType<Flag extends FlagTypeOrSchema> = Flag extends TypeFunctionArray<infer T> | FlagSchema<TypeFunctionArray<infer T>> ? Flag extends FlagSchemaDefault<TypeFunctionArray<T>, infer D> ? T[] | D : T[] : Flag extends TypeFunction<infer T> | FlagSchema<TypeFunction<infer T>> ? Flag extends FlagSchemaDefault<TypeFunction<T>, infer D> ? T | D : T | undefined : never;
60
- interface ParsedFlags<Schemas = Record<string, unknown>> {
61
- flags: Schemas;
62
- unknownFlags: Record<string, (string | boolean)[]>;
63
- _: string[] & {
64
- [DOUBLE_DASH]: string[];
65
- };
66
- }
67
- type TypeFlag<Schemas extends Flags$1> = ParsedFlags<{
68
- [flag in keyof Schemas]: InferFlagType<Schemas[flag]>;
69
- }>;
70
-
71
- type StripBrackets<Parameter extends string> = Parameter extends `<${infer ParameterName}>` | `[${infer ParameterName}]` ? ParameterName extends `${infer SpreadName}...` ? SpreadName : ParameterName : never;
72
- type ParameterType<Parameter extends string> = Parameter extends `<${infer _ParameterName}...>` | `[${infer _ParameterName}...]` ? string[] : Parameter extends `<${infer _ParameterName}>` ? string : Parameter extends `[${infer _ParameterName}]` ? string | undefined : never;
73
- type NonNullableParameters<T extends string[] | undefined> = T extends undefined ? [] : NonNullable<T>;
74
- type TransformParameters<C extends Command> = {
75
- [Parameter in NonNullableParameters<C["parameters"]>[number] as CamelCase<StripBrackets<Parameter>>]: ParameterType<Parameter>;
76
- };
77
- type MakeEventMap<T extends Commands> = {
78
- [K in keyof T]: [InterceptorContext];
79
- };
80
- type FallbackFlags<F extends Flags | undefined> = Equals<NonNullableFlag<F>["flags"], {}> extends true ? Dict<any> : NonNullableFlag<F>["flags"];
81
- type NonNullableFlag<F extends Flags | undefined> = TypeFlag<NonNullable<F>>;
82
- type ParseFlag<C extends Commands, N extends keyof C, GF extends GlobalFlagOptions = {}> = N extends keyof C ? OmitIndexSignature<NonNullableFlag<C[N]["flags"] & GF>["flags"]> : FallbackFlags<C[N]["flags"] & GF>["flags"];
83
- type ParseRaw<C extends Command, GF extends GlobalFlagOptions = {}> = NonNullableFlag<C["flags"] & GF> & {
84
- flags: FallbackFlags<C["flags"] & GF>;
85
- parameters: string[];
86
- mergedFlags: FallbackFlags<C["flags"] & GF> & NonNullableFlag<C["flags"] & GF>["unknownFlags"];
87
- };
88
- 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]>;
89
-
90
- type Locales = Dict<Dict<string>>;
91
- type TranslateFn = (name: string, ...args: string[]) => string | undefined;
92
- interface I18N {
93
- add: (locales: Locales) => void;
94
- t: TranslateFn;
95
- }
96
-
97
- interface Plugin<T extends Clerc = Clerc, U extends Clerc = Clerc> {
98
- setup: (cli: T) => U;
3
+ //#region ../utils/src/types/type-fest.d.ts
4
+ type LiteralUnion<LiteralType, BaseType> = LiteralType | (BaseType & Record<never, never>);
5
+ type Prettify<T> = { [K in keyof T]: T[K] } & {};
6
+ type Primitive = null | undefined | string | number | boolean | symbol | bigint;
7
+ type BuiltIns = Primitive | void | Date | RegExp;
8
+ type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown) | Promise<unknown>;
9
+ type UnknownArray = readonly unknown[];
10
+ type MapsSetsOrArrays = ReadonlyMap<unknown, unknown> | WeakMap<WeakKey, unknown> | ReadonlySet<unknown> | WeakSet<WeakKey> | UnknownArray;
11
+ type ConditionalDeepPrettify<T, E = never, I$1 = unknown> = T extends E ? T : T extends I$1 ? { [TypeKey in keyof T]: ConditionalDeepPrettify<T[TypeKey], E, I$1> } : T;
12
+ type DeepPrettify<T, E = never> = ConditionalDeepPrettify<T, E | NonRecursiveType | MapsSetsOrArrays, object>;
13
+ //#endregion
14
+ //#region ../utils/src/types/index.d.ts
15
+ type MaybeArray<T> = T | T[];
16
+ type PartialRequired<T, K$1 extends keyof T> = T & { [P in K$1]-?: T[P] };
17
+ type UnionToIntersection<U$1> = (U$1 extends any ? (k: U$1) => void : never) extends ((k: infer I) => void) ? I : never;
18
+ //#endregion
19
+ //#region ../parser/src/types.d.ts
20
+ type FlagDefaultValue<T = unknown> = T | (() => T);
21
+ /**
22
+ * Defines how a string input is converted to the target type T.
23
+ *
24
+ * @template T The target type.
25
+ */
26
+ type FlagTypeFunction<T = unknown> = (value: string) => T;
27
+ /**
28
+ * A callback function to conditionally stop parsing.
29
+ * When it returns true, parsing stops and remaining arguments are preserved in `ignored`.
30
+ *
31
+ * @param type - The type of the current argument: 'known-flag' or 'unknown-flag' for flags, 'parameter' for positional arguments
32
+ * @param arg - The current argument being processed
33
+ * @returns true to stop parsing, false to continue
34
+ */
35
+ type IgnoreFunction = (type: typeof KNOWN_FLAG | typeof UNKNOWN_FLAG | typeof PARAMETER, arg: string) => boolean;
36
+ type FlagType<T = unknown> = FlagTypeFunction<T> | readonly [FlagTypeFunction<T>];
37
+ interface BaseFlagOptions<T extends FlagType = FlagType> {
38
+ /**
39
+ * The type constructor or a function to convert the string value.
40
+ * To support multiple occurrences of a flag (e.g., --file a --file b), wrap the type in an array: [String], [Number].
41
+ * e.g., String, Number, [String], (val) => val.split(',')
42
+ */
43
+ type: T;
44
+ /** Aliases for the flag. */
45
+ alias?: MaybeArray<string>;
46
+ /** The default value of the flag. */
47
+ default?: unknown;
48
+ }
49
+ type FlagOptions = (BaseFlagOptions<BooleanConstructor> & {
50
+ /**
51
+ * Whether to enable the `--no-<flag>` syntax to set the value to false.
52
+ * Only useful for boolean flags.
53
+ * When set on a non-boolean flag, a type error will be shown.
54
+ *
55
+ * @default true
56
+ */
57
+ negatable?: boolean;
58
+ }) | (BaseFlagOptions & {
59
+ negatable?: never;
60
+ });
61
+ type FlagDefinitionValue = FlagOptions | FlagType;
62
+ type FlagsDefinition = Record<string, FlagDefinitionValue>;
63
+ type RawInputType = string | boolean;
64
+ interface ObjectInputType {
65
+ [key: string]: RawInputType | ObjectInputType;
99
66
  }
100
-
101
- type CommandType = RootType | string;
102
- type FlagOptions = FlagSchema & {
103
- description: string;
67
+ /**
68
+ * The parsed result.
69
+ * @template TFlags The specific flags type inferred from ParserOptions.
70
+ */
71
+ interface ParsedResult<TFlags extends Record<string, any>> {
72
+ /** Positional arguments or commands. */
73
+ parameters: string[];
74
+ /** Arguments after the `--` delimiter. */
75
+ doubleDash: string[];
76
+ /**
77
+ * The parsed flags.
78
+ * This is a strongly-typed object whose structure is inferred from the `flags` configuration in ParserOptions.
79
+ */
80
+ flags: TFlags;
81
+ /** The raw command-line arguments. */
82
+ raw: string[];
83
+ /** Unknown flags encountered during parsing. */
84
+ unknown: Record<string, RawInputType>;
85
+ /** Arguments that were not parsed due to ignore callback. */
86
+ ignored: string[];
87
+ }
88
+ type InferFlagDefault<T extends FlagDefinitionValue, Fallback> = T extends {
89
+ default: FlagDefaultValue<infer DefaultType>;
90
+ } ? DefaultType : Fallback;
91
+ type _InferFlags<T extends FlagsDefinition> = { [K in keyof T]: T[K] extends readonly [BooleanConstructor] | {
92
+ type: readonly [BooleanConstructor];
93
+ } ? number : T[K] extends ObjectConstructor | {
94
+ type: ObjectConstructor;
95
+ } ? ObjectInputType : T[K] extends readonly [FlagType<infer U>] | {
96
+ type: readonly [FlagType<infer U>];
97
+ } ? U[] | InferFlagDefault<T[K], never> : T[K] extends FlagType<infer U> | {
98
+ type: FlagType<infer U>;
99
+ } ? U | InferFlagDefault<T[K], [U] extends [boolean] ? never : undefined> : never };
100
+ /**
101
+ * An advanced utility type that infers the exact type of the `flags` object in the parsed result,
102
+ * based on the provided `flags` configuration object T.
103
+ *
104
+ * @template T The type of the flags configuration object.
105
+ */
106
+ type InferFlags<T extends FlagsDefinition> = Prettify<_InferFlags<T>>;
107
+ //#endregion
108
+ //#region ../parser/src/iterator.d.ts
109
+ declare const KNOWN_FLAG = "known-flag";
110
+ declare const UNKNOWN_FLAG = "unknown-flag";
111
+ declare const PARAMETER = "parameter";
112
+ //#endregion
113
+ //#region src/types/clerc.d.ts
114
+ type ErrorHandler = (error: unknown) => void;
115
+ //#endregion
116
+ //#region src/types/flag.d.ts
117
+ type ClercFlagOptions = FlagOptions & {
118
+ description: string;
104
119
  };
105
- type Flag = FlagOptions & {
106
- name: string;
120
+ type ClercFlagsDefinition = Record<string, ClercFlagOptions>;
121
+ //#endregion
122
+ //#region src/types/parameters.d.ts
123
+ type InferParameter<T extends string> = T extends `<${infer Name extends string}...>` | `[${infer Name extends string}...]` ? Record<Name, string[]> : T extends `<${infer Name extends string}>` ? Record<Name, string> : T extends `[${infer Name extends string}]` ? Record<Name, string | undefined> : never;
124
+ type InferParameters<T extends string[]> = T extends (infer U extends string)[] ? Prettify<UnionToIntersection<InferParameter<U>>> : never;
125
+ //#endregion
126
+ //#region src/types/context.d.ts
127
+ type AddStringIndex<T> = T & Record<string, any>;
128
+ type InferFlagsWithGlobal<C extends Command, GF extends ClercFlagsDefinition> = AddStringIndex<InferFlags<NonNullable<C["flags"]> & Omit<GF, keyof NonNullable<C["flags"]>>>>;
129
+ interface BaseContext<C extends Command = Command, GF extends ClercFlagsDefinition = {}> {
130
+ resolved: boolean;
131
+ command?: C;
132
+ calledAs?: string;
133
+ parameters: InferParameters<NonNullable<C["parameters"]>>;
134
+ flags: InferFlagsWithGlobal<C, GF>;
135
+ ignored: string[];
136
+ rawParsed: ParsedResult<InferFlagsWithGlobal<C, GF>>;
137
+ maybeMissingParameters?: boolean;
138
+ }
139
+ //#endregion
140
+ //#region src/types/command.d.ts
141
+ interface CommandCustomOptions {}
142
+ interface CommandOptions<Parameters extends string[] = string[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> extends CommandCustomOptions {
143
+ alias?: MaybeArray<string>;
144
+ parameters?: Parameters;
145
+ flags?: Flags;
146
+ /**
147
+ * A callback function to conditionally stop parsing. When it returns true, parsing stops and remaining arguments are preserved in ignored.
148
+ */
149
+ ignore?: IgnoreFunction;
150
+ }
151
+ interface Command<Name$1 extends string = string, Parameters extends string[] = string[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> extends CommandOptions<Parameters, Flags> {
152
+ name: Name$1;
153
+ description: string;
154
+ }
155
+ type CommandWithHandler<Name$1 extends string = string, Parameters extends string[] = string[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> = Command<Name$1, Parameters, Flags> & {
156
+ handler?: CommandHandler<Command<Name$1, Parameters, Flags>>;
107
157
  };
108
- type GlobalFlagOption = FlagSchema;
109
- type Flags = Dict<FlagOptions>;
110
- type GlobalFlagOptions = Dict<GlobalFlagOption>;
111
- declare interface CommandCustomProperties {
112
- }
113
- interface CommandOptions<P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags> extends CommandCustomProperties {
114
- alias?: A;
115
- parameters?: P;
116
- flags?: F;
158
+ type CommandsRecord = Record<string, Command>;
159
+ type CommandsMap = Map<string, Command>;
160
+ type MakeEmitterEvents<Commands extends CommandsRecord, GlobalFlags extends ClercFlagsDefinition = ClercFlagsDefinition> = { [K in keyof Commands]: [CommandHandlerContext<Commands[K], GlobalFlags>] };
161
+ type CommandHandlerContext<C extends Command = Command, GF extends ClercFlagsDefinition = ClercFlagsDefinition> = DeepPrettify<PartialRequired<BaseContext<C, GF>, "command" | "calledAs"> & {
162
+ resolved: true;
163
+ }>;
164
+ type CommandHandler<C extends Command = Command, GF extends ClercFlagsDefinition = ClercFlagsDefinition> = (context: CommandHandlerContext<C, GF>) => void;
165
+ //#endregion
166
+ //#region src/types/interceptor.d.ts
167
+ type InterceptorContext<C extends Command = Command, GF extends ClercFlagsDefinition = {}> = DeepPrettify<BaseContext<C, GF>>;
168
+ /**
169
+ * Function to call the next interceptor in the chain.
170
+ * **MUST** be awaited.
171
+ */
172
+ type InterceptorNext = () => void | Promise<void>;
173
+ type InterceptorHandler<C extends Command = Command, GF extends ClercFlagsDefinition = {}> = (context: InterceptorContext<C, GF>, next: InterceptorNext) => void | Promise<void>;
174
+ interface InterceptorObject<C extends Command = Command, GF extends ClercFlagsDefinition = {}> {
175
+ enforce?: "pre" | "normal" | "post";
176
+ handler: InterceptorHandler<C, GF>;
177
+ }
178
+ type Interceptor<C extends Command = Command, GF extends ClercFlagsDefinition = {}> = InterceptorHandler<C, GF> | InterceptorObject<C, GF>;
179
+ //#endregion
180
+ //#region src/types/plugin.d.ts
181
+ interface Plugin {
182
+ setup: (cli: Clerc) => void;
183
+ }
184
+ //#endregion
185
+ //#region src/cli.d.ts
186
+ interface CreateOptions {
187
+ name?: string;
188
+ scriptName?: string;
189
+ description?: string;
190
+ version?: string;
117
191
  }
118
- type Command<N extends string | RootType = string, O extends CommandOptions = CommandOptions> = O & {
119
- name: N;
120
- description: string;
121
- };
122
- type CommandAlias<N extends string | RootType = string, O extends CommandOptions = CommandOptions> = Command<N, O> & {
123
- __isAlias?: true;
124
- };
125
- type CommandWithHandler<N extends string | RootType = string, O extends CommandOptions = CommandOptions> = Command<N, O> & {
126
- handler?: HandlerInCommand<HandlerContext<Record<N, Command<N, O>> & Record<never, never>, N>>;
127
- };
128
- type Commands = Dict<Command> & {
129
- [Root]?: Command;
130
- };
131
192
  interface ParseOptions {
132
- argv?: string[];
133
- run?: boolean;
134
- }
135
- interface HandlerContext<C extends Commands = Commands, N extends keyof C = keyof C, GF extends GlobalFlagOptions = {}> {
136
- name?: LiteralUnion<N, string>;
137
- called?: string | RootType;
138
- resolved: boolean;
139
- hasRootOrAlias: boolean;
140
- hasRoot: boolean;
141
- raw: Simplify<ParseRaw<C[N], GF>>;
142
- parameters: Simplify<ParseParameters<C, N>>;
143
- unknownFlags: ParsedFlags["unknownFlags"];
144
- flags: Simplify<ParseFlag<C, N, GF> & Record<string, any>>;
145
- cli: Clerc<C, GF>;
146
- }
147
- type Handler<C extends Commands = Commands, K extends keyof C = keyof C, GF extends GlobalFlagOptions = {}> = (ctx: HandlerContext<C, K, GF>) => void;
148
- type HandlerInCommand<C extends HandlerContext> = (ctx: {
149
- [K in keyof C]: C[K];
150
- }) => void;
151
- type FallbackType<T, U> = {} extends T ? U : T;
152
- type InterceptorContext<C extends Commands = Commands> = HandlerContext<C> & {
153
- flags: FallbackType<TypeFlag<NonNullable<C[keyof C]["flags"]>>["flags"], Dict<any>>;
154
- };
155
- type Interceptor<C extends Commands = Commands> = InterceptorFn<C> | InterceptorObject<C>;
156
- type InterceptorFn<C extends Commands = Commands> = (ctx: InterceptorContext<C>, next: () => void) => void;
157
- interface InterceptorObject<C extends Commands = Commands> {
158
- enforce?: "pre" | "post";
159
- fn: InterceptorFn<C>;
160
- }
161
-
162
- declare const Root: unique symbol;
163
- type RootType = typeof Root;
164
- declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}> {
165
- #private;
166
- get i18n(): I18N;
167
- private constructor();
168
- get _name(): string;
169
- get _scriptName(): string;
170
- get _description(): string;
171
- get _version(): string;
172
- /**
173
- * @deprecated This is a typo. Use `_interceptor` instead.
174
- */
175
- get _inspectors(): Interceptor[];
176
- get _interceptors(): Interceptor[];
177
- get _commands(): C;
178
- get _flags(): GF;
179
- /**
180
- * Create a new cli
181
- *
182
- * @example
183
- *
184
- * ```ts
185
- * const cli = Clerc.create();
186
- * ```
187
- *
188
- * @param name
189
- * @param description
190
- * @param version
191
- * @returns
192
- */
193
- static create(name?: string, description?: string, version?: string): Clerc<{}, {}>;
194
- /**
195
- * Set the name of the cli
196
- *
197
- * @example
198
- *
199
- * ```ts
200
- * Clerc.create().name("test");
201
- * ```
202
- *
203
- * @param name
204
- * @returns
205
- */
206
- name(name: string): this;
207
- /**
208
- * Set the script name of the cli
209
- *
210
- * @example
211
- *
212
- * ```ts
213
- * Clerc.create().scriptName("test");
214
- * ```
215
- *
216
- * @param scriptName
217
- * @returns
218
- */
219
- scriptName(scriptName: string): this;
220
- /**
221
- * Set the description of the cli
222
- *
223
- * @example
224
- *
225
- * ```ts
226
- * Clerc.create().description("test cli");
227
- * ```
228
- *
229
- * @param description
230
- * @returns
231
- */
232
- description(description: string): this;
233
- /**
234
- * Set the version of the cli
235
- *
236
- * @example
237
- *
238
- * ```ts
239
- * Clerc.create().version("1.0.0");
240
- * ```
241
- *
242
- * @param version
243
- * @returns
244
- */
245
- version(version: string): this;
246
- /**
247
- * Set the Locale You must call this method once after you created the Clerc
248
- * instance.
249
- *
250
- * @example
251
- *
252
- * ```ts
253
- * Clerc.create()
254
- * .locale("en")
255
- * .command(...)
256
- * ```
257
- *
258
- * @param locale
259
- * @returns
260
- */
261
- locale(locale: string): this;
262
- /**
263
- * Set the fallback Locale You must call this method once after you created
264
- * the Clerc instance.
265
- *
266
- * @example
267
- *
268
- * ```ts
269
- * Clerc.create()
270
- * .fallbackLocale("en")
271
- * .command(...)
272
- * ```
273
- *
274
- * @param fallbackLocale
275
- * @returns
276
- */
277
- fallbackLocale(fallbackLocale: string): this;
278
- /**
279
- * Register a error handler
280
- *
281
- * @example
282
- *
283
- * ```ts
284
- * Clerc.create().errorHandler((err) => {
285
- * console.log(err);
286
- * });
287
- * ```
288
- *
289
- * @param handler
290
- * @returns
291
- */
292
- errorHandler(handler: (err: any) => void): this;
293
- /**
294
- * Register a command
295
- *
296
- * @example
297
- *
298
- * ```ts
299
- * Clerc.create().command("test", "test command", {
300
- * alias: "t",
301
- * flags: {
302
- * foo: {
303
- * alias: "f",
304
- * description: "foo flag",
305
- * },
306
- * },
307
- * });
308
- * ```
309
- *
310
- * @example
311
- *
312
- * ```ts
313
- * Clerc.create().command("", "root", {
314
- * flags: {
315
- * foo: {
316
- * alias: "f",
317
- * description: "foo flag",
318
- * },
319
- * },
320
- * });
321
- * ```
322
- *
323
- * @param name
324
- * @param description
325
- * @param options
326
- * @returns
327
- */
328
- 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>;
329
- 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>;
330
- /**
331
- * Register a global flag
332
- *
333
- * @example
334
- *
335
- * ```ts
336
- * Clerc.create().flag("help", "help", {
337
- * alias: "h",
338
- * type: Boolean,
339
- * });
340
- * ```
341
- *
342
- * @param name
343
- * @param description
344
- * @param options
345
- * @returns
346
- */
347
- flag<N extends string, O extends GlobalFlagOption>(name: N, description: string, options: O): this & Clerc<C, GF & Record<N, O>>;
348
- /**
349
- * Register a handler
350
- *
351
- * @example
352
- *
353
- * ```ts
354
- * Clerc.create()
355
- * .command("test", "test command")
356
- * .on("test", (ctx) => {
357
- * console.log(ctx);
358
- * });
359
- * ```
360
- *
361
- * @param name
362
- * @param handler
363
- * @returns
364
- */
365
- on<K extends LiteralUnion<keyof CM, string | RootType>, CM extends this["_commands"] = this["_commands"]>(name: K, handler: Handler<CM, K, this["_flags"]>): this;
366
- /**
367
- * Use a plugin
368
- *
369
- * @example
370
- *
371
- * ```ts
372
- * Clerc.create().use(plugin);
373
- * ```
374
- *
375
- * @param plugin
376
- * @returns
377
- */
378
- use<T extends Clerc, U extends Clerc>(plugin: Plugin<T, U>): this & Clerc<C & U["_commands"]> & U;
379
- /**
380
- * @deprecated This is a typo. Use `intercetor()` instead.
381
- */
382
- inspector(interceptor: Interceptor): this;
383
- /**
384
- * Register a interceptor
385
- *
386
- * @example
387
- *
388
- * ```ts
389
- * Clerc.create().interceptor((ctx, next) => {
390
- * console.log(ctx);
391
- * next();
392
- * });
393
- * ```
394
- *
395
- * @param interceptor
396
- * @returns
397
- */
398
- interceptor(interceptor: Interceptor): this;
399
- /**
400
- * Parse the command line arguments
401
- *
402
- * @example
403
- *
404
- * ```ts
405
- * Clerc.create().parse(process.argv.slice(2)); // Optional
406
- * ```
407
- *
408
- * @param args
409
- * @param optionsOrArgv
410
- * @returns
411
- */
412
- parse(optionsOrArgv?: string[] | ParseOptions): this;
413
- /**
414
- * Run matched command
415
- *
416
- * @example
417
- *
418
- * ```ts
419
- * Clerc.create().parse({ run: false }).runMatchedCommand();
420
- * ```
421
- *
422
- * @returns
423
- */
424
- runMatchedCommand(): this;
425
- }
426
-
427
- declare class CommandExistsError extends Error {
428
- commandName: string;
429
- constructor(commandName: string, t: TranslateFn);
430
- }
193
+ run?: boolean;
194
+ }
195
+ declare class Clerc<Commands extends CommandsRecord = {}, GlobalFlags extends ClercFlagsDefinition = {}> {
196
+ #private;
197
+ private constructor();
198
+ get _name(): string;
199
+ get _scriptName(): string;
200
+ get _description(): string;
201
+ get _version(): string;
202
+ get _commands(): CommandsMap;
203
+ get _globalFlags(): GlobalFlags;
204
+ static create(options?: CreateOptions): Clerc;
205
+ name(name: string): this;
206
+ scriptName(scriptName: string): this;
207
+ description(description: string): this;
208
+ version(version: string): this;
209
+ use(plugin: Plugin): this;
210
+ errorHandler(handler: ErrorHandler$1): this;
211
+ command<Name$1 extends string, Parameters extends string[] = [], Flags extends ClercFlagsDefinition = {}>(command: CommandWithHandler<Name$1, [...Parameters], Flags>): Clerc<Commands & Record<string, CommandWithHandler<Name$1, Parameters, Flags>>, GlobalFlags>;
212
+ command<Name$1 extends string, Parameters extends string[] = [], Flags extends ClercFlagsDefinition = {}>(name: Name$1 extends keyof Commands ? ["COMMAND ALREADY EXISTS"] : Name$1, description: string, options?: CommandOptions<[...Parameters], Flags>): Clerc<Commands & Record<Name$1, Command<Name$1, Parameters, Flags>>, GlobalFlags>;
213
+ globalFlag<Name$1 extends string, Flag extends FlagDefinitionValue>(name: Name$1, description: string, options: Flag): Clerc<Commands, GlobalFlags & Record<Name$1, Flag>>;
214
+ interceptor(interceptor: Interceptor<Command, GlobalFlags>): this;
215
+ on<Name$1 extends LiteralUnion<keyof Commands, string>>(name: Name$1, handler: CommandHandler<Commands[Name$1], GlobalFlags>): this;
216
+ run(): void;
217
+ parse(argv?: string[], {
218
+ run
219
+ }?: ParseOptions): this;
220
+ }
221
+ //#endregion
222
+ //#region src/commands.d.ts
223
+ declare function resolveCommand(commandsMap: CommandsMap, parameters: string[]): [Command, string] | [undefined, undefined];
224
+ //#endregion
225
+ //#region src/errors.d.ts
431
226
  declare class NoSuchCommandError extends Error {
432
- commandName: string;
433
- constructor(commandName: string, t: TranslateFn);
227
+ commandName: string;
228
+ constructor(commandName: string);
434
229
  }
435
230
  declare class NoCommandGivenError extends Error {
436
- constructor(t: TranslateFn);
437
- }
438
- declare class CommandNameConflictError extends Error {
439
- n1: string;
440
- n2: string;
441
- constructor(n1: string, n2: string, t: TranslateFn);
442
- }
443
- declare class ScriptNameNotSetError extends Error {
444
- constructor(t: TranslateFn);
445
- }
446
- declare class DescriptionNotSetError extends Error {
447
- constructor(t: TranslateFn);
448
- }
449
- declare class VersionNotSetError extends Error {
450
- constructor(t: TranslateFn);
451
- }
452
- declare class InvalidCommandNameError extends Error {
453
- commandName: string;
454
- constructor(commandName: string, t: TranslateFn);
455
- }
456
- declare class LocaleNotCalledFirstError extends Error {
457
- constructor(t: TranslateFn);
458
- }
459
-
460
- type MaybeArray<T> = T | T[];
461
-
462
- declare const definePlugin: <T extends Clerc<{}, {}>, U extends Clerc<{}, {}>>(p: Plugin<T, U>) => Plugin<T, U>;
463
- declare const defineHandler: <C extends Clerc<{}, {}>, K extends keyof C["_commands"]>(_cli: C, _key: K, handler: Handler<C["_commands"], K>) => Handler<C["_commands"], K>;
464
- declare const defineInterceptor: <C extends Clerc<{}, {}>>(_cli: C, interceptor: Interceptor<C["_commands"]>) => Interceptor<C["_commands"]>;
465
- /**
466
- * @deprecated This is a typo. Use `defineInterceptor` instead.
467
- */
468
- declare const defineInspector: <C extends Clerc<{}, {}>>(_cli: C, interceptor: Interceptor<C["_commands"]>) => Interceptor<C["_commands"]>;
469
- 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>>;
470
-
471
- declare function resolveFlattenCommands(commands: Commands, t: TranslateFn): Map<string[] | typeof Root, CommandAlias>;
472
- declare function resolveCommand(commands: Commands, argv: string[], t: TranslateFn): [Command<string | RootType> | undefined, string[] | RootType | undefined];
473
- declare const resolveArgv: () => string[];
474
- declare function compose(interceptors: Interceptor[]): (ctx: InterceptorContext) => void;
475
- declare const isValidName: (name: CommandType) => boolean;
476
- declare const withBrackets: (s: string, isOptional?: boolean) => string;
477
- declare const formatCommandName: (name: string | string[] | RootType) => string;
478
- declare const detectLocale: () => string;
479
- declare const stripFlags: (argv: string[]) => string[];
480
-
481
- export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandType, CommandWithHandler, Commands, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, GlobalFlagOption, GlobalFlagOptions, Handler, HandlerContext, HandlerInCommand, I18N, Interceptor, InterceptorContext, InterceptorFn, InterceptorObject, InvalidCommandNameError, LocaleNotCalledFirstError, Locales, MakeEventMap, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, Root, RootType, ScriptNameNotSetError, TranslateFn, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, defineInterceptor, definePlugin, detectLocale, formatCommandName, isValidName, resolveArgv, resolveCommand, resolveFlattenCommands, stripFlags, withBrackets };
231
+ constructor();
232
+ }
233
+ declare class InvalidCommandError extends Error {
234
+ constructor(message: string);
235
+ }
236
+ declare class MissingRequiredMetadataError extends Error {
237
+ constructor(metadataName: string);
238
+ }
239
+ declare class InvalidParametersError extends Error {
240
+ constructor(message: string);
241
+ }
242
+ //#endregion
243
+ //#region src/helpers.d.ts
244
+ declare const defineCommand: <Name$1 extends string, Parameters extends string[] = [], Flags extends ClercFlagsDefinition = {}>(command: CommandWithHandler<Name$1, [...Parameters], Flags>) => CommandWithHandler<Name$1, [...Parameters], Flags>;
245
+ //#endregion
246
+ //#region src/ignore.d.ts
247
+ declare function createStopAtFirstParameter(): IgnoreFunction;
248
+ //#endregion
249
+ //#region src/plugin.d.ts
250
+ declare const definePlugin: (plugin: Plugin) => Plugin;
251
+ //#endregion
252
+ export { BaseContext, Clerc, ClercFlagOptions, ClercFlagsDefinition, Command, CommandCustomOptions, CommandHandler, CommandHandlerContext, CommandOptions, CommandWithHandler, CommandsMap, CommandsRecord, ErrorHandler, InferParameters, Interceptor, InterceptorContext, InterceptorHandler, InterceptorNext, InterceptorObject, InvalidCommandError, InvalidParametersError, MakeEmitterEvents, MissingRequiredMetadataError, NoCommandGivenError, NoSuchCommandError, Plugin, createStopAtFirstParameter, defineCommand, definePlugin, resolveCommand };