@clerc/parser 1.0.0-beta.14 → 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/index.d.ts CHANGED
@@ -2,10 +2,13 @@
2
2
  declare class InvalidSchemaError extends Error {
3
3
  constructor(message: string);
4
4
  }
5
+ declare class MissingRequiredFlagError extends Error {
6
+ constructor(name: string);
7
+ }
5
8
  //#endregion
6
9
  //#region src/flag-types.d.ts
7
10
  /**
8
- * Creates a Choices type function that validates the input against allowed values.
11
+ * Creates a Enum type function that validates the input against allowed values.
9
12
  * The display name will be formatted as "value1 | value2 | ..." for help output.
10
13
  *
11
14
  * @param values - Array of allowed string values
@@ -14,11 +17,11 @@ declare class InvalidSchemaError extends Error {
14
17
  *
15
18
  * @example
16
19
  * ```typescript
17
- * const format = Choices(['json', 'yaml', 'xml']);
20
+ * const format = Enum(['json', 'yaml', 'xml']);
18
21
  * // Help output will show: json | yaml | xml
19
22
  * ```
20
23
  */
21
- declare function Choices<T extends string>(...values: T[]): FlagTypeFunction<T>;
24
+ declare function Enum<T extends string>(...values: T[]): FlagTypeFunction<T>;
22
25
  //#endregion
23
26
  //#region ../utils/src/types/type-fest.d.ts
24
27
  type Prettify<T> = { [K in keyof T]: T[K] } & {};
@@ -28,21 +31,24 @@ type IsAny<T> = 0 extends 1 & T ? true : false;
28
31
  type MaybeArray<T> = T | T[];
29
32
  //#endregion
30
33
  //#region src/types.d.ts
31
- type FlagDefaultValue<T = unknown> = T | ((() => T) & {
34
+ interface FlagDefaultValueFunction<T> {
35
+ (): T;
32
36
  display?: string;
33
- });
37
+ }
38
+ type FlagDefaultValue<T = unknown> = T | FlagDefaultValueFunction<T>;
34
39
  /**
35
40
  * Defines how a string input is converted to the target type T.
36
41
  *
37
42
  * @template T The target type.
38
43
  */
39
- type FlagTypeFunction<T = unknown> = ((value: string) => T) & {
44
+ interface FlagTypeFunction<T = unknown> {
45
+ (value: string): T;
40
46
  /**
41
47
  * Optional display name for the type, useful in help output.
42
48
  * If provided, this will be shown instead of the function name.
43
49
  */
44
50
  display?: string;
45
- };
51
+ }
46
52
  /**
47
53
  * A callback function to conditionally stop parsing.
48
54
  * When it returns true, parsing stops and remaining arguments are preserved in `ignored`.
@@ -64,6 +70,8 @@ interface BaseFlagOptions<T extends FlagType = FlagType> {
64
70
  alias?: MaybeArray<string>;
65
71
  /** The default value of the flag. */
66
72
  default?: unknown;
73
+ /** Whether the flag is required. */
74
+ required?: boolean;
67
75
  }
