@gunshi/combinators 0.37.2 → 1.0.0-beta.0
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 +864 -116
- package/lib/index.js +88 -334
- package/package.json +2 -2
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
|
|
@@ -451,13 +550,6 @@ interface ArgSchema {
|
|
|
451
550
|
*/
|
|
452
551
|
parse?: (value: string) => any;
|
|
453
552
|
}
|
|
454
|
-
/**
|
|
455
|
-
* Machine-readable error codes for {@link ArgsValidationError}.
|
|
456
|
-
*
|
|
457
|
-
* Each code identifies a validation failure category and is also suitable as an
|
|
458
|
-
* i18n resource key for localized rendering.
|
|
459
|
-
*/
|
|
460
|
-
|
|
461
553
|
/**
|
|
462
554
|
* An object that contains {@link ArgSchema | argument schema}.
|
|
463
555
|
*
|
|
@@ -466,20 +558,13 @@ interface ArgSchema {
|
|
|
466
558
|
interface Args {
|
|
467
559
|
[option: string]: ArgSchema;
|
|
468
560
|
}
|
|
469
|
-
/**
|
|
470
|
-
* An object that contains the values of the arguments.
|
|
471
|
-
*
|
|
472
|
-
* @typeParam T - {@link Args | Arguments} which is an object that defines the command line arguments.
|
|
473
|
-
*/
|
|
474
561
|
//#endregion
|
|
475
|
-
//#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
|
|
476
563
|
//#region src/combinators.d.ts
|
|
477
564
|
/**
|
|
478
565
|
* A combinator produced by combinator factory functions.
|
|
479
566
|
*
|
|
480
567
|
* @typeParam T - The parsed value type.
|
|
481
|
-
*
|
|
482
|
-
* @experimental
|
|
483
568
|
*/
|
|
484
569
|
type Combinator<T> = {
|
|
485
570
|
/**
|
|
@@ -492,17 +577,30 @@ type Combinator<T> = {
|
|
|
492
577
|
};
|
|
493
578
|
/**
|
|
494
579
|
* A schema produced by combinator factory functions.
|
|
495
|
-
* 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>`.
|
|
496
583
|
*
|
|
497
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.
|
|
498
592
|
*
|
|
499
|
-
*
|
|
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.
|
|
500
595
|
*/
|
|
501
|
-
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;
|
|
502
602
|
/**
|
|
503
603
|
* Common options shared by all base combinators.
|
|
504
|
-
*
|
|
505
|
-
* @experimental
|
|
506
604
|
*/
|
|
507
605
|
interface BaseOptions {
|
|
508
606
|
/**
|
|
@@ -519,13 +617,47 @@ interface BaseOptions {
|
|
|
519
617
|
short?: string;
|
|
520
618
|
/**
|
|
521
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.
|
|
522
637
|
*/
|
|
523
638
|
required?: boolean;
|
|
524
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;
|
|
525
659
|
/**
|
|
526
660
|
* Options for the {@link string} combinator.
|
|
527
|
-
*
|
|
528
|
-
* @experimental
|
|
529
661
|
*/
|
|
530
662
|
interface StringOptions extends BaseOptions {
|
|
531
663
|
/**
|
|
@@ -544,6 +676,8 @@ interface StringOptions extends BaseOptions {
|
|
|
544
676
|
/**
|
|
545
677
|
* Create a string argument schema with optional validation.
|
|
546
678
|
*
|
|
679
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps.
|
|
680
|
+
*
|
|
547
681
|
* @param opts - Validation options.
|
|
548
682
|
* @returns A combinator schema that resolves to string.
|
|
549
683
|
*
|
|
@@ -553,14 +687,46 @@ interface StringOptions extends BaseOptions {
|
|
|
553
687
|
* name: string({ minLength: 1, maxLength: 50 })
|
|
554
688
|
* }
|
|
555
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
|
+
*
|
|
701
|
+
* @param opts - Validation options.
|
|
702
|
+
* @returns A combinator schema that resolves to string.
|
|
703
|
+
*
|
|
704
|
+
* @example
|
|
705
|
+
* ```ts
|
|
706
|
+
* const args = {
|
|
707
|
+
* name: string({ minLength: 1, maxLength: 50 })
|
|
708
|
+
* }
|
|
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.
|
|
716
|
+
*
|
|
717
|
+
* @param opts - Validation options.
|
|
718
|
+
* @returns A combinator schema that resolves to string.
|
|
556
719
|
*
|
|
557
|
-
* @
|
|
720
|
+
* @example
|
|
721
|
+
* ```ts
|
|
722
|
+
* const args = {
|
|
723
|
+
* name: string({ minLength: 1, maxLength: 50 })
|
|
724
|
+
* }
|
|
725
|
+
* ```
|
|
558
726
|
*/
|
|
559
727
|
declare function string(opts?: StringOptions): CombinatorSchema<string>;
|
|
560
728
|
/**
|
|
561
729
|
* Options for the {@link number} combinator.
|
|
562
|
-
*
|
|
563
|
-
* @experimental
|
|
564
730
|
*/
|
|
565
731
|
interface NumberOptions extends BaseOptions {
|
|
566
732
|
/**
|
|
@@ -577,6 +743,8 @@ interface NumberOptions extends BaseOptions {
|
|
|
577
743
|
*
|
|
578
744
|
* Accepts any numeric value (integer or float).
|
|
579
745
|
*
|
|
746
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps.
|
|
747
|
+
*
|
|
580
748
|
* @param opts - Range options.
|
|
581
749
|
* @returns A combinator schema that resolves to number.
|
|
582
750
|
*
|
|
@@ -586,14 +754,50 @@ interface NumberOptions extends BaseOptions {
|
|
|
586
754
|
* timeout: number({ min: 0, max: 30000 })
|
|
587
755
|
* }
|
|
588
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.
|
|
763
|
+
*
|
|
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.
|
|
589
790
|
*
|
|
590
|
-
* @
|
|
791
|
+
* @example
|
|
792
|
+
* ```ts
|
|
793
|
+
* const args = {
|
|
794
|
+
* timeout: number({ min: 0, max: 30000 })
|
|
795
|
+
* }
|
|
796
|
+
* ```
|
|
591
797
|
*/
|
|
592
798
|
declare function number(opts?: NumberOptions): CombinatorSchema<number>;
|
|
593
799
|
/**
|
|
594
800
|
* Options for the {@link integer} combinator.
|
|
595
|
-
*
|
|
596
|
-
* @experimental
|
|
597
801
|
*/
|
|
598
802
|
interface IntegerOptions extends BaseOptions {
|
|
599
803
|
/**
|
|
@@ -610,6 +814,8 @@ interface IntegerOptions extends BaseOptions {
|
|
|
610
814
|
*
|
|
611
815
|
* Only accepts integer values (no decimals).
|
|
612
816
|
*
|
|
817
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps.
|
|
818
|
+
*
|
|
613
819
|
* @param opts - Range options.
|
|
614
820
|
* @returns A combinator schema that resolves to number (integer).
|
|
615
821
|
*
|
|
@@ -619,14 +825,50 @@ interface IntegerOptions extends BaseOptions {
|
|
|
619
825
|
* retries: integer({ min: 0, max: 10 })
|
|
620
826
|
* }
|
|
621
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.
|
|
622
840
|
*
|
|
623
|
-
* @
|
|
841
|
+
* @param opts - Range options.
|
|
842
|
+
* @returns A combinator schema that resolves to number (integer).
|
|
843
|
+
*
|
|
844
|
+
* @example
|
|
845
|
+
* ```ts
|
|
846
|
+
* const args = {
|
|
847
|
+
* retries: integer({ min: 0, max: 10 })
|
|
848
|
+
* }
|
|
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).
|
|
861
|
+
*
|
|
862
|
+
* @example
|
|
863
|
+
* ```ts
|
|
864
|
+
* const args = {
|
|
865
|
+
* retries: integer({ min: 0, max: 10 })
|
|
866
|
+
* }
|
|
867
|
+
* ```
|
|
624
868
|
*/
|
|
625
869
|
declare function integer(opts?: IntegerOptions): CombinatorSchema<number>;
|
|
626
870
|
/**
|
|
627
871
|
* Options for the {@link float} combinator.
|
|
628
|
-
*
|
|
629
|
-
* @experimental
|
|
630
872
|
*/
|
|
631
873
|
interface FloatOptions extends BaseOptions {
|
|
632
874
|
/**
|
|
@@ -643,6 +885,30 @@ interface FloatOptions extends BaseOptions {
|
|
|
643
885
|
*
|
|
644
886
|
* Rejects `NaN` and `Infinity` values.
|
|
645
887
|
*
|
|
888
|
+
* @typeParam R - The type of `required` in the options, which the schema keeps.
|
|
889
|
+
*
|
|
890
|
+
* @param opts - Range options.
|
|
891
|
+
* @returns A combinator schema that resolves to number (float).
|
|
892
|
+
*
|
|
893
|
+
* @example
|
|
894
|
+
* ```ts
|
|
895
|
+
* const args = {
|
|
896
|
+
* ratio: float({ min: 0, max: 1 })
|
|
897
|
+
* }
|
|
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.
|
|
911
|
+
*
|
|
646
912
|
* @param opts - Range options.
|
|
647
913
|
* @returns A combinator schema that resolves to number (float).
|
|
648
914
|
*
|
|
@@ -652,14 +918,28 @@ interface FloatOptions extends BaseOptions {
|
|
|
652
918
|
* ratio: float({ min: 0, max: 1 })
|
|
653
919
|
* }
|
|
654
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.
|
|
655
929
|
*
|
|
656
|
-
* @
|
|
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
|
+
* ```
|
|
657
939
|
*/
|
|
658
940
|
declare function float(opts?: FloatOptions): CombinatorSchema<number>;
|
|
659
941
|
/**
|
|
660
942
|
* Options for the {@link boolean} combinator.
|
|
661
|
-
*
|
|
662
|
-
* @experimental
|
|
663
943
|
*/
|
|
664
944
|
interface BooleanOptions extends BaseOptions {
|
|
665
945
|
/**
|
|
@@ -671,7 +951,10 @@ interface BooleanOptions extends BaseOptions {
|
|
|
671
951
|
* Create a boolean argument schema.
|
|
672
952
|
*
|
|
673
953
|
* Boolean arguments are existence-based. The resolver passes `"true"` or `"false"`
|
|
674
|
-
* 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.
|
|
675
958
|
*
|
|
676
959
|
* @param opts - Boolean options.
|
|
677
960
|
* @returns A combinator schema for boolean flags.
|
|
@@ -683,8 +966,52 @@ interface BooleanOptions extends BaseOptions {
|
|
|
683
966
|
* }
|
|
684
967
|
* // Usage: --color (true), --no-color (false)
|
|
685
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.
|
|
983
|
+
*
|
|
984
|
+
* @param opts - Boolean options.
|
|
985
|
+
* @returns A combinator schema for boolean flags.
|
|
686
986
|
*
|
|
687
|
-
* @
|
|
987
|
+
* @example
|
|
988
|
+
* ```ts
|
|
989
|
+
* const args = {
|
|
990
|
+
* color: boolean({ negatable: true })
|
|
991
|
+
* }
|
|
992
|
+
* // Usage: --color (true), --no-color (false)
|
|
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.
|
|
1000
|
+
*
|
|
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
|
+
* ```
|
|
688
1015
|
*/
|
|
689
1016
|
declare function boolean(opts?: BooleanOptions): CombinatorSchema<boolean>;
|
|
690
1017
|
/**
|
|
@@ -693,13 +1020,46 @@ declare function boolean(opts?: BooleanOptions): CombinatorSchema<boolean>;
|
|
|
693
1020
|
type ArgSchemaPositionalType = {
|
|
694
1021
|
type: 'positional';
|
|
695
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;
|
|
696
1043
|
/**
|
|
697
1044
|
* Create a positional argument schema.
|
|
698
1045
|
*
|
|
699
1046
|
* Without a parser, resolves to string.
|
|
700
1047
|
* With a parser (e.g., `positional(integer())`), resolves to the parser's return type.
|
|
701
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
|
+
*
|
|
702
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.
|
|
703
1063
|
*
|
|
704
1064
|
* @param parser - The parser combinator schema.
|
|
705
1065
|
* @returns A positional argument schema resolving to the parser's type.
|
|
@@ -712,17 +1072,112 @@ type ArgSchemaPositionalType = {
|
|
|
712
1072
|
* query: unrequired(positional()) // optional positional
|
|
713
1073
|
* }
|
|
714
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.
|
|
1094
|
+
*
|
|
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
|
+
* ```
|
|
1103
|
+
*/
|
|
1104
|
+
declare function positional<S extends CombinatorSchema<unknown>>(parser: S): PositionalOf<S>;
|
|
1105
|
+
/**
|
|
1106
|
+
* Create a positional argument schema.
|
|
1107
|
+
*
|
|
1108
|
+
* Without a parser, resolves to string.
|
|
1109
|
+
* With a parser (e.g., `positional(integer())`), resolves to the parser's return type.
|
|
1110
|
+
*
|
|
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.
|
|
1122
|
+
* @returns A positional argument schema resolving to string.
|
|
1123
|
+
*
|
|
1124
|
+
* @example
|
|
1125
|
+
* ```ts
|
|
1126
|
+
* const args = {
|
|
1127
|
+
* command: positional(), // resolves to string
|
|
1128
|
+
* port: positional(integer()), // resolves to number
|
|
1129
|
+
* query: unrequired(positional()) // optional positional
|
|
1130
|
+
* }
|
|
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.
|
|
1138
|
+
*
|
|
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 } : {}`.
|
|
715
1152
|
*
|
|
716
|
-
* @
|
|
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
|
+
* ```
|
|
717
1165
|
*/
|
|
718
|
-
declare function positional<
|
|
1166
|
+
declare function positional<const R extends boolean | undefined = boolean | undefined>(parser?: BaseOptions & {
|
|
1167
|
+
required?: R;
|
|
1168
|
+
}): WithUnrequiredOption<Omit<CombinatorSchema<string>, 'type'> & ArgSchemaPositionalType, R>;
|
|
719
1169
|
/**
|
|
720
1170
|
* Create a positional argument schema.
|
|
721
1171
|
*
|
|
722
1172
|
* Without a parser, resolves to string.
|
|
723
1173
|
* With a parser (e.g., `positional(integer())`), resolves to the parser's return type.
|
|
724
1174
|
*
|
|
725
|
-
*
|
|
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.
|
|
726
1181
|
* @returns A positional argument schema resolving to string.
|
|
727
1182
|
*
|
|
728
1183
|
* @example
|
|
@@ -733,19 +1188,43 @@ declare function positional<T>(parser: CombinatorSchema<T>): CombinatorSchema<T>
|
|
|
733
1188
|
* query: unrequired(positional()) // optional positional
|
|
734
1189
|
* }
|
|
735
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.
|
|
736
1195
|
*
|
|
737
|
-
*
|
|
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
|
+
* ```
|
|
738
1212
|
*/
|
|
739
|
-
declare function
|
|
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>;
|
|
740
1216
|
/**
|
|
741
1217
|
* Create an enum-like argument schema with literal type inference.
|
|
742
1218
|
*
|
|
743
1219
|
* Uses `const T` generic to infer literal union types from the values array.
|
|
744
1220
|
*
|
|
745
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.
|
|
746
1225
|
*
|
|
747
1226
|
* @param values - Allowed values.
|
|
748
|
-
* @param opts - Common options (description, short, required).
|
|
1227
|
+
* @param opts - Common options (description, short, hidden, required).
|
|
749
1228
|
* @returns A combinator schema that resolves to a union of the allowed values.
|
|
750
1229
|
*
|
|
751
1230
|
* @example
|
|
@@ -755,21 +1234,42 @@ declare function positional(parser?: BaseOptions): ArgSchema & ArgSchemaPosition
|
|
|
755
1234
|
* }
|
|
756
1235
|
* // typeof values.level === 'debug' | 'info' | 'warn' | 'error'
|
|
757
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.
|
|
758
1251
|
*
|
|
759
|
-
* @
|
|
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
|
+
* ```
|
|
760
1259
|
*/
|
|
761
1260
|
declare function choice<const T extends readonly string[]>(values: T, opts?: BaseOptions): CombinatorSchema<T[number]>;
|
|
762
1261
|
/**
|
|
763
1262
|
* Options for the {@link combinator} factory function.
|
|
764
1263
|
*
|
|
765
1264
|
* @typeParam T - The parsed value type.
|
|
766
|
-
*
|
|
767
|
-
* @experimental
|
|
768
1265
|
*/
|
|
769
1266
|
interface CombinatorOptions<T> extends BaseOptions {
|
|
770
1267
|
/**
|
|
771
1268
|
* The parse function that converts a string to the desired type.
|
|
772
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
|
+
*
|
|
773
1273
|
* @param value - The input string value.
|
|
774
1274
|
* @returns The parsed value of type T.
|
|
775
1275
|
*/
|
|
@@ -791,6 +1291,7 @@ interface CombinatorOptions<T> extends BaseOptions {
|
|
|
791
1291
|
* The returned schema has `type: 'custom'`.
|
|
792
1292
|
*
|
|
793
1293
|
* @typeParam T - The parsed value type.
|
|
1294
|
+
* @typeParam R - The type of `required` in the configuration, which the schema keeps.
|
|
794
1295
|
*
|
|
795
1296
|
* @param config - Configuration with a parse function and optional metavar.
|
|
796
1297
|
* @returns A combinator schema that resolves to the parse function's return type.
|
|
@@ -808,8 +1309,71 @@ interface CombinatorOptions<T> extends BaseOptions {
|
|
|
808
1309
|
* metavar: 'date'
|
|
809
1310
|
* })
|
|
810
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.
|
|
1318
|
+
*
|
|
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.
|
|
811
1322
|
*
|
|
812
|
-
*
|
|
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
|
+
* ```
|
|
813
1377
|
*/
|
|
814
1378
|
declare function combinator<T>(config: CombinatorOptions<T>): CombinatorSchema<T>;
|
|
815
1379
|
/**
|
|
@@ -817,9 +1381,24 @@ declare function combinator<T>(config: CombinatorOptions<T>): CombinatorSchema<T
|
|
|
817
1381
|
*
|
|
818
1382
|
* Creates a new schema that applies `transform` to the result of `schema.parse`.
|
|
819
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>>`.
|
|
820
1396
|
*
|
|
821
1397
|
* @typeParam T - The input schema's parsed type.
|
|
822
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.
|
|
823
1402
|
*
|
|
824
1403
|
* @param schema - The base combinator schema.
|
|
825
1404
|
* @param transform - The transformation function.
|
|
@@ -831,10 +1410,72 @@ declare function combinator<T>(config: CombinatorOptions<T>): CombinatorSchema<T
|
|
|
831
1410
|
* doubled: map(integer(), n => n * 2)
|
|
832
1411
|
* }
|
|
833
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.
|
|
1429
|
+
*
|
|
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.
|
|
834
1437
|
*
|
|
835
|
-
* @
|
|
1438
|
+
* @example
|
|
1439
|
+
* ```ts
|
|
1440
|
+
* const args = {
|
|
1441
|
+
* doubled: map(positional(integer()), n => n * 2)
|
|
1442
|
+
* }
|
|
1443
|
+
* ```
|
|
836
1444
|
*/
|
|
837
|
-
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>>;
|
|
838
1479
|
/**
|
|
839
1480
|
* Options for the {@link withDefault} combinator.
|
|
840
1481
|
*/
|
|
@@ -844,12 +1485,29 @@ type CombinatorWithDefault<T> = {
|
|
|
844
1485
|
/**
|
|
845
1486
|
* Set a default value on a combinator schema.
|
|
846
1487
|
*
|
|
847
|
-
* 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>`.
|
|
848
1502
|
*
|
|
849
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.
|
|
850
1508
|
*
|
|
851
1509
|
* @param schema - The base combinator schema.
|
|
852
|
-
* @param defaultValue - The default value.
|
|
1510
|
+
* @param defaultValue - The default value, a value of the schema's parsed type.
|
|
853
1511
|
* @returns A new schema with the default value set.
|
|
854
1512
|
*
|
|
855
1513
|
* @example
|
|
@@ -858,17 +1516,119 @@ type CombinatorWithDefault<T> = {
|
|
|
858
1516
|
* port: withDefault(integer({ min: 1, max: 65535 }), 8080)
|
|
859
1517
|
* }
|
|
860
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.
|
|
861
1532
|
*
|
|
862
|
-
* @
|
|
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
|
+
* ```
|
|
863
1547
|
*/
|
|
864
|
-
declare function withDefault<T extends string | boolean | number>(schema:
|
|
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>>;
|
|
865
1549
|
/**
|
|
866
|
-
*
|
|
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.
|
|
867
1566
|
*
|
|
868
|
-
*
|
|
869
|
-
*
|
|
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.
|
|
870
1592
|
*/
|
|
871
|
-
type WithFlag<S, F> =
|
|
1593
|
+
type WithFlag<S, F> = Without<S, keyof F> & F & UntypedFlag<S, F>;
|
|
1594
|
+
/**
|
|
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`.
|
|
1614
|
+
*
|
|
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`.
|
|
1624
|
+
*/
|
|
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;
|
|
872
1632
|
/**
|
|
873
1633
|
* Options for the {@link multiple} combinator.
|
|
874
1634
|
*/
|
|
@@ -892,10 +1652,8 @@ type CombinatorMultiple = {
|
|
|
892
1652
|
* }
|
|
893
1653
|
* // typeof values.tags === string[]
|
|
894
1654
|
* ```
|
|
895
|
-
*
|
|
896
|
-
* @experimental
|
|
897
1655
|
*/
|
|
898
|
-
declare function multiple<S extends CombinatorSchema<unknown>>(schema: S):
|
|
1656
|
+
declare function multiple<S extends CombinatorSchema<unknown>>(schema: S): Modified<S, CombinatorMultiple>;
|
|
899
1657
|
/**
|
|
900
1658
|
* Options for the {@link required} combinator.
|
|
901
1659
|
*/
|
|
@@ -919,10 +1677,8 @@ type CombinatorRequired = {
|
|
|
919
1677
|
* name: required(string())
|
|
920
1678
|
* }
|
|
921
1679
|
* ```
|
|
922
|
-
*
|
|
923
|
-
* @experimental
|
|
924
1680
|
*/
|
|
925
|
-
declare function required<S extends CombinatorSchema<unknown>>(schema: S):
|
|
1681
|
+
declare function required<S extends CombinatorSchema<unknown>>(schema: S): Modified<S, CombinatorRequired>;
|
|
926
1682
|
/**
|
|
927
1683
|
* Options for the {@link short} combinator.
|
|
928
1684
|
*/
|
|
@@ -933,9 +1689,14 @@ type CombinatorShort<S extends string> = {
|
|
|
933
1689
|
* Set a short alias on a combinator schema.
|
|
934
1690
|
*
|
|
935
1691
|
* The original schema is not modified.
|
|
1692
|
+
* Other modifiers on `schema` (for example {@link multiple}) are kept.
|
|
936
1693
|
*
|
|
937
|
-
* @typeParam T - The schema's parsed type.
|
|
938
|
-
*
|
|
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.
|
|
939
1700
|
*
|
|
940
1701
|
* @param schema - The base combinator schema.
|
|
941
1702
|
* @param alias - Single character short alias.
|
|
@@ -948,10 +1709,8 @@ type CombinatorShort<S extends string> = {
|
|
|
948
1709
|
* }
|
|
949
1710
|
* // Usage: -v or --verbose
|
|
950
1711
|
* ```
|
|
951
|
-
*
|
|
952
|
-
* @experimental
|
|
953
1712
|
*/
|
|
954
|
-
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>>;
|
|
955
1714
|
/**
|
|
956
1715
|
* Options for the {@link describe} combinator.
|
|
957
1716
|
*/
|
|
@@ -962,9 +1721,14 @@ type CombinatorDescribe<D extends string> = {
|
|
|
962
1721
|
* Set a description on a combinator schema for help text generation.
|
|
963
1722
|
*
|
|
964
1723
|
* The original schema is not modified.
|
|
1724
|
+
* Other modifiers on `schema` (for example {@link required}) are kept.
|
|
965
1725
|
*
|
|
966
|
-
* @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.
|
|
967
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.
|
|
968
1732
|
*
|
|
969
1733
|
* @param schema - The base combinator schema.
|
|
970
1734
|
* @param text - Human-readable description.
|
|
@@ -976,10 +1740,8 @@ type CombinatorDescribe<D extends string> = {
|
|
|
976
1740
|
* port: describe(integer(), 'Port number to listen on')
|
|
977
1741
|
* }
|
|
978
1742
|
* ```
|
|
979
|
-
*
|
|
980
|
-
* @experimental
|
|
981
1743
|
*/
|
|
982
|
-
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>>;
|
|
983
1745
|
/**
|
|
984
1746
|
* Options for the {@link hidden} combinator.
|
|
985
1747
|
*/
|
|
@@ -991,6 +1753,7 @@ type CombinatorHidden = {
|
|
|
991
1753
|
*
|
|
992
1754
|
* The original schema is not modified. This only marks renderer metadata and
|
|
993
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`.
|
|
994
1757
|
*
|
|
995
1758
|
* @typeParam T - The schema type.
|
|
996
1759
|
*
|
|
@@ -1003,10 +1766,8 @@ type CombinatorHidden = {
|
|
|
1003
1766
|
* legacy: hidden(string())
|
|
1004
1767
|
* }
|
|
1005
1768
|
* ```
|
|
1006
|
-
*
|
|
1007
|
-
* @experimental
|
|
1008
1769
|
*/
|
|
1009
|
-
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;
|
|
1010
1771
|
/**
|
|
1011
1772
|
* Options for the {@link unrequired} combinator.
|
|
1012
1773
|
*/
|
|
@@ -1019,6 +1780,8 @@ type CombinatorUnrequired = {
|
|
|
1019
1780
|
* Useful for overriding a base combinator that was created with `required: true`,
|
|
1020
1781
|
* or for making a positional argument explicitly optional.
|
|
1021
1782
|
* The original schema is not modified.
|
|
1783
|
+
* For a union of schemas, such as `strict ? integer() : string()`, each schema gets
|
|
1784
|
+
* `required: false`.
|
|
1022
1785
|
*
|
|
1023
1786
|
* @typeParam T - The schema type.
|
|
1024
1787
|
*
|
|
@@ -1032,10 +1795,8 @@ type CombinatorUnrequired = {
|
|
|
1032
1795
|
* query: unrequired(positional())
|
|
1033
1796
|
* }
|
|
1034
1797
|
* ```
|
|
1035
|
-
*
|
|
1036
|
-
* @experimental
|
|
1037
1798
|
*/
|
|
1038
|
-
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;
|
|
1039
1800
|
/**
|
|
1040
1801
|
* Recursively merge a tuple of {@link Args} types.
|
|
1041
1802
|
* Later types override earlier ones on key conflicts.
|
|
@@ -1061,8 +1822,6 @@ type MergeArgs<T extends Args[]> = T extends [infer Only extends Args] ? Only :
|
|
|
1061
1822
|
* help: short(boolean(), 'h')
|
|
1062
1823
|
* })
|
|
1063
1824
|
* ```
|
|
1064
|
-
*
|
|
1065
|
-
* @experimental
|
|
1066
1825
|
*/
|
|
1067
1826
|
declare function args<T extends Args>(fields: T): T;
|
|
1068
1827
|
/**
|
|
@@ -1083,8 +1842,6 @@ declare function args<T extends Args>(fields: T): T;
|
|
|
1083
1842
|
* const network = args({ host: required(string()), port: withDefault(integer(), 8080) })
|
|
1084
1843
|
* const schema = merge(common, network)
|
|
1085
1844
|
* ```
|
|
1086
|
-
*
|
|
1087
|
-
* @experimental
|
|
1088
1845
|
*/
|
|
1089
1846
|
declare function merge<A extends Args, B extends Args>(a: A, b: B): Omit<A, keyof B> & B;
|
|
1090
1847
|
/**
|
|
@@ -1094,8 +1851,6 @@ declare function merge<A extends Args, B extends Args>(a: A, b: B): Omit<A, keyo
|
|
|
1094
1851
|
* @param b - Second schema.
|
|
1095
1852
|
* @param c - Third schema.
|
|
1096
1853
|
* @returns A merged schema containing all fields.
|
|
1097
|
-
*
|
|
1098
|
-
* @experimental
|
|
1099
1854
|
*/
|
|
1100
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;
|
|
1101
1856
|
/**
|
|
@@ -1106,8 +1861,6 @@ declare function merge<A extends Args, B extends Args, C extends Args>(a: A, b:
|
|
|
1106
1861
|
* @param c - Third schema.
|
|
1107
1862
|
* @param d - Fourth schema.
|
|
1108
1863
|
* @returns A merged schema containing all fields.
|
|
1109
|
-
*
|
|
1110
|
-
* @experimental
|
|
1111
1864
|
*/
|
|
1112
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]>;
|
|
1113
1866
|
/**
|
|
@@ -1115,8 +1868,6 @@ declare function merge<A extends Args, B extends Args, C extends Args, D extends
|
|
|
1115
1868
|
*
|
|
1116
1869
|
* @param schemas - The schemas to merge.
|
|
1117
1870
|
* @returns A merged schema containing all fields.
|
|
1118
|
-
*
|
|
1119
|
-
* @experimental
|
|
1120
1871
|
*/
|
|
1121
1872
|
declare function merge<T extends Args[]>(...schemas: T): MergeArgs<T>;
|
|
1122
1873
|
/**
|
|
@@ -1137,12 +1888,9 @@ declare function merge<T extends Args[]>(...schemas: T): MergeArgs<T>;
|
|
|
1137
1888
|
* const base = args({ port: withDefault(integer(), 8080) })
|
|
1138
1889
|
* const strict = extend(base, { port: required(integer({ min: 1, max: 65535 })) })
|
|
1139
1890
|
* ```
|
|
1140
|
-
*
|
|
1141
|
-
* @experimental
|
|
1142
1891
|
*/
|
|
1143
1892
|
declare function extend<T extends Args, U extends Args>(base: T, overrides: U): Omit<T, keyof U> & U;
|
|
1144
1893
|
//#endregion
|
|
1145
|
-
//#endregion
|
|
1146
1894
|
//#region src/index.d.ts
|
|
1147
1895
|
/**
|
|
1148
1896
|
* @author kazuya kawaguchi (a.k.a. kazupon)
|