@optique/core 0.1.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +475 -0
- package/dist/doc.cjs +104 -0
- package/dist/doc.d.cts +129 -0
- package/dist/doc.d.ts +129 -0
- package/dist/doc.js +104 -0
- package/dist/facade.cjs +98 -0
- package/dist/facade.d.cts +113 -0
- package/dist/facade.d.ts +113 -0
- package/dist/facade.js +97 -0
- package/dist/index.cjs +42 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/message.cjs +202 -0
- package/dist/message.d.cts +199 -0
- package/dist/message.d.ts +199 -0
- package/dist/message.js +194 -0
- package/dist/parser.cjs +1065 -0
- package/dist/parser.d.cts +580 -0
- package/dist/parser.d.ts +580 -0
- package/dist/parser.js +1053 -0
- package/dist/usage.cjs +242 -0
- package/dist/usage.d.cts +217 -0
- package/dist/usage.d.ts +217 -0
- package/dist/usage.js +239 -0
- package/dist/valueparser.cjs +332 -0
- package/dist/valueparser.d.cts +332 -0
- package/dist/valueparser.d.ts +332 -0
- package/dist/valueparser.js +325 -0
- package/package.json +117 -0
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { Message } from "./message.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/valueparser.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Interface for parsing CLI option values and arguments.
|
|
7
|
+
*
|
|
8
|
+
* A `ValueParser` is responsible for converting string input (typically from
|
|
9
|
+
* CLI arguments or option values) into strongly-typed values of type {@link T}.
|
|
10
|
+
*
|
|
11
|
+
* @template T The type of value this parser produces.
|
|
12
|
+
*/
|
|
13
|
+
interface ValueParser<T> {
|
|
14
|
+
/**
|
|
15
|
+
* The metavariable name for this parser. Used in help messages
|
|
16
|
+
* to indicate what kind of value this parser expects. Usually
|
|
17
|
+
* a single word in uppercase, like `PORT` or `FILE`.
|
|
18
|
+
*/
|
|
19
|
+
readonly metavar: string;
|
|
20
|
+
/**
|
|
21
|
+
* Parses a string input into a value of type {@link T}.
|
|
22
|
+
*
|
|
23
|
+
* @param input The string input to parse
|
|
24
|
+
* (e.g., the `value` part of `--option=value`).
|
|
25
|
+
* @returns A result object indicating success or failure with an error
|
|
26
|
+
* message.
|
|
27
|
+
*/
|
|
28
|
+
parse(input: string): ValueParserResult<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Formats a value of type {@link T} into a string representation.
|
|
31
|
+
* This is useful for displaying the value in help messages or
|
|
32
|
+
* documentation.
|
|
33
|
+
*
|
|
34
|
+
* @param value The value to format.
|
|
35
|
+
* @returns A string representation of the value.
|
|
36
|
+
*/
|
|
37
|
+
format(value: T): string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Result type returned by {@link ValueParser#parse}.
|
|
41
|
+
*
|
|
42
|
+
* This is a discriminated union that represents either a successful parse
|
|
43
|
+
* with the resulting value, or a failed parse with an error message.
|
|
44
|
+
*
|
|
45
|
+
* @template T The type of the successfully parsed value.
|
|
46
|
+
*/
|
|
47
|
+
type ValueParserResult<T> = {
|
|
48
|
+
/** Indicates that the parsing operation was successful. */
|
|
49
|
+
readonly success: true;
|
|
50
|
+
/** The successfully parsed value of type {@link T}. */
|
|
51
|
+
readonly value: T;
|
|
52
|
+
} | {
|
|
53
|
+
/** Indicates that the parsing operation failed. */
|
|
54
|
+
readonly success: false;
|
|
55
|
+
/** The error message describing why the parsing failed. */
|
|
56
|
+
readonly error: Message;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Options for creating a string parser.
|
|
60
|
+
*/
|
|
61
|
+
interface StringOptions {
|
|
62
|
+
/**
|
|
63
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
64
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
65
|
+
* word in uppercase, like `HOST` or `NAME`.
|
|
66
|
+
* @default `"STRING"`
|
|
67
|
+
*/
|
|
68
|
+
readonly metavar?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Optional regular expression pattern that the string must match.
|
|
71
|
+
*/
|
|
72
|
+
readonly pattern?: RegExp;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Options for creating a {@link choice} parser.
|
|
76
|
+
*/
|
|
77
|
+
interface ChoiceOptions {
|
|
78
|
+
/**
|
|
79
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
80
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
81
|
+
* word in uppercase, like `TYPE` or `MODE`.
|
|
82
|
+
* @default `"TYPE"`
|
|
83
|
+
*/
|
|
84
|
+
readonly metavar?: string;
|
|
85
|
+
/**
|
|
86
|
+
* If `true`, the parser will perform case-insensitive matching
|
|
87
|
+
* against the enumerated values. This means that input like "value",
|
|
88
|
+
* "Value", or "VALUE" will all match the same enumerated value.
|
|
89
|
+
* If `false`, the matching will be case-sensitive.
|
|
90
|
+
* @default `false`
|
|
91
|
+
*/
|
|
92
|
+
readonly caseInsensitive?: boolean;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* A predicate function that checks if an object is a {@link ValueParser}.
|
|
96
|
+
* @param object The object to check.
|
|
97
|
+
* @return `true` if the object is a {@link ValueParser}, `false` otherwise.
|
|
98
|
+
*/
|
|
99
|
+
declare function isValueParser<T>(object: unknown): object is ValueParser<T>;
|
|
100
|
+
/**
|
|
101
|
+
* Creates a {@link ValueParser} that accepts one of multiple
|
|
102
|
+
* string values, so-called enumerated values.
|
|
103
|
+
*
|
|
104
|
+
* This parser validates that the input string matches one of
|
|
105
|
+
* the specified values. If the input does not match any of the values,
|
|
106
|
+
* it returns an error message indicating the valid options.
|
|
107
|
+
* @param values An array of valid string values that this parser can accept.
|
|
108
|
+
* @param options Configuration options for the choice parser.
|
|
109
|
+
* @returns A {@link ValueParser} that checks if the input matches one of the
|
|
110
|
+
* specified values.
|
|
111
|
+
*/
|
|
112
|
+
declare function choice<const T extends string>(values: readonly T[], options?: ChoiceOptions): ValueParser<T>;
|
|
113
|
+
/**
|
|
114
|
+
* Creates a {@link ValueParser} for strings.
|
|
115
|
+
*
|
|
116
|
+
* This parser validates that the input is a string and optionally checks
|
|
117
|
+
* if it matches a specified regular expression pattern.
|
|
118
|
+
* @param options Configuration options for the string parser.
|
|
119
|
+
* @returns A {@link ValueParser} that parses strings according to the
|
|
120
|
+
* specified options.
|
|
121
|
+
*/
|
|
122
|
+
declare function string(options?: StringOptions): ValueParser<string>;
|
|
123
|
+
/**
|
|
124
|
+
* Options for creating an integer parser that returns a JavaScript `number`.
|
|
125
|
+
*
|
|
126
|
+
* This interface is used when you want to parse integers as regular JavaScript
|
|
127
|
+
* numbers (which are safe up to `Number.MAX_SAFE_INTEGER`).
|
|
128
|
+
*/
|
|
129
|
+
interface IntegerOptionsNumber {
|
|
130
|
+
/**
|
|
131
|
+
* The type of integer to parse.
|
|
132
|
+
* @default `"number"`
|
|
133
|
+
*/
|
|
134
|
+
readonly type?: "number";
|
|
135
|
+
/**
|
|
136
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
137
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
138
|
+
* word in uppercase, like `PORT`.
|
|
139
|
+
* @default `"INTEGER"`
|
|
140
|
+
*/
|
|
141
|
+
readonly metavar?: string;
|
|
142
|
+
/**
|
|
143
|
+
* Minimum allowed value (inclusive). If not specified,
|
|
144
|
+
* no minimum is enforced.
|
|
145
|
+
*/
|
|
146
|
+
readonly min?: number;
|
|
147
|
+
/**
|
|
148
|
+
* Maximum allowed value (inclusive). If not specified,
|
|
149
|
+
* no maximum is enforced.
|
|
150
|
+
*/
|
|
151
|
+
readonly max?: number;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Options for creating an integer parser that returns a `bigint`.
|
|
155
|
+
*
|
|
156
|
+
* This interface is used when you need to parse very large integers that
|
|
157
|
+
* exceed JavaScript's safe integer range.
|
|
158
|
+
*/
|
|
159
|
+
interface IntegerOptionsBigInt {
|
|
160
|
+
/** Must be set to `"bigint"` to create a `bigint` parser. */
|
|
161
|
+
readonly type: "bigint";
|
|
162
|
+
/**
|
|
163
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
164
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
165
|
+
* word in uppercase, like `PORT`.
|
|
166
|
+
* @default `"INTEGER"`
|
|
167
|
+
*/
|
|
168
|
+
readonly metavar?: string;
|
|
169
|
+
/**
|
|
170
|
+
* Minimum allowed value (inclusive). If not specified,
|
|
171
|
+
* no minimum is enforced.
|
|
172
|
+
*/
|
|
173
|
+
readonly min?: bigint;
|
|
174
|
+
/**
|
|
175
|
+
* Maximum allowed value (inclusive). If not specified,
|
|
176
|
+
* no maximum is enforced.
|
|
177
|
+
*/
|
|
178
|
+
readonly max?: bigint;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Creates a ValueParser for integers that returns JavaScript numbers.
|
|
182
|
+
*
|
|
183
|
+
* @param options Configuration options for the integer parser.
|
|
184
|
+
* @returns A {@link ValueParser} that parses strings into numbers.
|
|
185
|
+
*/
|
|
186
|
+
declare function integer(options?: IntegerOptionsNumber): ValueParser<number>;
|
|
187
|
+
/**
|
|
188
|
+
* Creates a ValueParser for integers that returns `bigint` values.
|
|
189
|
+
*
|
|
190
|
+
* @param options Configuration options for the `bigint` parser.
|
|
191
|
+
* @returns A {@link ValueParser} that parses strings into `bigint` values.
|
|
192
|
+
*/
|
|
193
|
+
declare function integer(options: IntegerOptionsBigInt): ValueParser<bigint>;
|
|
194
|
+
/**
|
|
195
|
+
* Options for creating a {@link float} parser.
|
|
196
|
+
*/
|
|
197
|
+
interface FloatOptions {
|
|
198
|
+
/**
|
|
199
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
200
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
201
|
+
* word in uppercase, like `RATE` or `PRICE`.
|
|
202
|
+
* @default `"NUMBER"`
|
|
203
|
+
*/
|
|
204
|
+
readonly metavar?: string;
|
|
205
|
+
/**
|
|
206
|
+
* Minimum allowed value (inclusive). If not specified,
|
|
207
|
+
* no minimum is enforced.
|
|
208
|
+
*/
|
|
209
|
+
readonly min?: number;
|
|
210
|
+
/**
|
|
211
|
+
* Maximum allowed value (inclusive). If not specified,
|
|
212
|
+
* no maximum is enforced.
|
|
213
|
+
*/
|
|
214
|
+
readonly max?: number;
|
|
215
|
+
/**
|
|
216
|
+
* If `true`, allows the special value `NaN` (not a number).
|
|
217
|
+
* This is useful for cases where `NaN` is a valid input,
|
|
218
|
+
* such as in some scientific calculations.
|
|
219
|
+
* @default `false`
|
|
220
|
+
*/
|
|
221
|
+
readonly allowNaN?: boolean;
|
|
222
|
+
/**
|
|
223
|
+
* If `true`, allows the special values `Infinity` and `-Infinity`.
|
|
224
|
+
* This is useful for cases where infinite values are valid inputs,
|
|
225
|
+
* such as in mathematical calculations or limits.
|
|
226
|
+
* @default `false`
|
|
227
|
+
*/
|
|
228
|
+
readonly allowInfinity?: boolean;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Creates a {@link ValueParser} for floating-point numbers.
|
|
232
|
+
*
|
|
233
|
+
* This parser validates that the input is a valid floating-point number
|
|
234
|
+
* and optionally enforces minimum and maximum value constraints.
|
|
235
|
+
* @param options Configuration options for the float parser.
|
|
236
|
+
* @returns A {@link ValueParser} that parses strings into floating-point
|
|
237
|
+
* numbers.
|
|
238
|
+
*/
|
|
239
|
+
declare function float(options?: FloatOptions): ValueParser<number>;
|
|
240
|
+
/**
|
|
241
|
+
* Options for creating a {@link url} parser.
|
|
242
|
+
*/
|
|
243
|
+
interface UrlOptions {
|
|
244
|
+
/**
|
|
245
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
246
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
247
|
+
* word in uppercase, like `URL` or `ENDPOINT`.
|
|
248
|
+
* @default `"URL"`
|
|
249
|
+
*/
|
|
250
|
+
readonly metavar?: string;
|
|
251
|
+
/**
|
|
252
|
+
* List of allowed URL protocols (e.g., `["http:", "https:"]`).
|
|
253
|
+
* If specified, the parsed URL must use one of these protocols.
|
|
254
|
+
* Protocol names should include the trailing colon (e.g., `"https:"`).
|
|
255
|
+
* If not specified, any protocol is allowed.
|
|
256
|
+
*/
|
|
257
|
+
readonly allowedProtocols?: readonly string[];
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Creates a {@link ValueParser} for URL values.
|
|
261
|
+
*
|
|
262
|
+
* This parser validates that the input is a well-formed URL and optionally
|
|
263
|
+
* restricts the allowed protocols. The parsed result is a JavaScript `URL`
|
|
264
|
+
* object.
|
|
265
|
+
* @param options Configuration options for the URL parser.
|
|
266
|
+
* @returns A {@link ValueParser} that converts string input to `URL` objects.
|
|
267
|
+
*/
|
|
268
|
+
declare function url(options?: UrlOptions): ValueParser<URL>;
|
|
269
|
+
/**
|
|
270
|
+
* Options for creating a {@link locale} parser.
|
|
271
|
+
*/
|
|
272
|
+
interface LocaleOptions {
|
|
273
|
+
/**
|
|
274
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
275
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
276
|
+
* word in uppercase, like `LOCALE` or `LANG`.
|
|
277
|
+
* @default `"LOCALE"`
|
|
278
|
+
*/
|
|
279
|
+
readonly metavar?: string;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Creates a {@link ValueParser} for locale values.
|
|
283
|
+
*
|
|
284
|
+
* This parser validates that the input is a well-formed locale identifier
|
|
285
|
+
* according to the Unicode Locale Identifier standard (BCP 47).
|
|
286
|
+
* The parsed result is a JavaScript `Intl.Locale` object.
|
|
287
|
+
* @param options Configuration options for the locale parser.
|
|
288
|
+
* @returns A {@link ValueParser} that converts string input to `Intl.Locale`
|
|
289
|
+
* objects.
|
|
290
|
+
*/
|
|
291
|
+
declare function locale(options?: LocaleOptions): ValueParser<Intl.Locale>;
|
|
292
|
+
/**
|
|
293
|
+
* Type representing a UUID string.
|
|
294
|
+
*
|
|
295
|
+
* A UUID is a 36-character string in the format:
|
|
296
|
+
* `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`
|
|
297
|
+
* where each `x` is a hexadecimal digit.
|
|
298
|
+
*/
|
|
299
|
+
type Uuid = `${string}-${string}-${string}-${string}-${string}`;
|
|
300
|
+
/**
|
|
301
|
+
* Options for creating a {@link uuid} parser.
|
|
302
|
+
*/
|
|
303
|
+
interface UuidOptions {
|
|
304
|
+
/**
|
|
305
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
306
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
307
|
+
* word in uppercase, like `UUID` or `ID`.
|
|
308
|
+
* @default `"UUID"`
|
|
309
|
+
*/
|
|
310
|
+
readonly metavar?: string;
|
|
311
|
+
/**
|
|
312
|
+
* List of allowed UUID versions (e.g., `[4, 5]` for UUIDs version 4 and 5).
|
|
313
|
+
* If specified, the parser will validate that the UUID matches one of the
|
|
314
|
+
* allowed versions. If not specified, any valid UUID format is accepted.
|
|
315
|
+
*/
|
|
316
|
+
readonly allowedVersions?: readonly number[];
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Creates a {@link ValueParser} for UUID values.
|
|
320
|
+
*
|
|
321
|
+
* This parser validates that the input is a well-formed UUID string in the
|
|
322
|
+
* standard format: `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx` where each `x`
|
|
323
|
+
* is a hexadecimal digit. The parser can optionally restrict to specific
|
|
324
|
+
* UUID versions.
|
|
325
|
+
*
|
|
326
|
+
* @param options Configuration options for the UUID parser.
|
|
327
|
+
* @returns A {@link ValueParser} that converts string input to {@link Uuid}
|
|
328
|
+
* strings.
|
|
329
|
+
*/
|
|
330
|
+
declare function uuid(options?: UuidOptions): ValueParser<Uuid>;
|
|
331
|
+
//#endregion
|
|
332
|
+
export { ChoiceOptions, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid };
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { Message } from "./message.js";
|
|
2
|
+
|
|
3
|
+
//#region src/valueparser.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Interface for parsing CLI option values and arguments.
|
|
7
|
+
*
|
|
8
|
+
* A `ValueParser` is responsible for converting string input (typically from
|
|
9
|
+
* CLI arguments or option values) into strongly-typed values of type {@link T}.
|
|
10
|
+
*
|
|
11
|
+
* @template T The type of value this parser produces.
|
|
12
|
+
*/
|
|
13
|
+
interface ValueParser<T> {
|
|
14
|
+
/**
|
|
15
|
+
* The metavariable name for this parser. Used in help messages
|
|
16
|
+
* to indicate what kind of value this parser expects. Usually
|
|
17
|
+
* a single word in uppercase, like `PORT` or `FILE`.
|
|
18
|
+
*/
|
|
19
|
+
readonly metavar: string;
|
|
20
|
+
/**
|
|
21
|
+
* Parses a string input into a value of type {@link T}.
|
|
22
|
+
*
|
|
23
|
+
* @param input The string input to parse
|
|
24
|
+
* (e.g., the `value` part of `--option=value`).
|
|
25
|
+
* @returns A result object indicating success or failure with an error
|
|
26
|
+
* message.
|
|
27
|
+
*/
|
|
28
|
+
parse(input: string): ValueParserResult<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Formats a value of type {@link T} into a string representation.
|
|
31
|
+
* This is useful for displaying the value in help messages or
|
|
32
|
+
* documentation.
|
|
33
|
+
*
|
|
34
|
+
* @param value The value to format.
|
|
35
|
+
* @returns A string representation of the value.
|
|
36
|
+
*/
|
|
37
|
+
format(value: T): string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Result type returned by {@link ValueParser#parse}.
|
|
41
|
+
*
|
|
42
|
+
* This is a discriminated union that represents either a successful parse
|
|
43
|
+
* with the resulting value, or a failed parse with an error message.
|
|
44
|
+
*
|
|
45
|
+
* @template T The type of the successfully parsed value.
|
|
46
|
+
*/
|
|
47
|
+
type ValueParserResult<T> = {
|
|
48
|
+
/** Indicates that the parsing operation was successful. */
|
|
49
|
+
readonly success: true;
|
|
50
|
+
/** The successfully parsed value of type {@link T}. */
|
|
51
|
+
readonly value: T;
|
|
52
|
+
} | {
|
|
53
|
+
/** Indicates that the parsing operation failed. */
|
|
54
|
+
readonly success: false;
|
|
55
|
+
/** The error message describing why the parsing failed. */
|
|
56
|
+
readonly error: Message;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Options for creating a string parser.
|
|
60
|
+
*/
|
|
61
|
+
interface StringOptions {
|
|
62
|
+
/**
|
|
63
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
64
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
65
|
+
* word in uppercase, like `HOST` or `NAME`.
|
|
66
|
+
* @default `"STRING"`
|
|
67
|
+
*/
|
|
68
|
+
readonly metavar?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Optional regular expression pattern that the string must match.
|
|
71
|
+
*/
|
|
72
|
+
readonly pattern?: RegExp;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Options for creating a {@link choice} parser.
|
|
76
|
+
*/
|
|
77
|
+
interface ChoiceOptions {
|
|
78
|
+
/**
|
|
79
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
80
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
81
|
+
* word in uppercase, like `TYPE` or `MODE`.
|
|
82
|
+
* @default `"TYPE"`
|
|
83
|
+
*/
|
|
84
|
+
readonly metavar?: string;
|
|
85
|
+
/**
|
|
86
|
+
* If `true`, the parser will perform case-insensitive matching
|
|
87
|
+
* against the enumerated values. This means that input like "value",
|
|
88
|
+
* "Value", or "VALUE" will all match the same enumerated value.
|
|
89
|
+
* If `false`, the matching will be case-sensitive.
|
|
90
|
+
* @default `false`
|
|
91
|
+
*/
|
|
92
|
+
readonly caseInsensitive?: boolean;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* A predicate function that checks if an object is a {@link ValueParser}.
|
|
96
|
+
* @param object The object to check.
|
|
97
|
+
* @return `true` if the object is a {@link ValueParser}, `false` otherwise.
|
|
98
|
+
*/
|
|
99
|
+
declare function isValueParser<T>(object: unknown): object is ValueParser<T>;
|
|
100
|
+
/**
|
|
101
|
+
* Creates a {@link ValueParser} that accepts one of multiple
|
|
102
|
+
* string values, so-called enumerated values.
|
|
103
|
+
*
|
|
104
|
+
* This parser validates that the input string matches one of
|
|
105
|
+
* the specified values. If the input does not match any of the values,
|
|
106
|
+
* it returns an error message indicating the valid options.
|
|
107
|
+
* @param values An array of valid string values that this parser can accept.
|
|
108
|
+
* @param options Configuration options for the choice parser.
|
|
109
|
+
* @returns A {@link ValueParser} that checks if the input matches one of the
|
|
110
|
+
* specified values.
|
|
111
|
+
*/
|
|
112
|
+
declare function choice<const T extends string>(values: readonly T[], options?: ChoiceOptions): ValueParser<T>;
|
|
113
|
+
/**
|
|
114
|
+
* Creates a {@link ValueParser} for strings.
|
|
115
|
+
*
|
|
116
|
+
* This parser validates that the input is a string and optionally checks
|
|
117
|
+
* if it matches a specified regular expression pattern.
|
|
118
|
+
* @param options Configuration options for the string parser.
|
|
119
|
+
* @returns A {@link ValueParser} that parses strings according to the
|
|
120
|
+
* specified options.
|
|
121
|
+
*/
|
|
122
|
+
declare function string(options?: StringOptions): ValueParser<string>;
|
|
123
|
+
/**
|
|
124
|
+
* Options for creating an integer parser that returns a JavaScript `number`.
|
|
125
|
+
*
|
|
126
|
+
* This interface is used when you want to parse integers as regular JavaScript
|
|
127
|
+
* numbers (which are safe up to `Number.MAX_SAFE_INTEGER`).
|
|
128
|
+
*/
|
|
129
|
+
interface IntegerOptionsNumber {
|
|
130
|
+
/**
|
|
131
|
+
* The type of integer to parse.
|
|
132
|
+
* @default `"number"`
|
|
133
|
+
*/
|
|
134
|
+
readonly type?: "number";
|
|
135
|
+
/**
|
|
136
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
137
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
138
|
+
* word in uppercase, like `PORT`.
|
|
139
|
+
* @default `"INTEGER"`
|
|
140
|
+
*/
|
|
141
|
+
readonly metavar?: string;
|
|
142
|
+
/**
|
|
143
|
+
* Minimum allowed value (inclusive). If not specified,
|
|
144
|
+
* no minimum is enforced.
|
|
145
|
+
*/
|
|
146
|
+
readonly min?: number;
|
|
147
|
+
/**
|
|
148
|
+
* Maximum allowed value (inclusive). If not specified,
|
|
149
|
+
* no maximum is enforced.
|
|
150
|
+
*/
|
|
151
|
+
readonly max?: number;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Options for creating an integer parser that returns a `bigint`.
|
|
155
|
+
*
|
|
156
|
+
* This interface is used when you need to parse very large integers that
|
|
157
|
+
* exceed JavaScript's safe integer range.
|
|
158
|
+
*/
|
|
159
|
+
interface IntegerOptionsBigInt {
|
|
160
|
+
/** Must be set to `"bigint"` to create a `bigint` parser. */
|
|
161
|
+
readonly type: "bigint";
|
|
162
|
+
/**
|
|
163
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
164
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
165
|
+
* word in uppercase, like `PORT`.
|
|
166
|
+
* @default `"INTEGER"`
|
|
167
|
+
*/
|
|
168
|
+
readonly metavar?: string;
|
|
169
|
+
/**
|
|
170
|
+
* Minimum allowed value (inclusive). If not specified,
|
|
171
|
+
* no minimum is enforced.
|
|
172
|
+
*/
|
|
173
|
+
readonly min?: bigint;
|
|
174
|
+
/**
|
|
175
|
+
* Maximum allowed value (inclusive). If not specified,
|
|
176
|
+
* no maximum is enforced.
|
|
177
|
+
*/
|
|
178
|
+
readonly max?: bigint;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Creates a ValueParser for integers that returns JavaScript numbers.
|
|
182
|
+
*
|
|
183
|
+
* @param options Configuration options for the integer parser.
|
|
184
|
+
* @returns A {@link ValueParser} that parses strings into numbers.
|
|
185
|
+
*/
|
|
186
|
+
declare function integer(options?: IntegerOptionsNumber): ValueParser<number>;
|
|
187
|
+
/**
|
|
188
|
+
* Creates a ValueParser for integers that returns `bigint` values.
|
|
189
|
+
*
|
|
190
|
+
* @param options Configuration options for the `bigint` parser.
|
|
191
|
+
* @returns A {@link ValueParser} that parses strings into `bigint` values.
|
|
192
|
+
*/
|
|
193
|
+
declare function integer(options: IntegerOptionsBigInt): ValueParser<bigint>;
|
|
194
|
+
/**
|
|
195
|
+
* Options for creating a {@link float} parser.
|
|
196
|
+
*/
|
|
197
|
+
interface FloatOptions {
|
|
198
|
+
/**
|
|
199
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
200
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
201
|
+
* word in uppercase, like `RATE` or `PRICE`.
|
|
202
|
+
* @default `"NUMBER"`
|
|
203
|
+
*/
|
|
204
|
+
readonly metavar?: string;
|
|
205
|
+
/**
|
|
206
|
+
* Minimum allowed value (inclusive). If not specified,
|
|
207
|
+
* no minimum is enforced.
|
|
208
|
+
*/
|
|
209
|
+
readonly min?: number;
|
|
210
|
+
/**
|
|
211
|
+
* Maximum allowed value (inclusive). If not specified,
|
|
212
|
+
* no maximum is enforced.
|
|
213
|
+
*/
|
|
214
|
+
readonly max?: number;
|
|
215
|
+
/**
|
|
216
|
+
* If `true`, allows the special value `NaN` (not a number).
|
|
217
|
+
* This is useful for cases where `NaN` is a valid input,
|
|
218
|
+
* such as in some scientific calculations.
|
|
219
|
+
* @default `false`
|
|
220
|
+
*/
|
|
221
|
+
readonly allowNaN?: boolean;
|
|
222
|
+
/**
|
|
223
|
+
* If `true`, allows the special values `Infinity` and `-Infinity`.
|
|
224
|
+
* This is useful for cases where infinite values are valid inputs,
|
|
225
|
+
* such as in mathematical calculations or limits.
|
|
226
|
+
* @default `false`
|
|
227
|
+
*/
|
|
228
|
+
readonly allowInfinity?: boolean;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Creates a {@link ValueParser} for floating-point numbers.
|
|
232
|
+
*
|
|
233
|
+
* This parser validates that the input is a valid floating-point number
|
|
234
|
+
* and optionally enforces minimum and maximum value constraints.
|
|
235
|
+
* @param options Configuration options for the float parser.
|
|
236
|
+
* @returns A {@link ValueParser} that parses strings into floating-point
|
|
237
|
+
* numbers.
|
|
238
|
+
*/
|
|
239
|
+
declare function float(options?: FloatOptions): ValueParser<number>;
|
|
240
|
+
/**
|
|
241
|
+
* Options for creating a {@link url} parser.
|
|
242
|
+
*/
|
|
243
|
+
interface UrlOptions {
|
|
244
|
+
/**
|
|
245
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
246
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
247
|
+
* word in uppercase, like `URL` or `ENDPOINT`.
|
|
248
|
+
* @default `"URL"`
|
|
249
|
+
*/
|
|
250
|
+
readonly metavar?: string;
|
|
251
|
+
/**
|
|
252
|
+
* List of allowed URL protocols (e.g., `["http:", "https:"]`).
|
|
253
|
+
* If specified, the parsed URL must use one of these protocols.
|
|
254
|
+
* Protocol names should include the trailing colon (e.g., `"https:"`).
|
|
255
|
+
* If not specified, any protocol is allowed.
|
|
256
|
+
*/
|
|
257
|
+
readonly allowedProtocols?: readonly string[];
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Creates a {@link ValueParser} for URL values.
|
|
261
|
+
*
|
|
262
|
+
* This parser validates that the input is a well-formed URL and optionally
|
|
263
|
+
* restricts the allowed protocols. The parsed result is a JavaScript `URL`
|
|
264
|
+
* object.
|
|
265
|
+
* @param options Configuration options for the URL parser.
|
|
266
|
+
* @returns A {@link ValueParser} that converts string input to `URL` objects.
|
|
267
|
+
*/
|
|
268
|
+
declare function url(options?: UrlOptions): ValueParser<URL>;
|
|
269
|
+
/**
|
|
270
|
+
* Options for creating a {@link locale} parser.
|
|
271
|
+
*/
|
|
272
|
+
interface LocaleOptions {
|
|
273
|
+
/**
|
|
274
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
275
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
276
|
+
* word in uppercase, like `LOCALE` or `LANG`.
|
|
277
|
+
* @default `"LOCALE"`
|
|
278
|
+
*/
|
|
279
|
+
readonly metavar?: string;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Creates a {@link ValueParser} for locale values.
|
|
283
|
+
*
|
|
284
|
+
* This parser validates that the input is a well-formed locale identifier
|
|
285
|
+
* according to the Unicode Locale Identifier standard (BCP 47).
|
|
286
|
+
* The parsed result is a JavaScript `Intl.Locale` object.
|
|
287
|
+
* @param options Configuration options for the locale parser.
|
|
288
|
+
* @returns A {@link ValueParser} that converts string input to `Intl.Locale`
|
|
289
|
+
* objects.
|
|
290
|
+
*/
|
|
291
|
+
declare function locale(options?: LocaleOptions): ValueParser<Intl.Locale>;
|
|
292
|
+
/**
|
|
293
|
+
* Type representing a UUID string.
|
|
294
|
+
*
|
|
295
|
+
* A UUID is a 36-character string in the format:
|
|
296
|
+
* `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`
|
|
297
|
+
* where each `x` is a hexadecimal digit.
|
|
298
|
+
*/
|
|
299
|
+
type Uuid = `${string}-${string}-${string}-${string}-${string}`;
|
|
300
|
+
/**
|
|
301
|
+
* Options for creating a {@link uuid} parser.
|
|
302
|
+
*/
|
|
303
|
+
interface UuidOptions {
|
|
304
|
+
/**
|
|
305
|
+
* The metavariable name for this parser. This is used in help messages to
|
|
306
|
+
* indicate what kind of value this parser expects. Usually a single
|
|
307
|
+
* word in uppercase, like `UUID` or `ID`.
|
|
308
|
+
* @default `"UUID"`
|
|
309
|
+
*/
|
|
310
|
+
readonly metavar?: string;
|
|
311
|
+
/**
|
|
312
|
+
* List of allowed UUID versions (e.g., `[4, 5]` for UUIDs version 4 and 5).
|
|
313
|
+
* If specified, the parser will validate that the UUID matches one of the
|
|
314
|
+
* allowed versions. If not specified, any valid UUID format is accepted.
|
|
315
|
+
*/
|
|
316
|
+
readonly allowedVersions?: readonly number[];
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Creates a {@link ValueParser} for UUID values.
|
|
320
|
+
*
|
|
321
|
+
* This parser validates that the input is a well-formed UUID string in the
|
|
322
|
+
* standard format: `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx` where each `x`
|
|
323
|
+
* is a hexadecimal digit. The parser can optionally restrict to specific
|
|
324
|
+
* UUID versions.
|
|
325
|
+
*
|
|
326
|
+
* @param options Configuration options for the UUID parser.
|
|
327
|
+
* @returns A {@link ValueParser} that converts string input to {@link Uuid}
|
|
328
|
+
* strings.
|
|
329
|
+
*/
|
|
330
|
+
declare function uuid(options?: UuidOptions): ValueParser<Uuid>;
|
|
331
|
+
//#endregion
|
|
332
|
+
export { ChoiceOptions, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid };
|