@gunshi/combinators 0.37.3 → 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.
- package/README.md +2 -9
- package/lib/index.d.ts +865 -110
- package/lib/index.js +69 -334
- package/package.json +3 -3
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
1
|
+
//#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/resolver.d.ts
|
|
2
2
|
//#region src/resolver.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* An argument schema definition for command-line argument parsing.
|
|
@@ -44,19 +44,25 @@ interface ArgSchema {
|
|
|
44
44
|
/**
|
|
45
45
|
* Type of the argument value.
|
|
46
46
|
*
|
|
47
|
-
* - `'string'`: Text value
|
|
48
|
-
* - `'boolean'`: `true`/`false` flag (can be negatable with `--no-` prefix)
|
|
47
|
+
* - `'string'`: Text value
|
|
48
|
+
* - `'boolean'`: `true`/`false` flag (can be negatable with `--no-` prefix). `--flag=true` and
|
|
49
|
+
* `--flag=false` set the value explicitly; any other value after `=` is a type error
|
|
49
50
|
* - `'number'`: Numeric value (parsed as integer or float)
|
|
50
|
-
* - `'enum'`: One of predefined string values (requires `choices` property
|
|
51
|
+
* - `'enum'`: One of predefined string values (requires `choices` property, unless the argument
|
|
52
|
+
* has a `parse` function)
|
|
51
53
|
* - `'positional'`: Non-option argument by position
|
|
52
54
|
* - `'custom'`: Custom parsing with user-defined `parse` function
|
|
53
55
|
*
|
|
56
|
+
* Any other `type`, or no `type`, is a mistake that only untyped code can make: if the argument
|
|
57
|
+
* has no `parse` function, {@link resolveArgs} and `parse()` throw an `Error`, whether or not the
|
|
58
|
+
* argument is given.
|
|
59
|
+
*
|
|
54
60
|
* @example
|
|
55
61
|
* Different argument types:
|
|
56
62
|
* ```ts
|
|
57
63
|
* {
|
|
58
64
|
* name: { type: 'string' }, // --name value
|
|
59
|
-
* verbose: { type: 'boolean' }, // --verbose
|
|
65
|
+
* verbose: { type: 'boolean' }, // --verbose (--no-verbose needs negatable: true)
|
|
60
66
|
* port: { type: 'number' }, // --port 3000
|
|
61
67
|
* level: { type: 'enum', choices: ['debug', 'info'] },
|
|
62
68
|
* file: { type: 'positional' }, // first positional arg
|
|
@@ -131,14 +137,32 @@ interface ArgSchema {
|
|
|
131
137
|
/**
|
|
132
138
|
* Marks the argument as required.
|
|
133
139
|
*
|
|
134
|
-
* When `true`, the argument must be provided by the user.
|
|
135
|
-
*
|
|
140
|
+
* When `true`, the argument must be provided by the user. If it is missing, the error is an
|
|
141
|
+
* `ArgResolveError` with type 'required' and the code `err:arg:required-option`
|
|
142
|
+
* ({@link ArgsValidationErrorKeys}.requiredOption), or `err:arg:required-positional`
|
|
143
|
+
* ({@link ArgsValidationErrorKeys}.requiredPositional) for a positional argument. Its
|
|
144
|
+
* `values` has the `displayName` as in the message, such as `'--name'` for an option
|
|
145
|
+
* (`'--name' or '-n'` with the short name `n`) and `'file'` for a positional argument, and
|
|
146
|
+
* the `name`, which is the schema key.
|
|
147
|
+
* An option that is given without a value is reported as `err:arg:missing-value`
|
|
148
|
+
* ({@link ArgsValidationErrorKeys}.missingValue) instead, because the option itself was given.
|
|
149
|
+
* An explicit empty value, such as `--name=` or `-n ''`, is still reported as required, but the
|
|
150
|
+
* option counts as given: it is `true` in `explicit` and takes part in conflicts, as any other
|
|
151
|
+
* given option does.
|
|
136
152
|
*
|
|
137
|
-
* For single-value positional arguments, omitting `required` keeps the argument
|
|
138
|
-
*
|
|
139
|
-
* optional
|
|
153
|
+
* For single-value positional arguments, omitting `required` keeps the argument required for
|
|
154
|
+
* compatibility, unless it has a `default`, which makes it optional. A `multiple` positional
|
|
155
|
+
* argument is optional unless `required: true` is set. Set `required: false` to make a positional
|
|
156
|
+
* argument explicitly optional. Optional positional arguments leave enough input values for later
|
|
140
157
|
* required positional arguments before consuming a value.
|
|
141
158
|
*
|
|
159
|
+
* In the type of the values, such as {@link ArgValues}, a single-value positional argument whose
|
|
160
|
+
* `required` may be `false`, such as one of type `boolean`, is optional, as it may be missing,
|
|
161
|
+
* unless it has a `default`. An optional `required` of type `boolean`, as declared here, says
|
|
162
|
+
* nothing, and keeps it required. TypeScript widens a `required: true` to `boolean` in a schema
|
|
163
|
+
* written apart from the call, such as `{ type: 'positional' as const, required: true }`, which
|
|
164
|
+
* types the argument as optional: write such a schema `as const`.
|
|
165
|
+
*
|
|
142
166
|
* @example
|
|
143
167
|
* Required arguments:
|
|
144
168
|
* ```ts
|
|
@@ -159,8 +183,9 @@ interface ArgSchema {
|
|
|
159
183
|
/**
|
|
160
184
|
* Allows the argument to accept multiple values.
|
|
161
185
|
*
|
|
162
|
-
* When `true`, the resolved value becomes an array.
|
|
163
|
-
*
|
|
186
|
+
* When `true`, the resolved value becomes an array. When the argument is not given and has no
|
|
187
|
+
* `default`, its value is `undefined`, not an empty array.
|
|
188
|
+
* For options: can be specified multiple times (--tags foo --tags bar)
|
|
164
189
|
* For positional: collects remaining positional arguments after preserving values for
|
|
165
190
|
* later required positional arguments.
|
|
166
191
|
*
|
|
@@ -192,6 +217,12 @@ interface ArgSchema {
|
|
|
192
217
|
*
|
|
193
218
|
* Only applicable to `type: 'boolean'` arguments.
|
|
194
219
|
*
|
|
220
|
+
* The negated name is always `no-` followed by the full option name. An option named
|
|
221
|
+
* `no-cache` is negated by `--no-no-cache`, and `--no-cache` sets it to `true`.
|
|
222
|
+
*
|
|
223
|
+
* The negated form does not take a value. `--no-flag=<value>` is reported as
|
|
224
|
+
* `err:arg:unexpected-value` ({@link ArgsValidationErrorKeys}.unexpectedValue).
|
|
225
|
+
*
|
|
195
226
|
* @example
|
|
196
227
|
* Negatable boolean:
|
|
197
228
|
* ```ts
|
|
@@ -210,8 +241,17 @@ interface ArgSchema {
|
|
|
210
241
|
/**
|
|
211
242
|
* Array of allowed string values for enum-type arguments.
|
|
212
243
|
*
|
|
213
|
-
* Required when `type: 'enum'
|
|
214
|
-
*
|
|
244
|
+
* Required when `type: 'enum'`, unless the argument has a `parse` function: if `choices` is
|
|
245
|
+
* missing or not an array, {@link resolveArgs} and `parse()` throw a `TypeError`, whether or not
|
|
246
|
+
* the argument is given. With a `parse` function and no `choices`, the value is passed to the
|
|
247
|
+
* function as for a `string` argument with a `parse` function.
|
|
248
|
+
*
|
|
249
|
+
* The argument value must be one of these choices, otherwise the error is an `ArgResolveError`
|
|
250
|
+
* with type 'type' and the code `err:arg:invalid-choice`
|
|
251
|
+
* ({@link ArgsValidationErrorKeys}.invalidChoice).
|
|
252
|
+
*
|
|
253
|
+
* The value is checked before `parse` is called, so a `parse` function receives only one of
|
|
254
|
+
* these choices and can change it, for example to upper case.
|
|
215
255
|
*
|
|
216
256
|
* Supports both mutable arrays and readonly arrays for type safety.
|
|
217
257
|
*
|
|
@@ -235,18 +275,37 @@ interface ArgSchema {
|
|
|
235
275
|
*/
|
|
236
276
|
choices?: string[] | readonly string[];
|
|
237
277
|
/**
|
|
238
|
-
* Default value used when the argument is not provided.
|
|
278
|
+
* Default value used when the argument is not provided. An option that is given without a value,
|
|
279
|
+
* or with a value that is rejected, also gets its default, along with the error.
|
|
239
280
|
*
|
|
240
281
|
* The type must match the argument's `type` property:
|
|
241
282
|
* - `string` type: string default
|
|
242
283
|
* - `boolean` type: boolean default
|
|
243
284
|
* - `number` type: number default
|
|
244
|
-
* - `enum` type:
|
|
285
|
+
* - `enum` type: one of the `choices` values, or a value that a `parse` of your own returns
|
|
245
286
|
* - `positional`/`custom` type: string, boolean, or number default
|
|
246
287
|
*
|
|
247
|
-
*
|
|
248
|
-
* value
|
|
249
|
-
*
|
|
288
|
+
* The default is used as is and never goes through `parse`, including when an option is given
|
|
289
|
+
* without a value. An explicit empty value, such as `--name=` or `-n ''`, is a value, not a
|
|
290
|
+
* missing one: a `string` option without `parse` gets `''` instead of the default, unless it is
|
|
291
|
+
* `required`. What `parse` returns is a value too, even `undefined` or `null`, and the default
|
|
292
|
+
* does not replace it.
|
|
293
|
+
*
|
|
294
|
+
* The value of a `multiple` argument is an array, so its default becomes the only element of the
|
|
295
|
+
* array: `default: 'latest'` gives `['latest']`.
|
|
296
|
+
*
|
|
297
|
+
* The default of an `enum` option with `choices` is checked when it would be used, that is, when
|
|
298
|
+
* no value from the command line is used: one that is not one of the choices is reported as an
|
|
299
|
+
* `ArgResolveError` with type 'type' and the code `err:arg:invalid-default`
|
|
300
|
+
* ({@link ArgsValidationErrorKeys}.invalidDefault), and is not used. With a `parse` function of
|
|
301
|
+
* your own, or a `map()` transform, the default is a value that the function returns, which need
|
|
302
|
+
* not be one of the choices, and it is not checked. `choice()` returns the value as is, so its
|
|
303
|
+
* default is checked.
|
|
304
|
+
*
|
|
305
|
+
* For positional arguments, `multiple` ones included, the default is used when no value is left
|
|
306
|
+
* for the argument: when the positional values run out, or when the remaining ones are preserved
|
|
307
|
+
* for later required positional arguments. With `required: true`, the default is not used, and
|
|
308
|
+
* the argument is reported as required instead.
|
|
250
309
|
*
|
|
251
310
|
* @example
|
|
252
311
|
* Default values by type:
|
|
@@ -279,7 +338,8 @@ interface ArgSchema {
|
|
|
279
338
|
* When `true`, a property like `maxCount` becomes available as `--max-count`.
|
|
280
339
|
* This allows [CAC](https://github.com/cacjs/cac) user-friendly property names while maintaining CLI conventions.
|
|
281
340
|
*
|
|
282
|
-
*
|
|
341
|
+
* The `toKebab` option of {@link resolveArgs} and `parse()` applies it to all arguments, as in
|
|
342
|
+
* `resolveArgs(args, tokens, { toKebab: true })`.
|
|
283
343
|
*
|
|
284
344
|
* Note: Only `true` is allowed (not `false`) to make intent explicit.
|
|
285
345
|
*
|
|
@@ -303,8 +363,19 @@ interface ArgSchema {
|
|
|
303
363
|
/**
|
|
304
364
|
* Names of other options that conflict with this option.
|
|
305
365
|
*
|
|
306
|
-
* When this option is used together with any of the conflicting options,
|
|
307
|
-
*
|
|
366
|
+
* When this option is used together with any of the conflicting options, the error is an
|
|
367
|
+
* `ArgResolveError` with type 'conflict' and the code `err:arg:conflict`
|
|
368
|
+
* ({@link ArgsValidationErrorKeys}.conflict). Its `values` has the `displayName` and `name` of
|
|
369
|
+
* the argument whose `conflicts` names the other one, and the `conflictDisplayName` and
|
|
370
|
+
* `conflictName` of the other one. `displayName` and `conflictDisplayName` show an option as
|
|
371
|
+
* it was written, such as `'-p'`, and `name` and `conflictName` are the schema keys. When both
|
|
372
|
+
* name each other, `name` is the one that comes first in the schema.
|
|
373
|
+
*
|
|
374
|
+
* A positional argument can be on either side, such as `[file]` and `--stdin`. It conflicts when
|
|
375
|
+
* it is given, and it is shown by its name, such as `'file'` (in kebab-case with `toKebab`), as
|
|
376
|
+
* in its other errors. The message starts with the kind of the argument given as `name`:
|
|
377
|
+
* `Positional argument 'file' conflicts with '--stdin'` or
|
|
378
|
+
* `Optional argument '--stdin' conflicts with 'file'`.
|
|
308
379
|
*
|
|
309
380
|
* Conflicts only need to be defined on one side - if option A defines a conflict
|
|
310
381
|
* with option B, the conflict is automatically detected when both are used,
|
|
@@ -342,7 +413,7 @@ interface ArgSchema {
|
|
|
342
413
|
* // No conflicts defined, but still cannot use with --summer
|
|
343
414
|
* }
|
|
344
415
|
* }
|
|
345
|
-
* // Usage: --summer --autumn
|
|
416
|
+
* // Usage: --summer --autumn reports an error
|
|
346
417
|
* // Error: "Optional argument '--summer' conflicts with '--autumn'"
|
|
347
418
|
* ```
|
|
348
419
|
*
|
|
@@ -394,6 +465,10 @@ interface ArgSchema {
|
|
|
394
465
|
* Particularly useful for `type: 'custom'` arguments where the type
|
|
395
466
|
* name would otherwise be unhelpful.
|
|
396
467
|
*
|
|
468
|
+
* For a `custom` option that is given without a value, it is also the `expected` in the `values`
|
|
469
|
+
* of the `err:arg:missing-value` error ({@link ArgsValidationErrorKeys}.missingValue), which is
|
|
470
|
+
* `'custom'` without it.
|
|
471
|
+
*
|
|
397
472
|
* @example
|
|
398
473
|
* Metavar usage:
|
|
399
474
|
* ```ts
|
|
@@ -410,13 +485,37 @@ interface ArgSchema {
|
|
|
410
485
|
*/
|
|
411
486
|
metavar?: string;
|
|
412
487
|
/**
|
|
413
|
-
* Custom parsing function
|
|
488
|
+
* Custom parsing function, used in place of the parsing of the argument's `type`.
|
|
414
489
|
*
|
|
415
|
-
* Required when `type: 'custom'
|
|
416
|
-
*
|
|
490
|
+
* Required when `type: 'custom'`: if it is missing or not a function, {@link resolveArgs} and
|
|
491
|
+
* `parse()` throw a `TypeError`, whether or not the argument is given. The function receives the
|
|
492
|
+
* raw string value and must return the parsed result. It should throw an Error (or subclass) if
|
|
493
|
+
* parsing fails.
|
|
417
494
|
*
|
|
418
495
|
* The function's return type becomes the resolved argument type.
|
|
419
496
|
*
|
|
497
|
+
* `parse` is called synchronously, and what it returns becomes the value, even `undefined` or
|
|
498
|
+
* `null`, so throw to reject a value. An `async` function returns a promise, and the promise
|
|
499
|
+
* becomes the value as is: it is not awaited, and its rejection is not reported as an error.
|
|
500
|
+
* Await the value, or each of its elements with `multiple` (for example with `Promise.all()`),
|
|
501
|
+
* and handle the rejection yourself: a rejection that nothing handles is an unhandled rejection,
|
|
502
|
+
* which ends a Node.js process by default.
|
|
503
|
+
*
|
|
504
|
+
* A `boolean` option calls `parse` with `'true'`, or `'false'` for the negated form. Other
|
|
505
|
+
* options call it only with a value from the command line: when the option is given without a
|
|
506
|
+
* value, `parse` is not called and the missing value is reported as `err:arg:missing-value`
|
|
507
|
+
* ({@link ArgsValidationErrorKeys}.missingValue). When that error may suggest the argument after
|
|
508
|
+
* the option as its value, such as `--port=-5` for `--port -5`, a `parse` function of your own
|
|
509
|
+
* is not called to check it either: only the `parse` functions of `string()`, `number()`,
|
|
510
|
+
* `integer()`, `float()` and `choice()`, which have no side effects, are called for that.
|
|
511
|
+
* An explicit empty value, such as `--name=` or `-n=`, is passed as `''` unless
|
|
512
|
+
* `required: true` is set.
|
|
513
|
+
*
|
|
514
|
+
* An `enum` option with `choices` calls it only with one of them. Any other value, an explicit
|
|
515
|
+
* empty one included, is reported as `err:arg:invalid-choice`
|
|
516
|
+
* ({@link ArgsValidationErrorKeys}.invalidChoice), except that a required option reports an
|
|
517
|
+
* explicit empty value as required.
|
|
518
|
+
*
|
|
420
519
|
* @param value - Raw string value from command line
|
|
421
520
|
* @returns Parsed value of any type
|
|
422
521
|
* @throws {Error} Error or subclass when value is invalid
|
|
@@ -460,14 +559,12 @@ interface Args {
|
|
|
460
559
|
[option: string]: ArgSchema;
|
|
461
560
|
}
|
|
462
561
|
//#endregion
|
|
463
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
562
|
+
//#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/combinators.d.ts
|
|
464
563
|
//#region src/combinators.d.ts
|
|
465
564
|
/**
|
|
466
565
|
* A combinator produced by combinator factory functions.
|
|
467
566
|
*
|
|
468
567
|
* @typeParam T - The parsed value type.
|
|
469
|
-
*
|
|
470
|
-
* @experimental
|
|
471
568
|
*/
|
|
472
569
|
type Combinator<T> = {
|
|
473
570
|
/**
|
|
@@ -480,17 +577,30 @@ type Combinator<T> = {
|
|
|
480
577
|
};
|
|
481
578
|
/**
|
|
482
579
|
* A schema produced by combinator factory functions.
|
|
483
|
-
* Any {@link ArgSchema}
|
|
580
|
+
* Any {@link ArgSchema} whose parse function returns `T` qualifies. The `parse` of
|
|
581
|
+
* {@link ArgSchema}, which returns `any`, is left out, so that a schema fits only where the values
|
|
582
|
+
* that it parses do: `integer()` is not a `CombinatorSchema<string>`.
|
|
484
583
|
*
|
|
485
584
|
* @typeParam T - The parsed value type.
|
|
585
|
+
*/
|
|
586
|
+
type CombinatorSchema<T> = Omit<ArgSchema, 'parse'> & Combinator<T>;
|
|
587
|
+
/**
|
|
588
|
+
* How {@link short}, {@link describe}, {@link withDefault}, {@link required}, {@link multiple},
|
|
589
|
+
* {@link hidden}, {@link unrequired} and {@link positional} read a schema typed as `any`: its value
|
|
590
|
+
* is typed `unknown`, and the `parse` of {@link ArgSchema}, which returns `any`, is kept, so that
|
|
591
|
+
* the result still fits any {@link CombinatorSchema}, as the schema typed as `any` does.
|
|
486
592
|
*
|
|
487
|
-
*
|
|
593
|
+
* For a schema typed by a type parameter, whether it is `any` is not known, so the result is built
|
|
594
|
+
* from the properties of the type parameter, which TypeScript reads through its constraint.
|
|
488
595
|
*/
|
|
489
|
-
type
|
|
596
|
+
type UntypedCombinatorSchema = ArgSchema & Combinator<unknown>;
|
|
597
|
+
/**
|
|
598
|
+
* The type that a combinator schema `S` parses to, for each schema of a union:
|
|
599
|
+
* `string | number` for `CombinatorSchema<string> | CombinatorSchema<number>`.
|
|
600
|
+
*/
|
|
601
|
+
type ParsedType<S> = S extends Combinator<infer T> ? T : never;
|
|
490
602
|
/**
|
|
491
603
|
* Common options shared by all base combinators.
|
|
492
|
-
*
|
|
493
|
-
* @experimental
|
|
494
604
|
*/
|
|
495
605
|
interface BaseOptions {
|
|
496
606
|
/**
|
|
@@ -507,13 +617,47 @@ interface BaseOptions {
|
|
|
507
617
|
short?: string;
|
|
508
618
|
/**
|
|
509
619
|
* Mark as required.
|
|
620
|
+
*
|
|
621
|
+
* A literal `true` or `false` is kept in the type of the schema: `true` types the value as
|
|
622
|
+
* present, as {@link required} does, and `false` makes a positional argument optional. So does a
|
|
623
|
+
* `required` that may be `false`: one of type `boolean` that the options always have, or a
|
|
624
|
+
* `false` that they have only in some cases, with no `required` in the others, such as
|
|
625
|
+
* `optional ? { required: false } : {}`. A `true` that the options have only in some cases, such
|
|
626
|
+
* as `strict ? { required: true } : {}`, is not kept.
|
|
627
|
+
*
|
|
628
|
+
* TypeScript widens a `required: true` to `boolean` in options written apart from the call, such
|
|
629
|
+
* as `const options = { required: true }`, which makes a positional argument optional, as a
|
|
630
|
+
* `required` typed as `any` does: write such options `as const`.
|
|
631
|
+
*
|
|
632
|
+
* Options whose type only says that `required` is a `boolean` that may be missing, as the type of
|
|
633
|
+
* this property does, keep a positional argument typed as present, as options without `required`
|
|
634
|
+
* do. So do `optional ? { required: flag } : {}`, and the options that a generic helper takes as
|
|
635
|
+
* a type parameter, such as `T extends IntegerOptions`, whose `required` TypeScript reads from
|
|
636
|
+
* the constraint. Use {@link unrequired} for such a positional argument when it may be missing.
|
|
510
637
|
*/
|
|
511
638
|
required?: boolean;
|
|
512
639
|
}
|
|
640
|
+
/**
|
|
641
|
+
* The schema `S` with the type `R` of `required` in the options: `true` types the value as present,
|
|
642
|
+
* and `false` or `boolean` makes a positional argument optional. `any` is kept as `boolean`, which
|
|
643
|
+
* it may be, so that an option with it stays optional.
|
|
644
|
+
*/
|
|
645
|
+
type WithRequiredOption<S, R extends boolean> = WithFlag<S, {
|
|
646
|
+
required: unknown extends R ? boolean : R;
|
|
647
|
+
}>;
|
|
648
|
+
/**
|
|
649
|
+
* The schema `S` with an optional `required: false`, when the type `R` of `required` in options
|
|
650
|
+
* that may not have it may be `false` but not `true`, as for `optional ? { required: false } : {}`,
|
|
651
|
+
* so that a positional argument with it is optional. The property is optional, instead of
|
|
652
|
+
* `false | undefined`, so that the schema fits {@link ArgSchema} with `exactOptionalPropertyTypes`.
|
|
653
|
+
* `S` stays as is otherwise, such as when `R` is `boolean` or `boolean | undefined`, as for options
|
|
654
|
+
* without `required`.
|
|
655
|
+
*/
|
|
656
|
+
type WithUnrequiredOption<S, R extends boolean | undefined> = false extends R ? true extends R ? S : WithFlag<S, {
|
|
657
|
+
required?: false;
|
|
658
|
+
}> : S;
|
|
513
659
|
/**
|
|
514
660
|
* Options for the {@link string} combinator.
|
|
515
|
-
*
|
|
516
|
-
* @experimental
|
|
517
661
|
*/
|
|
518
662
|
interface StringOptions extends BaseOptions {
|
|
519
663
|
/**
|
|
@@ -532,6 +676,28 @@ interface StringOptions extends BaseOptions {
|
|
|
532
676
|
/**
|
|
533
677
|
* Create a string argument schema with optional validation.
|
|
534
678
|
*
|
|
679
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps.
|
|
680
|
+
*
|
|
681
|
+
* @param opts - Validation options.
|
|
682
|
+
* @returns A combinator schema that resolves to string.
|
|
683
|
+
*
|
|
684
|
+
* @example
|
|
685
|
+
* ```ts
|
|
686
|
+
* const args = {
|
|
687
|
+
* name: string({ minLength: 1, maxLength: 50 })
|
|
688
|
+
* }
|
|
689
|
+
* ```
|
|
690
|
+
*/
|
|
691
|
+
declare function string<const R extends boolean>(opts: StringOptions & {
|
|
692
|
+
required: R;
|
|
693
|
+
}): WithRequiredOption<CombinatorSchema<string>, R>;
|
|
694
|
+
/**
|
|
695
|
+
* Create a string argument schema with optional validation.
|
|
696
|
+
*
|
|
697
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps when it may be
|
|
698
|
+
* `false` but not `true`, as for `optional ? { required: false } : {}`, so that a positional
|
|
699
|
+
* argument with it is optional.
|
|
700
|
+
*
|
|
535
701
|
* @param opts - Validation options.
|
|
536
702
|
* @returns A combinator schema that resolves to string.
|
|
537
703
|
*
|
|
@@ -541,14 +707,26 @@ interface StringOptions extends BaseOptions {
|
|
|
541
707
|
* name: string({ minLength: 1, maxLength: 50 })
|
|
542
708
|
* }
|
|
543
709
|
* ```
|
|
710
|
+
*/
|
|
711
|
+
declare function string<const R extends boolean | undefined = boolean | undefined>(opts?: StringOptions & {
|
|
712
|
+
required?: R;
|
|
713
|
+
}): WithUnrequiredOption<CombinatorSchema<string>, R>;
|
|
714
|
+
/**
|
|
715
|
+
* Create a string argument schema with optional validation.
|
|
544
716
|
*
|
|
545
|
-
* @
|
|
717
|
+
* @param opts - Validation options.
|
|
718
|
+
* @returns A combinator schema that resolves to string.
|
|
719
|
+
*
|
|
720
|
+
* @example
|
|
721
|
+
* ```ts
|
|
722
|
+
* const args = {
|
|
723
|
+
* name: string({ minLength: 1, maxLength: 50 })
|
|
724
|
+
* }
|
|
725
|
+
* ```
|
|
546
726
|
*/
|
|
547
727
|
declare function string(opts?: StringOptions): CombinatorSchema<string>;
|
|
548
728
|
/**
|
|
549
729
|
* Options for the {@link number} combinator.
|
|
550
|
-
*
|
|
551
|
-
* @experimental
|
|
552
730
|
*/
|
|
553
731
|
interface NumberOptions extends BaseOptions {
|
|
554
732
|
/**
|
|
@@ -565,6 +743,8 @@ interface NumberOptions extends BaseOptions {
|
|
|
565
743
|
*
|
|
566
744
|
* Accepts any numeric value (integer or float).
|
|
567
745
|
*
|
|
746
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps.
|
|
747
|
+
*
|
|
568
748
|
* @param opts - Range options.
|
|
569
749
|
* @returns A combinator schema that resolves to number.
|
|
570
750
|
*
|
|
@@ -574,14 +754,50 @@ interface NumberOptions extends BaseOptions {
|
|
|
574
754
|
* timeout: number({ min: 0, max: 30000 })
|
|
575
755
|
* }
|
|
576
756
|
* ```
|
|
757
|
+
*/
|
|
758
|
+
declare function number<const R extends boolean>(opts: NumberOptions & {
|
|
759
|
+
required: R;
|
|
760
|
+
}): WithRequiredOption<CombinatorSchema<number>, R>;
|
|
761
|
+
/**
|
|
762
|
+
* Create a number argument schema with optional range validation.
|
|
577
763
|
*
|
|
578
|
-
*
|
|
764
|
+
* Accepts any numeric value (integer or float).
|
|
765
|
+
*
|
|
766
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps when it may be
|
|
767
|
+
* `false` but not `true`, as for `optional ? { required: false } : {}`, so that a positional
|
|
768
|
+
* argument with it is optional.
|
|
769
|
+
*
|
|
770
|
+
* @param opts - Range options.
|
|
771
|
+
* @returns A combinator schema that resolves to number.
|
|
772
|
+
*
|
|
773
|
+
* @example
|
|
774
|
+
* ```ts
|
|
775
|
+
* const args = {
|
|
776
|
+
* timeout: number({ min: 0, max: 30000 })
|
|
777
|
+
* }
|
|
778
|
+
* ```
|
|
779
|
+
*/
|
|
780
|
+
declare function number<const R extends boolean | undefined = boolean | undefined>(opts?: NumberOptions & {
|
|
781
|
+
required?: R;
|
|
782
|
+
}): WithUnrequiredOption<CombinatorSchema<number>, R>;
|
|
783
|
+
/**
|
|
784
|
+
* Create a number argument schema with optional range validation.
|
|
785
|
+
*
|
|
786
|
+
* Accepts any numeric value (integer or float).
|
|
787
|
+
*
|
|
788
|
+
* @param opts - Range options.
|
|
789
|
+
* @returns A combinator schema that resolves to number.
|
|
790
|
+
*
|
|
791
|
+
* @example
|
|
792
|
+
* ```ts
|
|
793
|
+
* const args = {
|
|
794
|
+
* timeout: number({ min: 0, max: 30000 })
|
|
795
|
+
* }
|
|
796
|
+
* ```
|
|
579
797
|
*/
|
|
580
798
|
declare function number(opts?: NumberOptions): CombinatorSchema<number>;
|
|
581
799
|
/**
|
|
582
800
|
* Options for the {@link integer} combinator.
|
|
583
|
-
*
|
|
584
|
-
* @experimental
|
|
585
801
|
*/
|
|
586
802
|
interface IntegerOptions extends BaseOptions {
|
|
587
803
|
/**
|
|
@@ -598,6 +814,30 @@ interface IntegerOptions extends BaseOptions {
|
|
|
598
814
|
*
|
|
599
815
|
* Only accepts integer values (no decimals).
|
|
600
816
|
*
|
|
817
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps.
|
|
818
|
+
*
|
|
819
|
+
* @param opts - Range options.
|
|
820
|
+
* @returns A combinator schema that resolves to number (integer).
|
|
821
|
+
*
|
|
822
|
+
* @example
|
|
823
|
+
* ```ts
|
|
824
|
+
* const args = {
|
|
825
|
+
* retries: integer({ min: 0, max: 10 })
|
|
826
|
+
* }
|
|
827
|
+
* ```
|
|
828
|
+
*/
|
|
829
|
+
declare function integer<const R extends boolean>(opts: IntegerOptions & {
|
|
830
|
+
required: R;
|
|
831
|
+
}): WithRequiredOption<CombinatorSchema<number>, R>;
|
|
832
|
+
/**
|
|
833
|
+
* Create an integer argument schema with optional range validation.
|
|
834
|
+
*
|
|
835
|
+
* Only accepts integer values (no decimals).
|
|
836
|
+
*
|
|
837
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps when it may be
|
|
838
|
+
* `false` but not `true`, as for `optional ? { required: false } : {}`, so that a positional
|
|
839
|
+
* argument with it is optional.
|
|
840
|
+
*
|
|
601
841
|
* @param opts - Range options.
|
|
602
842
|
* @returns A combinator schema that resolves to number (integer).
|
|
603
843
|
*
|
|
@@ -607,14 +847,28 @@ interface IntegerOptions extends BaseOptions {
|
|
|
607
847
|
* retries: integer({ min: 0, max: 10 })
|
|
608
848
|
* }
|
|
609
849
|
* ```
|
|
850
|
+
*/
|
|
851
|
+
declare function integer<const R extends boolean | undefined = boolean | undefined>(opts?: IntegerOptions & {
|
|
852
|
+
required?: R;
|
|
853
|
+
}): WithUnrequiredOption<CombinatorSchema<number>, R>;
|
|
854
|
+
/**
|
|
855
|
+
* Create an integer argument schema with optional range validation.
|
|
856
|
+
*
|
|
857
|
+
* Only accepts integer values (no decimals).
|
|
858
|
+
*
|
|
859
|
+
* @param opts - Range options.
|
|
860
|
+
* @returns A combinator schema that resolves to number (integer).
|
|
610
861
|
*
|
|
611
|
-
* @
|
|
862
|
+
* @example
|
|
863
|
+
* ```ts
|
|
864
|
+
* const args = {
|
|
865
|
+
* retries: integer({ min: 0, max: 10 })
|
|
866
|
+
* }
|
|
867
|
+
* ```
|
|
612
868
|
*/
|
|
613
869
|
declare function integer(opts?: IntegerOptions): CombinatorSchema<number>;
|
|
614
870
|
/**
|
|
615
871
|
* Options for the {@link float} combinator.
|
|
616
|
-
*
|
|
617
|
-
* @experimental
|
|
618
872
|
*/
|
|
619
873
|
interface FloatOptions extends BaseOptions {
|
|
620
874
|
/**
|
|
@@ -631,6 +885,8 @@ interface FloatOptions extends BaseOptions {
|
|
|
631
885
|
*
|
|
632
886
|
* Rejects `NaN` and `Infinity` values.
|
|
633
887
|
*
|
|
888
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps.
|
|
889
|
+
*
|
|
634
890
|
* @param opts - Range options.
|
|
635
891
|
* @returns A combinator schema that resolves to number (float).
|
|
636
892
|
*
|
|
@@ -640,14 +896,50 @@ interface FloatOptions extends BaseOptions {
|
|
|
640
896
|
* ratio: float({ min: 0, max: 1 })
|
|
641
897
|
* }
|
|
642
898
|
* ```
|
|
899
|
+
*/
|
|
900
|
+
declare function float<const R extends boolean>(opts: FloatOptions & {
|
|
901
|
+
required: R;
|
|
902
|
+
}): WithRequiredOption<CombinatorSchema<number>, R>;
|
|
903
|
+
/**
|
|
904
|
+
* Create a floating-point argument schema with optional range validation.
|
|
905
|
+
*
|
|
906
|
+
* Rejects `NaN` and `Infinity` values.
|
|
907
|
+
*
|
|
908
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps when it may be
|
|
909
|
+
* `false` but not `true`, as for `optional ? { required: false } : {}`, so that a positional
|
|
910
|
+
* argument with it is optional.
|
|
643
911
|
*
|
|
644
|
-
* @
|
|
912
|
+
* @param opts - Range options.
|
|
913
|
+
* @returns A combinator schema that resolves to number (float).
|
|
914
|
+
*
|
|
915
|
+
* @example
|
|
916
|
+
* ```ts
|
|
917
|
+
* const args = {
|
|
918
|
+
* ratio: float({ min: 0, max: 1 })
|
|
919
|
+
* }
|
|
920
|
+
* ```
|
|
921
|
+
*/
|
|
922
|
+
declare function float<const R extends boolean | undefined = boolean | undefined>(opts?: FloatOptions & {
|
|
923
|
+
required?: R;
|
|
924
|
+
}): WithUnrequiredOption<CombinatorSchema<number>, R>;
|
|
925
|
+
/**
|
|
926
|
+
* Create a floating-point argument schema with optional range validation.
|
|
927
|
+
*
|
|
928
|
+
* Rejects `NaN` and `Infinity` values.
|
|
929
|
+
*
|
|
930
|
+
* @param opts - Range options.
|
|
931
|
+
* @returns A combinator schema that resolves to number (float).
|
|
932
|
+
*
|
|
933
|
+
* @example
|
|
934
|
+
* ```ts
|
|
935
|
+
* const args = {
|
|
936
|
+
* ratio: float({ min: 0, max: 1 })
|
|
937
|
+
* }
|
|
938
|
+
* ```
|
|
645
939
|
*/
|
|
646
940
|
declare function float(opts?: FloatOptions): CombinatorSchema<number>;
|
|
647
941
|
/**
|
|
648
942
|
* Options for the {@link boolean} combinator.
|
|
649
|
-
*
|
|
650
|
-
* @experimental
|
|
651
943
|
*/
|
|
652
944
|
interface BooleanOptions extends BaseOptions {
|
|
653
945
|
/**
|
|
@@ -659,7 +951,35 @@ interface BooleanOptions extends BaseOptions {
|
|
|
659
951
|
* Create a boolean argument schema.
|
|
660
952
|
*
|
|
661
953
|
* Boolean arguments are existence-based. The resolver passes `"true"` or `"false"`
|
|
662
|
-
* to the parse function based on the presence or negation of the flag
|
|
954
|
+
* to the parse function based on the presence or negation of the flag, or on an explicit
|
|
955
|
+
* `=true` / `=false` value. Other inline values are rejected before the parse function is called.
|
|
956
|
+
*
|
|
957
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps.
|
|
958
|
+
*
|
|
959
|
+
* @param opts - Boolean options.
|
|
960
|
+
* @returns A combinator schema for boolean flags.
|
|
961
|
+
*
|
|
962
|
+
* @example
|
|
963
|
+
* ```ts
|
|
964
|
+
* const args = {
|
|
965
|
+
* color: boolean({ negatable: true })
|
|
966
|
+
* }
|
|
967
|
+
* // Usage: --color (true), --no-color (false)
|
|
968
|
+
* ```
|
|
969
|
+
*/
|
|
970
|
+
declare function boolean<const R extends boolean>(opts: BooleanOptions & {
|
|
971
|
+
required: R;
|
|
972
|
+
}): WithRequiredOption<CombinatorSchema<boolean>, R>;
|
|
973
|
+
/**
|
|
974
|
+
* Create a boolean argument schema.
|
|
975
|
+
*
|
|
976
|
+
* Boolean arguments are existence-based. The resolver passes `"true"` or `"false"`
|
|
977
|
+
* to the parse function based on the presence or negation of the flag, or on an explicit
|
|
978
|
+
* `=true` / `=false` value. Other inline values are rejected before the parse function is called.
|
|
979
|
+
*
|
|
980
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps when it may be
|
|
981
|
+
* `false` but not `true`, as for `optional ? { required: false } : {}`, so that a positional
|
|
982
|
+
* argument with it is optional.
|
|
663
983
|
*
|
|
664
984
|
* @param opts - Boolean options.
|
|
665
985
|
* @returns A combinator schema for boolean flags.
|
|
@@ -671,8 +991,27 @@ interface BooleanOptions extends BaseOptions {
|
|
|
671
991
|
* }
|
|
672
992
|
* // Usage: --color (true), --no-color (false)
|
|
673
993
|
* ```
|
|
994
|
+
*/
|
|
995
|
+
declare function boolean<const R extends boolean | undefined = boolean | undefined>(opts?: BooleanOptions & {
|
|
996
|
+
required?: R;
|
|
997
|
+
}): WithUnrequiredOption<CombinatorSchema<boolean>, R>;
|
|
998
|
+
/**
|
|
999
|
+
* Create a boolean argument schema.
|
|
674
1000
|
*
|
|
675
|
-
*
|
|
1001
|
+
* Boolean arguments are existence-based. The resolver passes `"true"` or `"false"`
|
|
1002
|
+
* to the parse function based on the presence or negation of the flag, or on an explicit
|
|
1003
|
+
* `=true` / `=false` value. Other inline values are rejected before the parse function is called.
|
|
1004
|
+
*
|
|
1005
|
+
* @param opts - Boolean options.
|
|
1006
|
+
* @returns A combinator schema for boolean flags.
|
|
1007
|
+
*
|
|
1008
|
+
* @example
|
|
1009
|
+
* ```ts
|
|
1010
|
+
* const args = {
|
|
1011
|
+
* color: boolean({ negatable: true })
|
|
1012
|
+
* }
|
|
1013
|
+
* // Usage: --color (true), --no-color (false)
|
|
1014
|
+
* ```
|
|
676
1015
|
*/
|
|
677
1016
|
declare function boolean(opts?: BooleanOptions): CombinatorSchema<boolean>;
|
|
678
1017
|
/**
|
|
@@ -681,13 +1020,46 @@ declare function boolean(opts?: BooleanOptions): CombinatorSchema<boolean>;
|
|
|
681
1020
|
type ArgSchemaPositionalType = {
|
|
682
1021
|
type: 'positional';
|
|
683
1022
|
};
|
|
1023
|
+
/**
|
|
1024
|
+
* The properties of a parser that {@link positional} keeps.
|
|
1025
|
+
*/
|
|
1026
|
+
type PositionalParserKey = 'parse' | 'metavar' | 'description' | 'hidden' | 'required' | 'default' | 'multiple';
|
|
1027
|
+
/**
|
|
1028
|
+
* The positional argument schema that {@link positional} builds from a parser: the properties it
|
|
1029
|
+
* keeps, with their types, as `type: 'positional'`. A parser of type `any` is read as
|
|
1030
|
+
* {@link UntypedCombinatorSchema}.
|
|
1031
|
+
*
|
|
1032
|
+
* As in {@link WithFlag}, it maps the properties of the parser itself, and reads `any` in a part of
|
|
1033
|
+
* its own, so that TypeScript still sees them for a parser typed by a type parameter.
|
|
1034
|
+
*/
|
|
1035
|
+
type PositionalWithParser<S> = { [K in keyof S as K extends PositionalParserKey ? K : never]: S[K]; } & ArgSchemaPositionalType & (0 extends 1 & S ? Pick<UntypedCombinatorSchema, PositionalParserKey> : unknown);
|
|
1036
|
+
/**
|
|
1037
|
+
* The positional argument schema that {@link positional} returns for a parser `S`:
|
|
1038
|
+
* {@link PositionalWithParser} for each schema of a union. For a parser typed by a type parameter,
|
|
1039
|
+
* TypeScript keeps this type unresolved, and relates it as {@link PositionalWithParser} of the
|
|
1040
|
+
* constraint of the type parameter, as for {@link Modified}.
|
|
1041
|
+
*/
|
|
1042
|
+
type PositionalOf<S> = S extends unknown ? PositionalWithParser<S> : never;
|
|
684
1043
|
/**
|
|
685
1044
|
* Create a positional argument schema.
|
|
686
1045
|
*
|
|
687
1046
|
* Without a parser, resolves to string.
|
|
688
1047
|
* With a parser (e.g., `positional(integer())`), resolves to the parser's return type.
|
|
689
1048
|
*
|
|
1049
|
+
* The positional argument keeps `required`, `default` and `multiple` of the parser, with their
|
|
1050
|
+
* types: `positional(unrequired(integer()))` is optional, and `positional(multiple(integer()))`
|
|
1051
|
+
* resolves to an array, as `multiple(positional(integer()))` does.
|
|
1052
|
+
*
|
|
1053
|
+
* An instantiation expression with one type argument, such as `typeof positional<number>`, is a
|
|
1054
|
+
* type error, since the overload for a union of schemas and the overloads for options take one
|
|
1055
|
+
* type argument too, which must be a schema for the former and a boolean (the type of `required`)
|
|
1056
|
+
* for the latter: give `S` as well, as in `typeof positional<number, CombinatorSchema<number>>`.
|
|
1057
|
+
*
|
|
690
1058
|
* @typeParam T - The parser's resolved type.
|
|
1059
|
+
* @typeParam S - The type of the parser, inferred from `parser`, whose `required`, `default`,
|
|
1060
|
+
* `multiple`, `description`, `hidden` and `metavar` the positional argument keeps. If type
|
|
1061
|
+
* arguments are given explicitly without `S`, `S` is `CombinatorSchema<T>`, and the type of the
|
|
1062
|
+
* result lacks them, although the returned object has them.
|
|
691
1063
|
*
|
|
692
1064
|
* @param parser - The parser combinator schema.
|
|
693
1065
|
* @returns A positional argument schema resolving to the parser's type.
|
|
@@ -700,17 +1072,53 @@ type ArgSchemaPositionalType = {
|
|
|
700
1072
|
* query: unrequired(positional()) // optional positional
|
|
701
1073
|
* }
|
|
702
1074
|
* ```
|
|
1075
|
+
*/
|
|
1076
|
+
declare function positional<T, S extends CombinatorSchema<T> = CombinatorSchema<T>>(parser: S & CombinatorSchema<T>): PositionalOf<S>;
|
|
1077
|
+
/**
|
|
1078
|
+
* Create a positional argument schema from a union of combinator schemas of different types, such
|
|
1079
|
+
* as `strict ? integer() : string()`.
|
|
1080
|
+
*
|
|
1081
|
+
* The positional argument resolves to a value of any of their types: a number or a string for
|
|
1082
|
+
* `strict ? integer() : string()`. Each schema of the union keeps its `required`, `default` and
|
|
1083
|
+
* `multiple`, and the argument is present only when each of them gives it a value:
|
|
1084
|
+
* `positional(strict ? multiple(integer()) : string())` resolves to an array of numbers or a
|
|
1085
|
+
* string, and is optional, since a `multiple` positional argument without `required: true` or a
|
|
1086
|
+
* default may be missing.
|
|
1087
|
+
*
|
|
1088
|
+
* @typeParam S - The type of the parser, inferred from `parser`: a union of schemas of different
|
|
1089
|
+
* types.
|
|
1090
|
+
*
|
|
1091
|
+
* @param parser - The parser combinator schema.
|
|
1092
|
+
* @returns A positional argument schema resolving to a value of any of the types that the schemas
|
|
1093
|
+
* of the union parse to, in an array for a `multiple` schema.
|
|
703
1094
|
*
|
|
704
|
-
* @
|
|
1095
|
+
* @example
|
|
1096
|
+
* ```ts
|
|
1097
|
+
* const strict = process.argv.includes('--strict')
|
|
1098
|
+
* const args = {
|
|
1099
|
+
* // a number or a string
|
|
1100
|
+
* port: positional(strict ? integer({ min: 1 }) : string())
|
|
1101
|
+
* }
|
|
1102
|
+
* ```
|
|
705
1103
|
*/
|
|
706
|
-
declare function positional<
|
|
1104
|
+
declare function positional<S extends CombinatorSchema<unknown>>(parser: S): PositionalOf<S>;
|
|
707
1105
|
/**
|
|
708
1106
|
* Create a positional argument schema.
|
|
709
1107
|
*
|
|
710
1108
|
* Without a parser, resolves to string.
|
|
711
1109
|
* With a parser (e.g., `positional(integer())`), resolves to the parser's return type.
|
|
712
1110
|
*
|
|
713
|
-
*
|
|
1111
|
+
* Without a parser, the schema has a `parse` function that returns the value as is, so that the
|
|
1112
|
+
* modifiers, such as {@link multiple} and {@link withDefault}, take it: `multiple(positional())`
|
|
1113
|
+
* collects the values as strings.
|
|
1114
|
+
*
|
|
1115
|
+
* With `required: false` in the options, the positional argument is optional, in its type too. A
|
|
1116
|
+
* `required` of type `boolean`, which may be `false`, types it as optional as well.
|
|
1117
|
+
*
|
|
1118
|
+
* @typeParam R - The type of `required` in the options, which the positional argument keeps.
|
|
1119
|
+
*
|
|
1120
|
+
* @param parser - Base options (description, short, hidden, required). `short` has no effect on a
|
|
1121
|
+
* positional argument.
|
|
714
1122
|
* @returns A positional argument schema resolving to string.
|
|
715
1123
|
*
|
|
716
1124
|
* @example
|
|
@@ -721,19 +1129,102 @@ declare function positional<T>(parser: CombinatorSchema<T>): CombinatorSchema<T>
|
|
|
721
1129
|
* query: unrequired(positional()) // optional positional
|
|
722
1130
|
* }
|
|
723
1131
|
* ```
|
|
1132
|
+
*/
|
|
1133
|
+
declare function positional<const R extends boolean>(parser: BaseOptions & {
|
|
1134
|
+
required: R;
|
|
1135
|
+
}): WithRequiredOption<Omit<CombinatorSchema<string>, 'type'> & ArgSchemaPositionalType, R>;
|
|
1136
|
+
/**
|
|
1137
|
+
* Create a positional argument schema.
|
|
724
1138
|
*
|
|
725
|
-
*
|
|
1139
|
+
* Without a parser, resolves to string.
|
|
1140
|
+
* With a parser (e.g., `positional(integer())`), resolves to the parser's return type.
|
|
1141
|
+
*
|
|
1142
|
+
* Without a parser, the schema has a `parse` function that returns the value as is, so that the
|
|
1143
|
+
* modifiers, such as {@link multiple} and {@link withDefault}, take it: `multiple(positional())`
|
|
1144
|
+
* collects the values as strings.
|
|
1145
|
+
*
|
|
1146
|
+
* With a `required: false` that the options have only in some cases, with no `required` in the
|
|
1147
|
+
* others, such as `optional ? { required: false } : {}`, the positional argument is optional, in
|
|
1148
|
+
* its type too.
|
|
1149
|
+
*
|
|
1150
|
+
* @typeParam R - The type of `required` in the options, which the positional argument keeps when it
|
|
1151
|
+
* may be `false` but not `true`, as for `optional ? { required: false } : {}`.
|
|
1152
|
+
*
|
|
1153
|
+
* @param parser - Optional base options (description, short, hidden, required). `short` has no
|
|
1154
|
+
* effect on a positional argument.
|
|
1155
|
+
* @returns A positional argument schema resolving to string.
|
|
1156
|
+
*
|
|
1157
|
+
* @example
|
|
1158
|
+
* ```ts
|
|
1159
|
+
* const args = {
|
|
1160
|
+
* command: positional(), // resolves to string
|
|
1161
|
+
* port: positional(integer()), // resolves to number
|
|
1162
|
+
* query: unrequired(positional()) // optional positional
|
|
1163
|
+
* }
|
|
1164
|
+
* ```
|
|
726
1165
|
*/
|
|
727
|
-
declare function positional(parser?: BaseOptions
|
|
1166
|
+
declare function positional<const R extends boolean | undefined = boolean | undefined>(parser?: BaseOptions & {
|
|
1167
|
+
required?: R;
|
|
1168
|
+
}): WithUnrequiredOption<Omit<CombinatorSchema<string>, 'type'> & ArgSchemaPositionalType, R>;
|
|
1169
|
+
/**
|
|
1170
|
+
* Create a positional argument schema.
|
|
1171
|
+
*
|
|
1172
|
+
* Without a parser, resolves to string.
|
|
1173
|
+
* With a parser (e.g., `positional(integer())`), resolves to the parser's return type.
|
|
1174
|
+
*
|
|
1175
|
+
* Without a parser, the schema has a `parse` function that returns the value as is, so that the
|
|
1176
|
+
* modifiers, such as {@link multiple} and {@link withDefault}, take it: `multiple(positional())`
|
|
1177
|
+
* collects the values as strings.
|
|
1178
|
+
*
|
|
1179
|
+
* @param parser - Optional base options (description, short, hidden, required). `short` has no
|
|
1180
|
+
* effect on a positional argument.
|
|
1181
|
+
* @returns A positional argument schema resolving to string.
|
|
1182
|
+
*
|
|
1183
|
+
* @example
|
|
1184
|
+
* ```ts
|
|
1185
|
+
* const args = {
|
|
1186
|
+
* command: positional(), // resolves to string
|
|
1187
|
+
* port: positional(integer()), // resolves to number
|
|
1188
|
+
* query: unrequired(positional()) // optional positional
|
|
1189
|
+
* }
|
|
1190
|
+
* ```
|
|
1191
|
+
*/
|
|
1192
|
+
declare function positional(parser?: BaseOptions): Omit<CombinatorSchema<string>, 'type'> & ArgSchemaPositionalType;
|
|
1193
|
+
/**
|
|
1194
|
+
* Create an enum-like argument schema with literal type inference.
|
|
1195
|
+
*
|
|
1196
|
+
* Uses `const T` generic to infer literal union types from the values array.
|
|
1197
|
+
*
|
|
1198
|
+
* @typeParam T - The readonly array of allowed string values.
|
|
1199
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps.
|
|
1200
|
+
*
|
|
1201
|
+
* @param values - Allowed values.
|
|
1202
|
+
* @param opts - Common options (description, short, hidden, required).
|
|
1203
|
+
* @returns A combinator schema that resolves to a union of the allowed values.
|
|
1204
|
+
*
|
|
1205
|
+
* @example
|
|
1206
|
+
* ```ts
|
|
1207
|
+
* const args = {
|
|
1208
|
+
* level: choice(['debug', 'info', 'warn', 'error'] as const)
|
|
1209
|
+
* }
|
|
1210
|
+
* // typeof values.level === 'debug' | 'info' | 'warn' | 'error'
|
|
1211
|
+
* ```
|
|
1212
|
+
*/
|
|
1213
|
+
declare function choice<const T extends readonly string[], const R extends boolean>(values: T, opts: BaseOptions & {
|
|
1214
|
+
required: R;
|
|
1215
|
+
}): WithRequiredOption<CombinatorSchema<T[number]>, R>;
|
|
728
1216
|
/**
|
|
729
1217
|
* Create an enum-like argument schema with literal type inference.
|
|
730
1218
|
*
|
|
731
1219
|
* Uses `const T` generic to infer literal union types from the values array.
|
|
732
1220
|
*
|
|
733
1221
|
* @typeParam T - The readonly array of allowed string values.
|
|
1222
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps when it may be
|
|
1223
|
+
* `false` but not `true`, as for `optional ? { required: false } : {}`, so that a positional
|
|
1224
|
+
* argument with it is optional.
|
|
734
1225
|
*
|
|
735
1226
|
* @param values - Allowed values.
|
|
736
|
-
* @param opts - Common options (description, short, required).
|
|
1227
|
+
* @param opts - Common options (description, short, hidden, required).
|
|
737
1228
|
* @returns A combinator schema that resolves to a union of the allowed values.
|
|
738
1229
|
*
|
|
739
1230
|
* @example
|
|
@@ -743,21 +1234,42 @@ declare function positional(parser?: BaseOptions): ArgSchema & ArgSchemaPosition
|
|
|
743
1234
|
* }
|
|
744
1235
|
* // typeof values.level === 'debug' | 'info' | 'warn' | 'error'
|
|
745
1236
|
* ```
|
|
1237
|
+
*/
|
|
1238
|
+
declare function choice<const T extends readonly string[], const R extends boolean | undefined = boolean | undefined>(values: T, opts?: BaseOptions & {
|
|
1239
|
+
required?: R;
|
|
1240
|
+
}): WithUnrequiredOption<CombinatorSchema<T[number]>, R>;
|
|
1241
|
+
/**
|
|
1242
|
+
* Create an enum-like argument schema with literal type inference.
|
|
1243
|
+
*
|
|
1244
|
+
* Uses `const T` generic to infer literal union types from the values array.
|
|
1245
|
+
*
|
|
1246
|
+
* @typeParam T - The readonly array of allowed string values.
|
|
1247
|
+
*
|
|
1248
|
+
* @param values - Allowed values.
|
|
1249
|
+
* @param opts - Common options (description, short, hidden, required).
|
|
1250
|
+
* @returns A combinator schema that resolves to a union of the allowed values.
|
|
746
1251
|
*
|
|
747
|
-
* @
|
|
1252
|
+
* @example
|
|
1253
|
+
* ```ts
|
|
1254
|
+
* const args = {
|
|
1255
|
+
* level: choice(['debug', 'info', 'warn', 'error'] as const)
|
|
1256
|
+
* }
|
|
1257
|
+
* // typeof values.level === 'debug' | 'info' | 'warn' | 'error'
|
|
1258
|
+
* ```
|
|
748
1259
|
*/
|
|
749
1260
|
declare function choice<const T extends readonly string[]>(values: T, opts?: BaseOptions): CombinatorSchema<T[number]>;
|
|
750
1261
|
/**
|
|
751
1262
|
* Options for the {@link combinator} factory function.
|
|
752
1263
|
*
|
|
753
1264
|
* @typeParam T - The parsed value type.
|
|
754
|
-
*
|
|
755
|
-
* @experimental
|
|
756
1265
|
*/
|
|
757
1266
|
interface CombinatorOptions<T> extends BaseOptions {
|
|
758
1267
|
/**
|
|
759
1268
|
* The parse function that converts a string to the desired type.
|
|
760
1269
|
*
|
|
1270
|
+
* It is called synchronously, as {@link ArgSchema}.parse is: a promise that it returns becomes
|
|
1271
|
+
* the value as is, and its rejection is not reported as an error.
|
|
1272
|
+
*
|
|
761
1273
|
* @param value - The input string value.
|
|
762
1274
|
* @returns The parsed value of type T.
|
|
763
1275
|
*/
|
|
@@ -779,6 +1291,7 @@ interface CombinatorOptions<T> extends BaseOptions {
|
|
|
779
1291
|
* The returned schema has `type: 'custom'`.
|
|
780
1292
|
*
|
|
781
1293
|
* @typeParam T - The parsed value type.
|
|
1294
|
+
* @typeParam R - The type of `required` in the configuration, which the schema keeps.
|
|
782
1295
|
*
|
|
783
1296
|
* @param config - Configuration with a parse function and optional metavar.
|
|
784
1297
|
* @returns A combinator schema that resolves to the parse function's return type.
|
|
@@ -796,8 +1309,71 @@ interface CombinatorOptions<T> extends BaseOptions {
|
|
|
796
1309
|
* metavar: 'date'
|
|
797
1310
|
* })
|
|
798
1311
|
* ```
|
|
1312
|
+
*/
|
|
1313
|
+
declare function combinator<T, const R extends boolean>(config: CombinatorOptions<T> & {
|
|
1314
|
+
required: R;
|
|
1315
|
+
}): WithRequiredOption<CombinatorSchema<T>, R>;
|
|
1316
|
+
/**
|
|
1317
|
+
* Create a custom argument schema with a user-defined parse function.
|
|
799
1318
|
*
|
|
800
|
-
*
|
|
1319
|
+
* This is the most general custom combinator. Use it when none of the built-in
|
|
1320
|
+
* base combinators ({@link string}, {@link number}, {@link integer},
|
|
1321
|
+
* {@link float}, {@link boolean}, {@link choice}) fit your needs.
|
|
1322
|
+
*
|
|
1323
|
+
* The returned schema has `type: 'custom'`.
|
|
1324
|
+
*
|
|
1325
|
+
* @typeParam T - The parsed value type.
|
|
1326
|
+
* @typeParam R - The type of `required` in the configuration, which the schema keeps when it may be
|
|
1327
|
+
* `false` but not `true`, as for `optional ? { required: false } : {}`, so that a positional
|
|
1328
|
+
* argument with it is optional.
|
|
1329
|
+
*
|
|
1330
|
+
* @param config - Configuration with a parse function and optional metavar.
|
|
1331
|
+
* @returns A combinator schema that resolves to the parse function's return type.
|
|
1332
|
+
*
|
|
1333
|
+
* @example
|
|
1334
|
+
* ```ts
|
|
1335
|
+
* const date = combinator({
|
|
1336
|
+
* parse: (value) => {
|
|
1337
|
+
* const d = new Date(value)
|
|
1338
|
+
* if (isNaN(d.getTime())) {
|
|
1339
|
+
* throw new Error('Invalid date format')
|
|
1340
|
+
* }
|
|
1341
|
+
* return d
|
|
1342
|
+
* },
|
|
1343
|
+
* metavar: 'date'
|
|
1344
|
+
* })
|
|
1345
|
+
* ```
|
|
1346
|
+
*/
|
|
1347
|
+
declare function combinator<T, const R extends boolean | undefined = boolean | undefined>(config: CombinatorOptions<T> & {
|
|
1348
|
+
required?: R;
|
|
1349
|
+
}): WithUnrequiredOption<CombinatorSchema<T>, R>;
|
|
1350
|
+
/**
|
|
1351
|
+
* Create a custom argument schema with a user-defined parse function.
|
|
1352
|
+
*
|
|
1353
|
+
* This is the most general custom combinator. Use it when none of the built-in
|
|
1354
|
+
* base combinators ({@link string}, {@link number}, {@link integer},
|
|
1355
|
+
* {@link float}, {@link boolean}, {@link choice}) fit your needs.
|
|
1356
|
+
*
|
|
1357
|
+
* The returned schema has `type: 'custom'`.
|
|
1358
|
+
*
|
|
1359
|
+
* @typeParam T - The parsed value type.
|
|
1360
|
+
*
|
|
1361
|
+
* @param config - Configuration with a parse function and optional metavar.
|
|
1362
|
+
* @returns A combinator schema that resolves to the parse function's return type.
|
|
1363
|
+
*
|
|
1364
|
+
* @example
|
|
1365
|
+
* ```ts
|
|
1366
|
+
* const date = combinator({
|
|
1367
|
+
* parse: (value) => {
|
|
1368
|
+
* const d = new Date(value)
|
|
1369
|
+
* if (isNaN(d.getTime())) {
|
|
1370
|
+
* throw new Error('Invalid date format')
|
|
1371
|
+
* }
|
|
1372
|
+
* return d
|
|
1373
|
+
* },
|
|
1374
|
+
* metavar: 'date'
|
|
1375
|
+
* })
|
|
1376
|
+
* ```
|
|
801
1377
|
*/
|
|
802
1378
|
declare function combinator<T>(config: CombinatorOptions<T>): CombinatorSchema<T>;
|
|
803
1379
|
/**
|
|
@@ -805,9 +1381,24 @@ declare function combinator<T>(config: CombinatorOptions<T>): CombinatorSchema<T
|
|
|
805
1381
|
*
|
|
806
1382
|
* Creates a new schema that applies `transform` to the result of `schema.parse`.
|
|
807
1383
|
* The original schema is not modified.
|
|
1384
|
+
* Other modifiers on `schema` (for example {@link multiple}) are kept, and `transform` is applied
|
|
1385
|
+
* to each value of a `multiple` schema.
|
|
1386
|
+
*
|
|
1387
|
+
* A default set on `schema` is kept, but it does not go through `transform`: when it is used, the
|
|
1388
|
+
* value is the default as is, although it is typed as `U`. Set the default after `map()`, with a
|
|
1389
|
+
* transformed value.
|
|
1390
|
+
*
|
|
1391
|
+
* A union of schemas of different types, such as `strict ? integer() : string()`, matches the
|
|
1392
|
+
* last overload, whose `transform` takes a value of any of their types. An instantiation expression
|
|
1393
|
+
* with two type arguments, such as `typeof map<number, string>`, is a type error, since that
|
|
1394
|
+
* overload takes two type arguments too, the first of which must be a schema: give `S` as well, as
|
|
1395
|
+
* in `typeof map<number, string, CombinatorSchema<number>>`.
|
|
808
1396
|
*
|
|
809
1397
|
* @typeParam T - The input schema's parsed type.
|
|
810
1398
|
* @typeParam U - The transformed type.
|
|
1399
|
+
* @typeParam S - The input combinator schema, inferred from `schema`. Its other modifiers are kept.
|
|
1400
|
+
* If type arguments are given explicitly without `S`, `S` is `CombinatorSchema<T>`, and the type
|
|
1401
|
+
* of the result lacks the other modifiers, although the returned object has them.
|
|
811
1402
|
*
|
|
812
1403
|
* @param schema - The base combinator schema.
|
|
813
1404
|
* @param transform - The transformation function.
|
|
@@ -819,10 +1410,72 @@ declare function combinator<T>(config: CombinatorOptions<T>): CombinatorSchema<T
|
|
|
819
1410
|
* doubled: map(integer(), n => n * 2)
|
|
820
1411
|
* }
|
|
821
1412
|
* ```
|
|
1413
|
+
*/
|
|
1414
|
+
declare function map<T, U, S extends CombinatorSchema<T> = CombinatorSchema<T>>(schema: S & CombinatorSchema<T>, transform: (value: T) => U): Modified<S, Combinator<U>>;
|
|
1415
|
+
/**
|
|
1416
|
+
* Transform the output of a combinator schema, as the first overload does, for a schema that fits
|
|
1417
|
+
* the first overload but that TypeScript does not match with it at first, such as
|
|
1418
|
+
* `positional(integer())`, a class instance or a schema typed by an interface. This overload keeps
|
|
1419
|
+
* such a schema from matching the overload for a union of schemas of different types.
|
|
1420
|
+
*
|
|
1421
|
+
* Creates a new schema that applies `transform` to the result of `schema.parse`.
|
|
1422
|
+
* The original schema is not modified.
|
|
1423
|
+
* Other modifiers on `schema` (for example {@link multiple}) are kept, and `transform` is applied
|
|
1424
|
+
* to each value of a `multiple` schema.
|
|
1425
|
+
*
|
|
1426
|
+
* A default set on `schema` is kept, but it does not go through `transform`: when it is used, the
|
|
1427
|
+
* value is the default as is, although it is typed as `U`. Set the default after `map()`, with a
|
|
1428
|
+
* transformed value.
|
|
822
1429
|
*
|
|
823
|
-
* @
|
|
1430
|
+
* @typeParam T - The input schema's parsed type.
|
|
1431
|
+
* @typeParam U - The transformed type.
|
|
1432
|
+
* @typeParam S - The input combinator schema, inferred from `schema`. Its other modifiers are kept.
|
|
1433
|
+
*
|
|
1434
|
+
* @param schema - The base combinator schema.
|
|
1435
|
+
* @param transform - The transformation function.
|
|
1436
|
+
* @returns A new combinator schema that resolves to the transformed type.
|
|
1437
|
+
*
|
|
1438
|
+
* @example
|
|
1439
|
+
* ```ts
|
|
1440
|
+
* const args = {
|
|
1441
|
+
* doubled: map(positional(integer()), n => n * 2)
|
|
1442
|
+
* }
|
|
1443
|
+
* ```
|
|
824
1444
|
*/
|
|
825
|
-
declare function map<T, U>(schema:
|
|
1445
|
+
declare function map<T, U, S extends CombinatorSchema<T> = CombinatorSchema<T>>(schema: S & Combinator<T>, transform: (value: T) => U): Modified<S, Combinator<U>>;
|
|
1446
|
+
/**
|
|
1447
|
+
* Transform the output of a union of combinator schemas of different types, such as
|
|
1448
|
+
* `strict ? integer() : string()`.
|
|
1449
|
+
*
|
|
1450
|
+
* Creates a new schema that applies `transform` to the result of `schema.parse`, which is a value
|
|
1451
|
+
* of any of their types: a number or a string for `strict ? integer() : string()`.
|
|
1452
|
+
* The original schema is not modified.
|
|
1453
|
+
* Other modifiers on `schema` (for example {@link multiple}) are kept, and `transform` is applied
|
|
1454
|
+
* to each value of a `multiple` schema.
|
|
1455
|
+
*
|
|
1456
|
+
* A default set on `schema` is kept, but it does not go through `transform`: when it is used, the
|
|
1457
|
+
* value is the default as is, although it is typed as `U`. Set the default after `map()`, with a
|
|
1458
|
+
* transformed value.
|
|
1459
|
+
*
|
|
1460
|
+
* @typeParam S - The input combinator schema, inferred from `schema`: a union of schemas of
|
|
1461
|
+
* different types. Its other modifiers are kept.
|
|
1462
|
+
* @typeParam U - The transformed type.
|
|
1463
|
+
*
|
|
1464
|
+
* @param schema - The base combinator schema.
|
|
1465
|
+
* @param transform - The transformation function, which takes a value of any of the types that the
|
|
1466
|
+
* schemas of the union parse to.
|
|
1467
|
+
* @returns A new combinator schema that resolves to the transformed type.
|
|
1468
|
+
*
|
|
1469
|
+
* @example
|
|
1470
|
+
* ```ts
|
|
1471
|
+
* const strict = process.argv.includes('--strict')
|
|
1472
|
+
* const args = {
|
|
1473
|
+
* // `value` is a number or a string
|
|
1474
|
+
* timeout: map(strict ? integer({ min: 0 }) : string(), value => String(value))
|
|
1475
|
+
* }
|
|
1476
|
+
* ```
|
|
1477
|
+
*/
|
|
1478
|
+
declare function map<S extends CombinatorSchema<unknown>, U>(schema: S, transform: (value: ParsedType<S>) => U): Modified<S, Combinator<U>>;
|
|
826
1479
|
/**
|
|
827
1480
|
* Options for the {@link withDefault} combinator.
|
|
828
1481
|
*/
|
|
@@ -832,12 +1485,29 @@ type CombinatorWithDefault<T> = {
|
|
|
832
1485
|
/**
|
|
833
1486
|
* Set a default value on a combinator schema.
|
|
834
1487
|
*
|
|
835
|
-
* The original schema is not modified.
|
|
1488
|
+
* The original schema is not modified. The default must be a value of the schema's parsed type:
|
|
1489
|
+
* `T` is inferred from `schema` only, so `withDefault(choice(['auto', 'always']), 'awlays')` is a
|
|
1490
|
+
* type error instead of adding `'awlays'` to the type. The schema must parse to a string, number or
|
|
1491
|
+
* boolean, since the default can only be one of them and does not go through `parse`: a schema that
|
|
1492
|
+
* parses to another type, such as a `Date`, or whose `parse` can return `null` or `undefined`,
|
|
1493
|
+
* cannot have a default, and giving it one is a type error.
|
|
1494
|
+
* Other modifiers on `schema` (for example {@link multiple}) are kept. The default of a `multiple`
|
|
1495
|
+
* schema is one value of the parsed type, which becomes the only element of the array.
|
|
1496
|
+
*
|
|
1497
|
+
* A union of schemas of different types, such as `strict ? integer() : string()`, matches the
|
|
1498
|
+
* last overload, whose default may be a value of any of their types. An instantiation expression
|
|
1499
|
+
* with one type argument, such as `typeof withDefault<number>`, is a type error, since that
|
|
1500
|
+
* overload takes one type argument too, which must be a schema: give `D` as well, as in
|
|
1501
|
+
* `typeof withDefault<number, number>`.
|
|
836
1502
|
*
|
|
837
1503
|
* @typeParam T - The schema's parsed type.
|
|
1504
|
+
* @typeParam D - The type of the default value, which must be assignable to `T`.
|
|
1505
|
+
* @typeParam S - The input combinator schema, inferred from `schema`. Its other modifiers are kept.
|
|
1506
|
+
* If type arguments are given explicitly without `S`, `S` is `CombinatorSchema<T>`, and the type
|
|
1507
|
+
* of the result lacks the other modifiers, although the returned object has them.
|
|
838
1508
|
*
|
|
839
1509
|
* @param schema - The base combinator schema.
|
|
840
|
-
* @param defaultValue - The default value.
|
|
1510
|
+
* @param defaultValue - The default value, a value of the schema's parsed type.
|
|
841
1511
|
* @returns A new schema with the default value set.
|
|
842
1512
|
*
|
|
843
1513
|
* @example
|
|
@@ -846,17 +1516,119 @@ type CombinatorWithDefault<T> = {
|
|
|
846
1516
|
* port: withDefault(integer({ min: 1, max: 65535 }), 8080)
|
|
847
1517
|
* }
|
|
848
1518
|
* ```
|
|
1519
|
+
*/
|
|
1520
|
+
declare function withDefault<T extends string | boolean | number, D extends T = T, S extends CombinatorSchema<T> = CombinatorSchema<T>>(schema: S & CombinatorSchema<T>, defaultValue: D): Modified<S, CombinatorWithDefault<T>>;
|
|
1521
|
+
/**
|
|
1522
|
+
* Set a default value on a combinator schema, as the first overload does, for a schema that fits
|
|
1523
|
+
* the first overload but that TypeScript does not match with it at first, such as
|
|
1524
|
+
* `positional(integer())`, a class instance or a schema typed by an interface. This overload keeps
|
|
1525
|
+
* such a schema from matching the overload for a union of schemas of different types.
|
|
1526
|
+
*
|
|
1527
|
+
* The original schema is not modified. The default must be a value of the schema's parsed type. The
|
|
1528
|
+
* schema must parse to a string, number or boolean, since the default can only be one of them and
|
|
1529
|
+
* does not go through `parse`.
|
|
1530
|
+
* Other modifiers on `schema` (for example {@link multiple}) are kept. The default of a `multiple`
|
|
1531
|
+
* schema is one value of the parsed type, which becomes the only element of the array.
|
|
1532
|
+
*
|
|
1533
|
+
* @typeParam T - The schema's parsed type.
|
|
1534
|
+
* @typeParam D - The type of the default value, which must be assignable to `T`.
|
|
1535
|
+
* @typeParam S - The input combinator schema, inferred from `schema`. Its other modifiers are kept.
|
|
1536
|
+
*
|
|
1537
|
+
* @param schema - The base combinator schema.
|
|
1538
|
+
* @param defaultValue - The default value, a value of the schema's parsed type.
|
|
1539
|
+
* @returns A new schema with the default value set.
|
|
1540
|
+
*
|
|
1541
|
+
* @example
|
|
1542
|
+
* ```ts
|
|
1543
|
+
* const args = {
|
|
1544
|
+
* port: withDefault(positional(integer()), 8080)
|
|
1545
|
+
* }
|
|
1546
|
+
* ```
|
|
1547
|
+
*/
|
|
1548
|
+
declare function withDefault<T extends string | boolean | number, D extends T = T, S extends CombinatorSchema<T> = CombinatorSchema<T>>(schema: S & Combinator<T>, defaultValue: D): Modified<S, CombinatorWithDefault<T>>;
|
|
1549
|
+
/**
|
|
1550
|
+
* Set a default value on a union of combinator schemas of different types, such as
|
|
1551
|
+
* `strict ? integer() : string()`.
|
|
1552
|
+
*
|
|
1553
|
+
* The original schema is not modified. The default must be a value of one of the types that the
|
|
1554
|
+
* schemas of the union parse to: a number or a string for `strict ? integer() : string()`. They
|
|
1555
|
+
* must parse to a string, number or boolean, since the default can only be one of them and does
|
|
1556
|
+
* not go through `parse`. A schema typed as `any` matches the first overload instead.
|
|
1557
|
+
* Other modifiers on `schema` (for example {@link multiple}) are kept. The default of a `multiple`
|
|
1558
|
+
* schema is one value, which becomes the only element of the array.
|
|
1559
|
+
* Since the default is used for whichever schema of the union is in use, the `parse` of each schema
|
|
1560
|
+
* is typed as returning a value of any of the types that the schemas parse to:
|
|
1561
|
+
* `withDefault(strict ? multiple(integer()) : string(), 'none')` resolves to
|
|
1562
|
+
* `(number | string)[] | number | string`.
|
|
1563
|
+
*
|
|
1564
|
+
* @typeParam S - The input combinator schema, inferred from `schema`: a union of schemas of
|
|
1565
|
+
* different types. Its other modifiers are kept.
|
|
849
1566
|
*
|
|
850
|
-
* @
|
|
1567
|
+
* @param schema - The base combinator schema.
|
|
1568
|
+
* @param defaultValue - The default value, a value of one of the types that the schemas of the
|
|
1569
|
+
* union parse to.
|
|
1570
|
+
* @returns A new schema with the default value set.
|
|
1571
|
+
*
|
|
1572
|
+
* @example
|
|
1573
|
+
* ```ts
|
|
1574
|
+
* const strict = process.argv.includes('--strict')
|
|
1575
|
+
* const args = {
|
|
1576
|
+
* timeout: withDefault(strict ? integer({ min: 0 }) : string(), 'none')
|
|
1577
|
+
* }
|
|
1578
|
+
* // typeof values.timeout === number | string
|
|
1579
|
+
* ```
|
|
1580
|
+
*/
|
|
1581
|
+
declare function withDefault<S extends CombinatorSchema<string | boolean | number>>(schema: S, defaultValue: unknown extends ParsedType<S> ? never : ParsedType<S>): Modified<S, CombinatorWithDefault<ParsedType<S>> & Combinator<ParsedType<S>>>;
|
|
1582
|
+
/**
|
|
1583
|
+
* Overlay the properties of `F` onto a combinator schema without dropping its other properties.
|
|
1584
|
+
*
|
|
1585
|
+
* Drops the keys of `F` from `S` first, so that what `F` sets replaces what `S` has, instead of
|
|
1586
|
+
* making an intersection with it (`'A' & 'B'`, that is `never`, for a description set twice).
|
|
1587
|
+
* The properties of `S` stay visible when `S` is a type parameter, also through other modifiers,
|
|
1588
|
+
* and each schema of a union gets `F` on its own.
|
|
1589
|
+
* A schema typed as `any`, such as one from untyped code, is taken as
|
|
1590
|
+
* {@link UntypedCombinatorSchema}, so that the result is still an argument schema, and still fits
|
|
1591
|
+
* any {@link CombinatorSchema}, unless `F` sets `parse`, as {@link map} does.
|
|
851
1592
|
*/
|
|
852
|
-
|
|
1593
|
+
type WithFlag<S, F> = Without<S, keyof F> & F & UntypedFlag<S, F>;
|
|
853
1594
|
/**
|
|
854
|
-
*
|
|
1595
|
+
* The schema that {@link short}, {@link describe}, {@link required}, {@link multiple}, {@link map}
|
|
1596
|
+
* and {@link withDefault} return: {@link WithFlag} for each schema of a union.
|
|
1597
|
+
*
|
|
1598
|
+
* For a schema typed by a type parameter, TypeScript keeps this type unresolved, and relates it as
|
|
1599
|
+
* {@link WithFlag} of the constraint of the type parameter, for each schema of a union constraint:
|
|
1600
|
+
* the result fits where the modifier on the constraint fits. {@link WithFlag} alone copies each
|
|
1601
|
+
* property `P` of the type parameter as `S[P]`, which TypeScript reads through the constraint:
|
|
1602
|
+
* `S['description']` as `string | undefined`, which `description?: string` does not take with
|
|
1603
|
+
* `exactOptionalPropertyTypes`, and, for a union constraint, `S['parse']` as the union of the
|
|
1604
|
+
* `parse` of its schemas, which fits none of them.
|
|
1605
|
+
*
|
|
1606
|
+
* A property read on this type is typed by the constraint as well: for a schema typed by a type
|
|
1607
|
+
* parameter `S`, `short(schema, 'x').parse` is the `parse` of the constraint, while `schema.parse`
|
|
1608
|
+
* keeps the type `S['parse']`.
|
|
1609
|
+
*/
|
|
1610
|
+
type Modified<S, F> = S extends unknown ? WithFlag<S, F> : never;
|
|
1611
|
+
/**
|
|
1612
|
+
* The properties of `S` without the keys `K` and without index signatures: the same properties as
|
|
1613
|
+
* `Omit<S, K>` for a combinator schema that is not a union, and `{}` for `any`.
|
|
855
1614
|
*
|
|
856
|
-
*
|
|
857
|
-
*
|
|
1615
|
+
* It maps the properties of `S` itself, so that TypeScript still sees them when `S` is a type
|
|
1616
|
+
* parameter with other modifiers on it, and it maps each schema of a union on its own.
|
|
1617
|
+
* For a type parameter, TypeScript does not take it for `Omit<S, K>`, so {@link hidden} and
|
|
1618
|
+
* {@link unrequired}, whose results were typed `Omit<S, K>` with their flag, return that type too.
|
|
1619
|
+
*/
|
|
1620
|
+
type Without<S, K> = { [P in keyof S as P extends K ? never : NamedKey<P>]: S[P]; };
|
|
1621
|
+
/**
|
|
1622
|
+
* `K` for a property name, and `never` for the `string`, `number` or `symbol` key of an index
|
|
1623
|
+
* signature, such as those of `any`.
|
|
858
1624
|
*/
|
|
859
|
-
type
|
|
1625
|
+
type NamedKey<K> = string extends K ? never : number extends K ? never : symbol extends K ? never : K;
|
|
1626
|
+
/**
|
|
1627
|
+
* {@link UntypedCombinatorSchema} without the keys of `F` for a schema typed as `any`, and
|
|
1628
|
+
* `unknown`, which adds nothing, for any other schema. For a schema typed by a type parameter,
|
|
1629
|
+
* TypeScript leaves it unresolved, and the properties come from {@link Without}.
|
|
1630
|
+
*/
|
|
1631
|
+
type UntypedFlag<S, F> = 0 extends 1 & S ? Omit<UntypedCombinatorSchema, keyof F> : unknown;
|
|
860
1632
|
/**
|
|
861
1633
|
* Options for the {@link multiple} combinator.
|
|
862
1634
|
*/
|
|
@@ -880,10 +1652,8 @@ type CombinatorMultiple = {
|
|
|
880
1652
|
* }
|
|
881
1653
|
* // typeof values.tags === string[]
|
|
882
1654
|
* ```
|
|
883
|
-
*
|
|
884
|
-
* @experimental
|
|
885
1655
|
*/
|
|
886
|
-
declare function multiple<S extends CombinatorSchema<unknown>>(schema: S):
|
|
1656
|
+
declare function multiple<S extends CombinatorSchema<unknown>>(schema: S): Modified<S, CombinatorMultiple>;
|
|
887
1657
|
/**
|
|
888
1658
|
* Options for the {@link required} combinator.
|
|
889
1659
|
*/
|
|
@@ -907,10 +1677,8 @@ type CombinatorRequired = {
|
|
|
907
1677
|
* name: required(string())
|
|
908
1678
|
* }
|
|
909
1679
|
* ```
|
|
910
|
-
*
|
|
911
|
-
* @experimental
|
|
912
1680
|
*/
|
|
913
|
-
declare function required<S extends CombinatorSchema<unknown>>(schema: S):
|
|
1681
|
+
declare function required<S extends CombinatorSchema<unknown>>(schema: S): Modified<S, CombinatorRequired>;
|
|
914
1682
|
/**
|
|
915
1683
|
* Options for the {@link short} combinator.
|
|
916
1684
|
*/
|
|
@@ -921,9 +1689,14 @@ type CombinatorShort<S extends string> = {
|
|
|
921
1689
|
* Set a short alias on a combinator schema.
|
|
922
1690
|
*
|
|
923
1691
|
* The original schema is not modified.
|
|
1692
|
+
* Other modifiers on `schema` (for example {@link multiple}) are kept.
|
|
924
1693
|
*
|
|
925
|
-
* @typeParam T - The schema's parsed type.
|
|
926
|
-
*
|
|
1694
|
+
* @typeParam T - The schema's parsed type, when type arguments are given explicitly. It is not
|
|
1695
|
+
* inferred, so that `schema` can be a union of schemas of different types.
|
|
1696
|
+
* @typeParam A - The short alias string literal type.
|
|
1697
|
+
* @typeParam S - The input combinator schema, inferred from `schema`. Its other modifiers are kept.
|
|
1698
|
+
* If type arguments are given explicitly without `S`, `S` is `CombinatorSchema<T>`, and the type
|
|
1699
|
+
* of the result lacks the other modifiers, although the returned object has them.
|
|
927
1700
|
*
|
|
928
1701
|
* @param schema - The base combinator schema.
|
|
929
1702
|
* @param alias - Single character short alias.
|
|
@@ -936,10 +1709,8 @@ type CombinatorShort<S extends string> = {
|
|
|
936
1709
|
* }
|
|
937
1710
|
* // Usage: -v or --verbose
|
|
938
1711
|
* ```
|
|
939
|
-
*
|
|
940
|
-
* @experimental
|
|
941
1712
|
*/
|
|
942
|
-
declare function short<T, S extends
|
|
1713
|
+
declare function short<T, A extends string, S extends CombinatorSchema<T> = CombinatorSchema<T>>(schema: S, alias: A): Modified<S, CombinatorShort<A>>;
|
|
943
1714
|
/**
|
|
944
1715
|
* Options for the {@link describe} combinator.
|
|
945
1716
|
*/
|
|
@@ -950,9 +1721,14 @@ type CombinatorDescribe<D extends string> = {
|
|
|
950
1721
|
* Set a description on a combinator schema for help text generation.
|
|
951
1722
|
*
|
|
952
1723
|
* The original schema is not modified.
|
|
1724
|
+
* Other modifiers on `schema` (for example {@link required}) are kept.
|
|
953
1725
|
*
|
|
954
|
-
* @typeParam T - The schema's parsed type.
|
|
1726
|
+
* @typeParam T - The schema's parsed type, when type arguments are given explicitly. It is not
|
|
1727
|
+
* inferred, so that `schema` can be a union of schemas of different types.
|
|
955
1728
|
* @typeParam D - The description string literal type.
|
|
1729
|
+
* @typeParam S - The input combinator schema, inferred from `schema`. Its other modifiers are kept.
|
|
1730
|
+
* If type arguments are given explicitly without `S`, `S` is `CombinatorSchema<T>`, and the type
|
|
1731
|
+
* of the result lacks the other modifiers, although the returned object has them.
|
|
956
1732
|
*
|
|
957
1733
|
* @param schema - The base combinator schema.
|
|
958
1734
|
* @param text - Human-readable description.
|
|
@@ -964,10 +1740,8 @@ type CombinatorDescribe<D extends string> = {
|
|
|
964
1740
|
* port: describe(integer(), 'Port number to listen on')
|
|
965
1741
|
* }
|
|
966
1742
|
* ```
|
|
967
|
-
*
|
|
968
|
-
* @experimental
|
|
969
1743
|
*/
|
|
970
|
-
declare function describe<T, D extends string>
|
|
1744
|
+
declare function describe<T, D extends string, S extends CombinatorSchema<T> = CombinatorSchema<T>>(schema: S, text: D): Modified<S, CombinatorDescribe<D>>;
|
|
971
1745
|
/**
|
|
972
1746
|
* Options for the {@link hidden} combinator.
|
|
973
1747
|
*/
|
|
@@ -979,6 +1753,7 @@ type CombinatorHidden = {
|
|
|
979
1753
|
*
|
|
980
1754
|
* The original schema is not modified. This only marks renderer metadata and
|
|
981
1755
|
* does not change parsing, validation, defaults, conflicts, or resolved values.
|
|
1756
|
+
* For a union of schemas, such as `strict ? integer() : string()`, each schema gets `hidden: true`.
|
|
982
1757
|
*
|
|
983
1758
|
* @typeParam T - The schema type.
|
|
984
1759
|
*
|
|
@@ -991,10 +1766,8 @@ type CombinatorHidden = {
|
|
|
991
1766
|
* legacy: hidden(string())
|
|
992
1767
|
* }
|
|
993
1768
|
* ```
|
|
994
|
-
*
|
|
995
|
-
* @experimental
|
|
996
1769
|
*/
|
|
997
|
-
declare function hidden<T extends ArgSchema>(schema: T): Omit<T, 'hidden'>
|
|
1770
|
+
declare function hidden<T extends ArgSchema>(schema: T): T extends unknown ? WithFlag<T, CombinatorHidden> & Omit<T, 'hidden'> : never;
|
|
998
1771
|
/**
|
|
999
1772
|
* Options for the {@link unrequired} combinator.
|
|
1000
1773
|
*/
|
|
@@ -1007,6 +1780,8 @@ type CombinatorUnrequired = {
|
|
|
1007
1780
|
* Useful for overriding a base combinator that was created with `required: true`,
|
|
1008
1781
|
* or for making a positional argument explicitly optional.
|
|
1009
1782
|
* The original schema is not modified.
|
|
1783
|
+
* For a union of schemas, such as `strict ? integer() : string()`, each schema gets
|
|
1784
|
+
* `required: false`.
|
|
1010
1785
|
*
|
|
1011
1786
|
* @typeParam T - The schema type.
|
|
1012
1787
|
*
|
|
@@ -1020,10 +1795,8 @@ type CombinatorUnrequired = {
|
|
|
1020
1795
|
* query: unrequired(positional())
|
|
1021
1796
|
* }
|
|
1022
1797
|
* ```
|
|
1023
|
-
*
|
|
1024
|
-
* @experimental
|
|
1025
1798
|
*/
|
|
1026
|
-
declare function unrequired<T extends ArgSchema>(schema: T): Omit<T, 'required'>
|
|
1799
|
+
declare function unrequired<T extends ArgSchema>(schema: T): T extends unknown ? WithFlag<T, CombinatorUnrequired> & Omit<T, 'required'> : never;
|
|
1027
1800
|
/**
|
|
1028
1801
|
* Recursively merge a tuple of {@link Args} types.
|
|
1029
1802
|
* Later types override earlier ones on key conflicts.
|
|
@@ -1049,8 +1822,6 @@ type MergeArgs<T extends Args[]> = T extends [infer Only extends Args] ? Only :
|
|
|
1049
1822
|
* help: short(boolean(), 'h')
|
|
1050
1823
|
* })
|
|
1051
1824
|
* ```
|
|
1052
|
-
*
|
|
1053
|
-
* @experimental
|
|
1054
1825
|
*/
|
|
1055
1826
|
declare function args<T extends Args>(fields: T): T;
|
|
1056
1827
|
/**
|
|
@@ -1071,8 +1842,6 @@ declare function args<T extends Args>(fields: T): T;
|
|
|
1071
1842
|
* const network = args({ host: required(string()), port: withDefault(integer(), 8080) })
|
|
1072
1843
|
* const schema = merge(common, network)
|
|
1073
1844
|
* ```
|
|
1074
|
-
*
|
|
1075
|
-
* @experimental
|
|
1076
1845
|
*/
|
|
1077
1846
|
declare function merge<A extends Args, B extends Args>(a: A, b: B): Omit<A, keyof B> & B;
|
|
1078
1847
|
/**
|
|
@@ -1082,8 +1851,6 @@ declare function merge<A extends Args, B extends Args>(a: A, b: B): Omit<A, keyo
|
|
|
1082
1851
|
* @param b - Second schema.
|
|
1083
1852
|
* @param c - Third schema.
|
|
1084
1853
|
* @returns A merged schema containing all fields.
|
|
1085
|
-
*
|
|
1086
|
-
* @experimental
|
|
1087
1854
|
*/
|
|
1088
1855
|
declare function merge<A extends Args, B extends Args, C extends Args>(a: A, b: B, c: C): Omit<Omit<A, keyof B | keyof C> & Omit<B, keyof C>, never> & C;
|
|
1089
1856
|
/**
|
|
@@ -1094,8 +1861,6 @@ declare function merge<A extends Args, B extends Args, C extends Args>(a: A, b:
|
|
|
1094
1861
|
* @param c - Third schema.
|
|
1095
1862
|
* @param d - Fourth schema.
|
|
1096
1863
|
* @returns A merged schema containing all fields.
|
|
1097
|
-
*
|
|
1098
|
-
* @experimental
|
|
1099
1864
|
*/
|
|
1100
1865
|
declare function merge<A extends Args, B extends Args, C extends Args, D extends Args>(a: A, b: B, c: C, d: D): MergeArgs<[A, B, C, D]>;
|
|
1101
1866
|
/**
|
|
@@ -1103,8 +1868,6 @@ declare function merge<A extends Args, B extends Args, C extends Args, D extends
|
|
|
1103
1868
|
*
|
|
1104
1869
|
* @param schemas - The schemas to merge.
|
|
1105
1870
|
* @returns A merged schema containing all fields.
|
|
1106
|
-
*
|
|
1107
|
-
* @experimental
|
|
1108
1871
|
*/
|
|
1109
1872
|
declare function merge<T extends Args[]>(...schemas: T): MergeArgs<T>;
|
|
1110
1873
|
/**
|
|
@@ -1125,16 +1888,8 @@ declare function merge<T extends Args[]>(...schemas: T): MergeArgs<T>;
|
|
|
1125
1888
|
* const base = args({ port: withDefault(integer(), 8080) })
|
|
1126
1889
|
* const strict = extend(base, { port: required(integer({ min: 1, max: 65535 })) })
|
|
1127
1890
|
* ```
|
|
1128
|
-
*
|
|
1129
|
-
* @experimental
|
|
1130
1891
|
*/
|
|
1131
1892
|
declare function extend<T extends Args, U extends Args>(base: T, overrides: U): Omit<T, keyof U> & U;
|
|
1132
1893
|
//#endregion
|
|
1133
|
-
//#region src/index.d.ts
|
|
1134
|
-
/**
|
|
1135
|
-
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
1136
|
-
* @license MIT
|
|
1137
|
-
*/
|
|
1138
|
-
|
|
1139
1894
|
//#endregion
|
|
1140
|
-
export { BaseOptions, BooleanOptions, Combinator, CombinatorOptions, CombinatorSchema, FloatOptions, IntegerOptions, NumberOptions, StringOptions, args, boolean, choice, combinator, describe, extend, float, hidden, integer, map, merge, multiple, number, positional, required, short, string, unrequired, withDefault };
|
|
1895
|
+
export { type BaseOptions, type BooleanOptions, type Combinator, type CombinatorOptions, type CombinatorSchema, type FloatOptions, type IntegerOptions, type NumberOptions, type StringOptions, args, boolean, choice, combinator, describe, extend, float, hidden, integer, map, merge, multiple, number, positional, required, short, string, unrequired, withDefault };
|