68
76
  type FlagOptions = (BaseFlagOptions<BooleanConstructor> & {
69
77
  /**
@@ -134,13 +142,15 @@ type IsTypeAny<T extends FlagDefinitionValue> = IsAny<T> extends true ? true : T
134
142
  } ? IsAny<Type> extends true ? true : false : false;
135
143
  type _InferFlags<T extends FlagsDefinition> = { [K in keyof T]: IsTypeAny<T[K]> extends true ? any : T[K] extends readonly [BooleanConstructor] | {
136
144
  type: readonly [BooleanConstructor];
137
- } ? number : T[K] extends ObjectConstructor | {
145
+ } ? number | InferFlagDefault<T[K], never> : T[K] extends ObjectConstructor | {
138
146
  type: ObjectConstructor;
139
- } ? ObjectInputType : T[K] extends readonly [FlagType<infer U>] | {
147
+ } ? ObjectInputType | InferFlagDefault<T[K], never> : T[K] extends readonly [FlagType<infer U>] | {
140
148
  type: readonly [FlagType<infer U>];
141
149
  } ? U[] | InferFlagDefault<T[K], never> : T[K] extends FlagType<infer U> | {
142
150
  type: FlagType<infer U>;
143
- } ? U | InferFlagDefault<T[K], [U] extends [boolean] ? never : undefined> : never };
151
+ } ? U | InferFlagDefault<T[K], [U] extends [boolean] ? never : T[K] extends {
152
+ required: true;
153
+ } ? never : undefined> : never };
144
154
  /**
145
155
  * An advanced utility type that infers the exact type of the `flags` object in the parsed result,
146
156
  * based on the provided `flags` configuration object T.
@@ -162,4 +172,4 @@ declare function createParser<T extends FlagsDefinition>(options?: ParserOptions
162
172
  };
163
173
  declare const parse: <T extends FlagsDefinition>(args: string[], options?: ParserOptions<T>) => ParsedResult<InferFlags<T>>;
164
174
  //#endregion
165
- export { BaseFlagOptions, Choices, DOUBLE_DASH, FlagDefaultValue, FlagDefinitionValue, FlagOptions, FlagType, FlagTypeFunction, FlagsDefinition, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG, ObjectInputType, PARAMETER, ParsedResult, ParserOptions, RawInputType, UNKNOWN_FLAG, createParser, parse };
175
+ 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 };
package/dist/index.js CHANGED
@@ -5,11 +5,17 @@ var InvalidSchemaError = class extends Error {
5
5
  this.name = "InvalidSchemaError";
6
6
  }
7
7
  };
8
+ var MissingRequiredFlagError = class extends Error {
9
+ constructor(name) {
10
+ super(`Missing required flag: ${name}`);
11
+ this.name = "MissingRequiredFlagError";
12
+ }
13
+ };
8
14
 
9
15
  //#endregion
10
16
  //#region src/flag-types.ts
11
17
  /**
12
- * Creates a Choices type function that validates the input against allowed values.
18
+ * Creates a Enum type function that validates the input against allowed values.
13
19
  * The display name will be formatted as "value1 | value2 | ..." for help output.
14
20
  *
15
21
  * @param values - Array of allowed string values
@@ -18,11 +24,11 @@ var InvalidSchemaError = class extends Error {
18
24
  *
19
25
  * @example
20
26
  * ```typescript
21
- * const format = Choices(['json', 'yaml', 'xml']);
27
+ * const format = Enum(['json', 'yaml', 'xml']);
22
28
  * // Help output will show: json | yaml | xml
23
29
  * ```
24
30
  */
25
- function Choices(...values) {
31
+ function Enum(...values) {
26
32
  const fn = ((value) => {
27
33
  if (!values.includes(value)) throw new Error(`Invalid value: ${value}. Must be one of: ${values.join(", ")}`);
28
34
  return value;
@@ -333,6 +339,7 @@ function createParser(options = {}) {
333
339
  else if (Array.isArray(config.type)) result.flags[key] = isArrayOfType(config.type, Boolean) ? 0 : [];
334
340
  else if (config.type === Object) result.flags[key] = {};
335
341
  else if (config.type === Boolean) result.flags[key] = false;
342
+ else if (config.required) throw new MissingRequiredFlagError(key);
336
343
  }
337
344
  return result;
338
345
  };
@@ -341,4 +348,4 @@ function createParser(options = {}) {
341
348
  const parse = (args, options = {}) => createParser(options).parse(args);
342
349
 
343
350
  //#endregion
344
- export { Choices, DOUBLE_DASH, InvalidSchemaError, KNOWN_FLAG, PARAMETER, UNKNOWN_FLAG, createParser, parse };
351
+ export { DOUBLE_DASH, Enum, InvalidSchemaError, KNOWN_FLAG, MissingRequiredFlagError, PARAMETER, UNKNOWN_FLAG, createParser, parse };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/parser",
3
- "version": "1.0.0-beta.14",
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 parser",
@@ -56,6 +56,6 @@
56
56
  "nopt": "^9.0.0",
57
57
  "type-flag": "^4.0.3",
58
58
  "yargs-parser": "^22.0.0",
59
- "@clerc/utils": "1.0.0-beta.14"
59
+ "@clerc/utils": "1.0.0-beta.16"
60
60
  }
61
61
  }