@clerc/core 1.0.0-beta.15 → 1.0.0-beta.16
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/chunk-BAz01cYq.js +18 -0
- package/dist/index.d.ts +84 -21
- package/dist/index.js +93 -12
- package/package.json +3 -3
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __export = (all, symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) {
|
|
6
|
+
__defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
if (symbols) {
|
|
12
|
+
__defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
13
|
+
}
|
|
14
|
+
return target;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { __export as t };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { ErrorHandler as ErrorHandler$1 } from "lite-emit";
|
|
2
2
|
|
|
3
|
-
//#region rolldown:runtime
|
|
4
|
-
//#endregion
|
|
5
3
|
//#region ../parser/src/errors.d.ts
|
|
6
4
|
declare class InvalidSchemaError extends Error {
|
|
7
5
|
constructor(message: string);
|
|
@@ -12,7 +10,7 @@ declare class MissingRequiredFlagError extends Error {
|
|
|
12
10
|
//#endregion
|
|
13
11
|
//#region ../parser/src/flag-types.d.ts
|
|
14
12
|
/**
|
|
15
|
-
* Creates a
|
|
13
|
+
* Creates a Enum type function that validates the input against allowed values.
|
|
16
14
|
* The display name will be formatted as "value1 | value2 | ..." for help output.
|
|
17
15
|
*
|
|
18
16
|
* @param values - Array of allowed string values
|
|
@@ -21,11 +19,11 @@ declare class MissingRequiredFlagError extends Error {
|
|
|
21
19
|
*
|
|
22
20
|
* @example
|
|
23
21
|
* ```typescript
|
|
24
|
-
* const format =
|
|
22
|
+
* const format = Enum(['json', 'yaml', 'xml']);
|
|
25
23
|
* // Help output will show: json | yaml | xml
|
|
26
24
|
* ```
|
|
27
25
|
*/
|
|
28
|
-
declare function
|
|
26
|
+
declare function Enum<T extends string>(...values: T[]): FlagTypeFunction<T>;
|
|
29
27
|
//#endregion
|
|
30
28
|
//#region ../utils/src/types/type-fest.d.ts
|
|
31
29
|
type LiteralUnion<LiteralType, BaseType> = LiteralType | (BaseType & Record<never, never>);
|
|
@@ -46,21 +44,24 @@ type UnionToIntersection<U$1> = (U$1 extends any ? (k: U$1) => void : never) ext
|
|
|
46
44
|
type CamelCase<S extends string> = S extends `${infer Head} ${infer Tail}` ? `${Head}${Capitalize<CamelCase<Tail>>}` : S extends `${infer Head}-${infer Tail}` ? `${Head}${Capitalize<CamelCase<Tail>>}` : S;
|
|
47
45
|
//#endregion
|
|
48
46
|
//#region ../parser/src/types.d.ts
|
|
49
|
-
|
|
47
|
+
interface FlagDefaultValueFunction<T> {
|
|
48
|
+
(): T;
|
|
50
49
|
display?: string;
|
|
51
|
-
}
|
|
50
|
+
}
|
|
51
|
+
type FlagDefaultValue<T = unknown> = T | FlagDefaultValueFunction<T>;
|
|
52
52
|
/**
|
|
53
53
|
* Defines how a string input is converted to the target type T.
|
|
54
54
|
*
|
|
55
55
|
* @template T The target type.
|
|
56
56
|
*/
|
|
57
|
-
|
|
57
|
+
interface FlagTypeFunction<T = unknown> {
|
|
58
|
+
(value: string): T;
|
|
58
59
|
/**
|
|
59
60
|
* Optional display name for the type, useful in help output.
|
|
60
61
|
* If provided, this will be shown instead of the function name.
|
|
61
62
|
*/
|
|
62
63
|
display?: string;
|
|
63
|
-
}
|
|
64
|
+
}
|
|
64
65
|
/**
|
|
65
66
|
* A callback function to conditionally stop parsing.
|
|
66
67
|
* When it returns true, parsing stops and remaining arguments are preserved in `ignored`.
|
|
@@ -184,7 +185,7 @@ declare function createParser<T extends FlagsDefinition>(options?: ParserOptions
|
|
|
184
185
|
};
|
|
185
186
|
declare const parse: <T extends FlagsDefinition>(args: string[], options?: ParserOptions<T>) => ParsedResult<InferFlags<T>>;
|
|
186
187
|
declare namespace index_d_exports {
|
|
187
|
-
export { BaseFlagOptions,
|
|
188
|
+
export { BaseFlagOptions, DOUBLE_DASH, Enum, FlagDefaultValue, FlagDefaultValueFunction, FlagDefinitionValue, FlagOptions, FlagType, FlagTypeFunction, FlagsDefinition, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG, MissingRequiredFlagError, ObjectInputType, PARAMETER, ParsedResult, ParserOptions, RawInputType, UNKNOWN_FLAG, createParser, parse };
|
|
188
189
|
}
|
|
189
190
|
//#endregion
|
|
190
191
|
//#region src/types/clerc.d.ts
|
|
@@ -195,13 +196,31 @@ interface FlagCustomOptions {}
|
|
|
195
196
|
type ClercFlagOptions = FlagOptions & FlagCustomOptions;
|
|
196
197
|
type ClercGlobalFlagDefinitionValue = ClercFlagOptions | FlagType;
|
|
197
198
|
type ClercFlagOptionsWithDescription = ClercFlagOptions & {
|
|
198
|
-
description
|
|
199
|
+
description?: string;
|
|
199
200
|
};
|
|
200
201
|
type ClercFlagsDefinition = Record<string, ClercFlagOptionsWithDescription>;
|
|
201
202
|
//#endregion
|
|
203
|
+
//#region src/types/constraint.d.ts
|
|
204
|
+
/**
|
|
205
|
+
* Creates an enum constraint function that validates the input against a set of allowed values.
|
|
206
|
+
*
|
|
207
|
+
* @template T - The union type of allowed string values. Just a marker type to help with inference.
|
|
208
|
+
*/
|
|
209
|
+
interface ConstraintFunction<T = string> {
|
|
210
|
+
(value: string): void;
|
|
211
|
+
display?: string;
|
|
212
|
+
}
|
|
213
|
+
//#endregion
|
|
202
214
|
//#region src/types/parameters.d.ts
|
|
203
|
-
type
|
|
204
|
-
type
|
|
215
|
+
type InferStringParameter<T extends string, Type$1 = string> = T extends `<${infer Name extends string}...>` | `[${infer Name extends string}...]` ? Record<CamelCase<Name>, Type$1[]> : T extends `<${infer Name extends string}>` ? Record<CamelCase<Name>, Type$1> : T extends `[${infer Name extends string}]` ? Record<CamelCase<Name>, Type$1 | undefined> : never;
|
|
216
|
+
type InferParameter<T extends Parameter> = T extends string ? InferStringParameter<T> : T extends ParameterDefinition ? T["constraint"] extends ConstraintFunction<infer U> ? InferStringParameter<T["key"], U> : InferStringParameter<T["key"]> : never;
|
|
217
|
+
type InferParameters<T extends readonly Parameter[]> = T extends readonly (infer U extends Parameter)[] ? Prettify<UnionToIntersection<InferParameter<U>>> : never;
|
|
218
|
+
interface ParameterDefinition {
|
|
219
|
+
key: string;
|
|
220
|
+
description?: string;
|
|
221
|
+
constraint?: ConstraintFunction;
|
|
222
|
+
}
|
|
223
|
+
type Parameter = string | ParameterDefinition;
|
|
205
224
|
//#endregion
|
|
206
225
|
//#region src/types/context.d.ts
|
|
207
226
|
type AddStringIndex<T> = T & Record<string, any>;
|
|
@@ -221,7 +240,7 @@ interface BaseContext<C extends Command = Command, GF extends ClercFlagsDefiniti
|
|
|
221
240
|
//#endregion
|
|
222
241
|
//#region src/types/command.d.ts
|
|
223
242
|
interface CommandCustomOptions {}
|
|
224
|
-
interface CommandOptions<Parameters extends readonly
|
|
243
|
+
interface CommandOptions<Parameters extends readonly Parameter[] = readonly Parameter[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> extends CommandCustomOptions {
|
|
225
244
|
alias?: MaybeArray<string>;
|
|
226
245
|
parameters?: Parameters;
|
|
227
246
|
flags?: Flags;
|
|
@@ -230,11 +249,11 @@ interface CommandOptions<Parameters extends readonly string[] = readonly string[
|
|
|
230
249
|
*/
|
|
231
250
|
ignore?: IgnoreFunction;
|
|
232
251
|
}
|
|
233
|
-
interface Command<Name$1 extends string = string, Parameters extends readonly
|
|
252
|
+
interface Command<Name$1 extends string = string, Parameters extends readonly Parameter[] = readonly Parameter[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> extends CommandOptions<Parameters, Flags> {
|
|
234
253
|
name: Name$1;
|
|
235
|
-
description
|
|
254
|
+
description?: string;
|
|
236
255
|
}
|
|
237
|
-
type CommandWithHandler<Name$1 extends string = string, Parameters extends readonly
|
|
256
|
+
type CommandWithHandler<Name$1 extends string = string, Parameters extends readonly Parameter[] = readonly Parameter[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> = Command<Name$1, Parameters, Flags> & {
|
|
238
257
|
handler?: CommandHandler<Command<Name$1, Parameters, Flags>>;
|
|
239
258
|
};
|
|
240
259
|
type CommandsRecord = Record<string, Command>;
|
|
@@ -292,9 +311,11 @@ declare class Clerc<Commands extends CommandsRecord = {}, GlobalFlags extends Cl
|
|
|
292
311
|
version(version: string): this;
|
|
293
312
|
use(plugin: Plugin): this;
|
|
294
313
|
errorHandler(handler: ErrorHandler$1): this;
|
|
295
|
-
command<Name$1 extends string, const Parameters extends readonly
|
|
296
|
-
command<Name$1 extends string, const Parameters extends readonly
|
|
314
|
+
command<Name$1 extends string, const Parameters extends readonly Parameter[] = readonly [], Flags extends ClercFlagsDefinition = {}>(command: CommandWithHandler<Name$1, Parameters, Flags>): Clerc<Commands & Record<string, CommandWithHandler<Name$1, Parameters, Flags>>, GlobalFlags>;
|
|
315
|
+
command<Name$1 extends string, const Parameters extends readonly Parameter[] = readonly [], Flags extends ClercFlagsDefinition = {}>(name: Name$1 extends keyof Commands ? ["COMMAND ALREADY EXISTS"] : Name$1, options?: CommandOptions<Parameters, Flags>): Clerc<Commands & Record<Name$1, Command<Name$1, Parameters, Flags>>, GlobalFlags>;
|
|
316
|
+
command<Name$1 extends string, const Parameters extends readonly Parameter[] = readonly [], 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>;
|
|
297
317
|
globalFlag<Name$1 extends string, Flag extends ClercGlobalFlagDefinitionValue>(name: Name$1, description: string, options: Flag): Clerc<Commands, GlobalFlags & Record<Name$1, Flag>>;
|
|
318
|
+
globalFlag<Name$1 extends string, Flag extends ClercGlobalFlagDefinitionValue>(name: Name$1, options: Flag): Clerc<Commands, GlobalFlags & Record<Name$1, Flag>>;
|
|
298
319
|
interceptor(interceptor: Interceptor<Command, GlobalFlags>): this;
|
|
299
320
|
on<Name$1 extends LiteralUnion<keyof Commands, string>>(name: Name$1, handler: CommandHandler<Commands[Name$1], GlobalFlags>): this;
|
|
300
321
|
run(): Promise<void>;
|
|
@@ -303,6 +324,45 @@ declare class Clerc<Commands extends CommandsRecord = {}, GlobalFlags extends Cl
|
|
|
303
324
|
//#endregion
|
|
304
325
|
//#region src/commands.d.ts
|
|
305
326
|
declare function resolveCommand(commandsMap: CommandsMap, parameters: string[]): [Command, string] | [undefined, undefined];
|
|
327
|
+
declare namespace constraint_d_exports {
|
|
328
|
+
export { Custom, Enum$1 as Enum, Range, Regex };
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Creates an enum constraint function that validates the input against a set of allowed values.
|
|
332
|
+
*
|
|
333
|
+
* @param values - Array of allowed string values
|
|
334
|
+
* @returns A ConstraintFunction that validates the input value
|
|
335
|
+
* @throws {Error} If the value is not in the allowed values list
|
|
336
|
+
*/
|
|
337
|
+
declare function Enum$1<T extends string>(...values: T[]): ConstraintFunction<T>;
|
|
338
|
+
/**
|
|
339
|
+
* Creates a range constraint function that validates the input is a number within the specified range.
|
|
340
|
+
*
|
|
341
|
+
* @param min - The minimum acceptable value (inclusive)
|
|
342
|
+
* @param max - The maximum acceptable value (inclusive)
|
|
343
|
+
* @returns A ConstraintFunction that validates the input value
|
|
344
|
+
* @throws {Error} If the value is not a number or is outside the specified range
|
|
345
|
+
*/
|
|
346
|
+
declare function Range(min: number, max: number): ConstraintFunction;
|
|
347
|
+
/**
|
|
348
|
+
* Creates a regex constraint function that validates the input against the provided pattern.
|
|
349
|
+
*
|
|
350
|
+
* @param pattern - The regular expression pattern to validate against
|
|
351
|
+
* @param description - Optional description for display purposes
|
|
352
|
+
* @returns A ConstraintFunction that validates the input value
|
|
353
|
+
* @throws {Error} If the value does not match the regex pattern
|
|
354
|
+
*/
|
|
355
|
+
declare function Regex(pattern: RegExp, description?: string): ConstraintFunction;
|
|
356
|
+
/**
|
|
357
|
+
* Just an utility to create custom constraints, helps you set the display name.
|
|
358
|
+
*
|
|
359
|
+
* @param validator - A function that validates the input value. Should return true if valid, false or throw an error if invalid.
|
|
360
|
+
* @param display - Optional display name for the constraint, useful in help output.
|
|
361
|
+
* @param errorMessage - Optional function to generate a custom error message when validation fails.
|
|
362
|
+
* @returns A ConstraintFunction that applies the custom validation logic.
|
|
363
|
+
* @throws {Error} If the validator returns false or throws an error.
|
|
364
|
+
*/
|
|
365
|
+
declare function Custom(validator: (value: string) => boolean | void, display?: string, errorMessage?: (value: string) => string): ConstraintFunction;
|
|
306
366
|
//#endregion
|
|
307
367
|
//#region src/errors.d.ts
|
|
308
368
|
declare class NoSuchCommandError extends Error {
|
|
@@ -321,9 +381,12 @@ declare class MissingRequiredMetadataError extends Error {
|
|
|
321
381
|
declare class InvalidParametersError extends Error {
|
|
322
382
|
constructor(message: string);
|
|
323
383
|
}
|
|
384
|
+
declare namespace flag_types_d_exports {
|
|
385
|
+
export { Enum };
|
|
386
|
+
}
|
|
324
387
|
//#endregion
|
|
325
388
|
//#region src/helpers.d.ts
|
|
326
|
-
declare const defineCommand: <Name$1 extends string, const Parameters extends readonly
|
|
389
|
+
declare const defineCommand: <Name$1 extends string, const Parameters extends readonly Parameter[] = readonly [], Flags extends ClercFlagsDefinition = {}>(command: Command<Name$1, Parameters, Flags>, handler?: NoInfer<CommandHandler<Command<Name$1, Parameters, Flags>>>) => CommandWithHandler<Name$1, Parameters, Flags>;
|
|
327
390
|
//#endregion
|
|
328
391
|
//#region src/ignore.d.ts
|
|
329
392
|
declare function createStopAtFirstParameter(): IgnoreFunction;
|
|
@@ -331,4 +394,4 @@ declare function createStopAtFirstParameter(): IgnoreFunction;
|
|
|
331
394
|
//#region src/plugin.d.ts
|
|
332
395
|
declare const definePlugin: (plugin: Plugin) => Plugin;
|
|
333
396
|
//#endregion
|
|
334
|
-
export { BaseContext,
|
|
397
|
+
export { BaseContext, Clerc, ClercFlagOptions, ClercFlagOptionsWithDescription, ClercFlagsDefinition, ClercGlobalFlagDefinitionValue, Command, CommandCustomOptions, CommandHandler, CommandHandlerContext, CommandOptions, CommandWithHandler, CommandsMap, CommandsRecord, ConstraintFunction, constraint_d_exports as Constraints, ContextStore, type CreateOptions, DOUBLE_DASH, ErrorHandler, FlagCustomOptions, InferParameters, Interceptor, InterceptorContext, InterceptorHandler, InterceptorNext, InterceptorObject, InvalidCommandError, InvalidParametersError, InvalidSchemaError, KNOWN_FLAG, MakeEmitterEvents, MissingRequiredMetadataError, NoCommandSpecifiedError, NoSuchCommandError, PARAMETER, Parameter, ParameterDefinition, type ParseOptions, type index_d_exports as Parser, Plugin, flag_types_d_exports as Types, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, resolveCommand };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as __export } from "./chunk-BAz01cYq.js";
|
|
2
|
+
import { DOUBLE_DASH, DOUBLE_DASH as DOUBLE_DASH$1, Enum, InvalidSchemaError, KNOWN_FLAG, PARAMETER, PARAMETER as PARAMETER$1, UNKNOWN_FLAG, parse } from "@clerc/parser";
|
|
2
3
|
import { LiteEmit } from "lite-emit";
|
|
3
4
|
|
|
4
5
|
//#region ../utils/src/index.ts
|
|
@@ -111,11 +112,12 @@ function _parseParameters(definitions, parameters) {
|
|
|
111
112
|
const result = {};
|
|
112
113
|
let hasOptional = false;
|
|
113
114
|
for (const [i, definition] of definitions.entries()) {
|
|
114
|
-
const
|
|
115
|
-
|
|
115
|
+
const definitionKey = typeof definition === "string" ? definition : definition.key;
|
|
116
|
+
const match = definitionKey.match(PARAMETER_REGEX);
|
|
117
|
+
if (!match || !isParameterDefinitionBracketsValid(definitionKey)) throw new InvalidParametersError(`Invalid parameter definition: ${definitionKey}`);
|
|
116
118
|
const name = camelCase(match[2]);
|
|
117
119
|
const isVariadic = !!match[3];
|
|
118
|
-
const isRequired =
|
|
120
|
+
const isRequired = definitionKey.startsWith("<");
|
|
119
121
|
if (name in result) throw new InvalidParametersError(`Duplicate parameter name: ${name}`);
|
|
120
122
|
if (isVariadic && i !== definitions.length - 1) throw new InvalidParametersError("Variadic parameter must be the last parameter in the definition.");
|
|
121
123
|
if (isRequired) {
|
|
@@ -123,6 +125,10 @@ function _parseParameters(definitions, parameters) {
|
|
|
123
125
|
} else hasOptional = true;
|
|
124
126
|
const value = isVariadic ? parameters.slice(i) : parameters[i];
|
|
125
127
|
if (isRequired && (isVariadic ? value.length === 0 : value === void 0)) throw new InvalidParametersError(`Missing required ${isVariadic ? "variadic " : ""}parameter: ${name}`);
|
|
128
|
+
if (typeof definition !== "string" && definition.constraint) {
|
|
129
|
+
if (isVariadic) for (const v of value) definition.constraint(v);
|
|
130
|
+
else if (value !== void 0) definition.constraint(value);
|
|
131
|
+
}
|
|
126
132
|
result[name] = value;
|
|
127
133
|
}
|
|
128
134
|
return result;
|
|
@@ -238,11 +244,12 @@ var Clerc = class Clerc {
|
|
|
238
244
|
if (this.#commands.has(name)) throw new InvalidCommandError(`Command with name "${name}" already exists.`);
|
|
239
245
|
for (const alias of aliases) if (this.#commands.has(alias)) throw new InvalidCommandError(`Command with name "${alias}" already exists.`);
|
|
240
246
|
}
|
|
241
|
-
command(nameOrCommandObject,
|
|
247
|
+
command(nameOrCommandObject, descriptionOrOptions, options) {
|
|
248
|
+
const isDescription = typeof descriptionOrOptions === "string";
|
|
242
249
|
const command = typeof nameOrCommandObject === "string" ? {
|
|
243
250
|
name: nameOrCommandObject,
|
|
244
|
-
description,
|
|
245
|
-
...options
|
|
251
|
+
description: isDescription ? descriptionOrOptions : void 0,
|
|
252
|
+
...isDescription ? options : descriptionOrOptions
|
|
246
253
|
} : nameOrCommandObject;
|
|
247
254
|
const aliases = toArray(command?.alias ?? []);
|
|
248
255
|
this.#callWithErrorHandler(() => this.#validateCommandNameAndAlias(command.name, aliases));
|
|
@@ -254,10 +261,11 @@ var Clerc = class Clerc {
|
|
|
254
261
|
if (command.handler) this.on(command.name, command.handler);
|
|
255
262
|
return this;
|
|
256
263
|
}
|
|
257
|
-
globalFlag(name,
|
|
264
|
+
globalFlag(name, descriptionOrOptions, options) {
|
|
265
|
+
const isDescription = typeof descriptionOrOptions === "string";
|
|
258
266
|
this.#globalFlags[name] = {
|
|
259
|
-
description,
|
|
260
|
-
...options
|
|
267
|
+
description: isDescription ? descriptionOrOptions : void 0,
|
|
268
|
+
...isDescription ? options : descriptionOrOptions
|
|
261
269
|
};
|
|
262
270
|
return this;
|
|
263
271
|
}
|
|
@@ -271,7 +279,6 @@ var Clerc = class Clerc {
|
|
|
271
279
|
}
|
|
272
280
|
#validate() {
|
|
273
281
|
if (!this.#scriptName) throw new MissingRequiredMetadataError("script name");
|
|
274
|
-
if (!this.#description) throw new MissingRequiredMetadataError("description");
|
|
275
282
|
if (!this.#version) throw new MissingRequiredMetadataError("version");
|
|
276
283
|
}
|
|
277
284
|
#parseArgv(argv, command) {
|
|
@@ -328,6 +335,80 @@ var Clerc = class Clerc {
|
|
|
328
335
|
}
|
|
329
336
|
};
|
|
330
337
|
|
|
338
|
+
//#endregion
|
|
339
|
+
//#region src/constraint.ts
|
|
340
|
+
var constraint_exports = /* @__PURE__ */ __export({
|
|
341
|
+
Custom: () => Custom,
|
|
342
|
+
Enum: () => Enum$1,
|
|
343
|
+
Range: () => Range,
|
|
344
|
+
Regex: () => Regex
|
|
345
|
+
});
|
|
346
|
+
/**
|
|
347
|
+
* Creates an enum constraint function that validates the input against a set of allowed values.
|
|
348
|
+
*
|
|
349
|
+
* @param values - Array of allowed string values
|
|
350
|
+
* @returns A ConstraintFunction that validates the input value
|
|
351
|
+
* @throws {Error} If the value is not in the allowed values list
|
|
352
|
+
*/
|
|
353
|
+
function Enum$1(...values) {
|
|
354
|
+
function fn(value) {
|
|
355
|
+
if (!values.includes(value)) throw new Error(`Invalid value: ${value}. Must be one of: ${values.join(", ")}`);
|
|
356
|
+
}
|
|
357
|
+
fn.display = values.join(" | ");
|
|
358
|
+
return fn;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Creates a range constraint function that validates the input is a number within the specified range.
|
|
362
|
+
*
|
|
363
|
+
* @param min - The minimum acceptable value (inclusive)
|
|
364
|
+
* @param max - The maximum acceptable value (inclusive)
|
|
365
|
+
* @returns A ConstraintFunction that validates the input value
|
|
366
|
+
* @throws {Error} If the value is not a number or is outside the specified range
|
|
367
|
+
*/
|
|
368
|
+
function Range(min, max) {
|
|
369
|
+
function fn(value) {
|
|
370
|
+
const num = Number(value);
|
|
371
|
+
if (Number.isNaN(num) || num < min || num > max) throw new Error(`Invalid value: ${value}. Must be a number between ${min} and ${max}`);
|
|
372
|
+
}
|
|
373
|
+
fn.display = `${min}-${max}`;
|
|
374
|
+
return fn;
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Creates a regex constraint function that validates the input against the provided pattern.
|
|
378
|
+
*
|
|
379
|
+
* @param pattern - The regular expression pattern to validate against
|
|
380
|
+
* @param description - Optional description for display purposes
|
|
381
|
+
* @returns A ConstraintFunction that validates the input value
|
|
382
|
+
* @throws {Error} If the value does not match the regex pattern
|
|
383
|
+
*/
|
|
384
|
+
function Regex(pattern, description) {
|
|
385
|
+
function fn(value) {
|
|
386
|
+
if (!pattern.test(value)) throw new Error(`Invalid value: ${value}. Must match pattern: ${pattern}`);
|
|
387
|
+
}
|
|
388
|
+
fn.display = description ?? pattern.toString();
|
|
389
|
+
return fn;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Just an utility to create custom constraints, helps you set the display name.
|
|
393
|
+
*
|
|
394
|
+
* @param validator - A function that validates the input value. Should return true if valid, false or throw an error if invalid.
|
|
395
|
+
* @param display - Optional display name for the constraint, useful in help output.
|
|
396
|
+
* @param errorMessage - Optional function to generate a custom error message when validation fails.
|
|
397
|
+
* @returns A ConstraintFunction that applies the custom validation logic.
|
|
398
|
+
* @throws {Error} If the validator returns false or throws an error.
|
|
399
|
+
*/
|
|
400
|
+
function Custom(validator, display, errorMessage) {
|
|
401
|
+
function fn(value) {
|
|
402
|
+
if (validator(value) === false) throw new Error(errorMessage ? errorMessage(value) : `Invalid value: ${value}`);
|
|
403
|
+
}
|
|
404
|
+
fn.display = display;
|
|
405
|
+
return fn;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
//#endregion
|
|
409
|
+
//#region src/flag-types.ts
|
|
410
|
+
var flag_types_exports = /* @__PURE__ */ __export({ Enum: () => Enum });
|
|
411
|
+
|
|
331
412
|
//#endregion
|
|
332
413
|
//#region src/helpers.ts
|
|
333
414
|
const defineCommand = (command, handler) => ({
|
|
@@ -353,4 +434,4 @@ function createStopAtFirstParameter() {
|
|
|
353
434
|
const definePlugin = (plugin) => plugin;
|
|
354
435
|
|
|
355
436
|
//#endregion
|
|
356
|
-
export {
|
|
437
|
+
export { Clerc, constraint_exports as Constraints, DOUBLE_DASH, InvalidCommandError, InvalidParametersError, InvalidSchemaError, KNOWN_FLAG, MissingRequiredMetadataError, NoCommandSpecifiedError, NoSuchCommandError, PARAMETER, flag_types_exports as Types, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, resolveCommand };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.16",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc core",
|
|
@@ -45,10 +45,10 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"lite-emit": "^3.1.0",
|
|
48
|
-
"@clerc/parser": "^1.0.0-beta.
|
|
48
|
+
"@clerc/parser": "^1.0.0-beta.16"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"is-platform": "^1.0.0",
|
|
52
|
-
"@clerc/utils": "1.0.0-beta.
|
|
52
|
+
"@clerc/utils": "1.0.0-beta.16"
|
|
53
53
|
}
|
|
54
54
|
}
|