@gunshi/definition 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/lib/index.d.ts +221 -88
- package/lib/index.js +12 -5
- 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/parser-DhESN1gR.d.ts
|
|
2
2
|
//#region src/parser.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* Entry point of argument parser.
|
|
@@ -42,20 +42,24 @@ interface ArgToken {
|
|
|
42
42
|
*/
|
|
43
43
|
rawName?: string;
|
|
44
44
|
/**
|
|
45
|
-
* Option value, e.g. `--foo=bar` => `bar
|
|
45
|
+
* Option value, e.g. `--foo=bar` => `bar`. The value of a short option, such as `bar` of
|
|
46
|
+
* `-x=bar`, `-1` of `-x=-1` or `-x-1`, and `''` of `-x=`, is in a token of its own after the
|
|
47
|
+
* option, which has no `name` and `rawName`.
|
|
46
48
|
* If the `allowCompatible` option is `true`, short option value will be same as Node.js `parseArgs` behavior.
|
|
47
49
|
*/
|
|
48
|
-
value?: string;
|
|
50
|
+
value?: string | undefined;
|
|
49
51
|
/**
|
|
50
|
-
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true
|
|
52
|
+
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true`, `-x-1` => `false`, since no `=`
|
|
53
|
+
* is written. Unlike Node.js `parseArgs`, `false` does not mean that the value is the next
|
|
54
|
+
* argument: the value token of `-x-1` has the `index` of that argument.
|
|
51
55
|
*/
|
|
52
|
-
inlineValue?: boolean;
|
|
56
|
+
inlineValue?: boolean | undefined;
|
|
53
57
|
}
|
|
54
58
|
/**
|
|
55
59
|
* Parser Options.
|
|
56
60
|
*/
|
|
57
61
|
//#endregion
|
|
58
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
62
|
+
//#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/resolver.d.ts
|
|
59
63
|
//#region src/resolver.d.ts
|
|
60
64
|
/**
|
|
61
65
|
* An argument schema definition for command-line argument parsing.
|
|
@@ -101,19 +105,25 @@ interface ArgSchema {
|
|
|
101
105
|
/**
|
|
102
106
|
* Type of the argument value.
|
|
103
107
|
*
|
|
104
|
-
* - `'string'`: Text value
|
|
105
|
-
* - `'boolean'`: `true`/`false` flag (can be negatable with `--no-` prefix)
|
|
108
|
+
* - `'string'`: Text value
|
|
109
|
+
* - `'boolean'`: `true`/`false` flag (can be negatable with `--no-` prefix). `--flag=true` and
|
|
110
|
+
* `--flag=false` set the value explicitly; any other value after `=` is a type error
|
|
106
111
|
* - `'number'`: Numeric value (parsed as integer or float)
|
|
107
|
-
* - `'enum'`: One of predefined string values (requires `choices` property
|
|
112
|
+
* - `'enum'`: One of predefined string values (requires `choices` property, unless the argument
|
|
113
|
+
* has a `parse` function)
|
|
108
114
|
* - `'positional'`: Non-option argument by position
|
|
109
115
|
* - `'custom'`: Custom parsing with user-defined `parse` function
|
|
110
116
|
*
|
|
117
|
+
* Any other `type`, or no `type`, is a mistake that only untyped code can make: if the argument
|
|
118
|
+
* has no `parse` function, {@link resolveArgs} and `parse()` throw an `Error`, whether or not the
|
|
119
|
+
* argument is given.
|
|
120
|
+
*
|
|
111
121
|
* @example
|
|
112
122
|
* Different argument types:
|
|
113
123
|
* ```ts
|
|
114
124
|
* {
|
|
115
125
|
* name: { type: 'string' }, // --name value
|
|
116
|
-
* verbose: { type: 'boolean' }, // --verbose
|
|
126
|
+
* verbose: { type: 'boolean' }, // --verbose (--no-verbose needs negatable: true)
|
|
117
127
|
* port: { type: 'number' }, // --port 3000
|
|
118
128
|
* level: { type: 'enum', choices: ['debug', 'info'] },
|
|
119
129
|
* file: { type: 'positional' }, // first positional arg
|
|
@@ -188,14 +198,32 @@ interface ArgSchema {
|
|
|
188
198
|
/**
|
|
189
199
|
* Marks the argument as required.
|
|
190
200
|
*
|
|
191
|
-
* When `true`, the argument must be provided by the user.
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
201
|
+
* When `true`, the argument must be provided by the user. If it is missing, the error is an
|
|
202
|
+
* `ArgResolveError` with type 'required' and the code `err:arg:required-option`
|
|
203
|
+
* ({@link ArgsValidationErrorKeys}.requiredOption), or `err:arg:required-positional`
|
|
204
|
+
* ({@link ArgsValidationErrorKeys}.requiredPositional) for a positional argument. Its
|
|
205
|
+
* `values` has the `displayName` as in the message, such as `'--name'` for an option
|
|
206
|
+
* (`'--name' or '-n'` with the short name `n`) and `'file'` for a positional argument, and
|
|
207
|
+
* the `name`, which is the schema key.
|
|
208
|
+
* An option that is given without a value is reported as `err:arg:missing-value`
|
|
209
|
+
* ({@link ArgsValidationErrorKeys}.missingValue) instead, because the option itself was given.
|
|
210
|
+
* An explicit empty value, such as `--name=` or `-n ''`, is still reported as required, but the
|
|
211
|
+
* option counts as given: it is `true` in `explicit` and takes part in conflicts, as any other
|
|
212
|
+
* given option does.
|
|
213
|
+
*
|
|
214
|
+
* For single-value positional arguments, omitting `required` keeps the argument required for
|
|
215
|
+
* compatibility, unless it has a `default`, which makes it optional. A `multiple` positional
|
|
216
|
+
* argument is optional unless `required: true` is set. Set `required: false` to make a positional
|
|
217
|
+
* argument explicitly optional. Optional positional arguments leave enough input values for later
|
|
197
218
|
* required positional arguments before consuming a value.
|
|
198
219
|
*
|
|
220
|
+
* In the type of the values, such as {@link ArgValues}, a single-value positional argument whose
|
|
221
|
+
* `required` may be `false`, such as one of type `boolean`, is optional, as it may be missing,
|
|
222
|
+
* unless it has a `default`. An optional `required` of type `boolean`, as declared here, says
|
|
223
|
+
* nothing, and keeps it required. TypeScript widens a `required: true` to `boolean` in a schema
|
|
224
|
+
* written apart from the call, such as `{ type: 'positional' as const, required: true }`, which
|
|
225
|
+
* types the argument as optional: write such a schema `as const`.
|
|
226
|
+
*
|
|
199
227
|
* @example
|
|
200
228
|
* Required arguments:
|
|
201
229
|
* ```ts
|
|
@@ -216,8 +244,9 @@ interface ArgSchema {
|
|
|
216
244
|
/**
|
|
217
245
|
* Allows the argument to accept multiple values.
|
|
218
246
|
*
|
|
219
|
-
* When `true`, the resolved value becomes an array.
|
|
220
|
-
*
|
|
247
|
+
* When `true`, the resolved value becomes an array. When the argument is not given and has no
|
|
248
|
+
* `default`, its value is `undefined`, not an empty array.
|
|
249
|
+
* For options: can be specified multiple times (--tags foo --tags bar)
|
|
221
250
|
* For positional: collects remaining positional arguments after preserving values for
|
|
222
251
|
* later required positional arguments.
|
|
223
252
|
*
|
|
@@ -249,6 +278,12 @@ interface ArgSchema {
|
|
|
249
278
|
*
|
|
250
279
|
* Only applicable to `type: 'boolean'` arguments.
|
|
251
280
|
*
|
|
281
|
+
* The negated name is always `no-` followed by the full option name. An option named
|
|
282
|
+
* `no-cache` is negated by `--no-no-cache`, and `--no-cache` sets it to `true`.
|
|
283
|
+
*
|
|
284
|
+
* The negated form does not take a value. `--no-flag=<value>` is reported as
|
|
285
|
+
* `err:arg:unexpected-value` ({@link ArgsValidationErrorKeys}.unexpectedValue).
|
|
286
|
+
*
|
|
252
287
|
* @example
|
|
253
288
|
* Negatable boolean:
|
|
254
289
|
* ```ts
|
|
@@ -267,8 +302,17 @@ interface ArgSchema {
|
|
|
267
302
|
/**
|
|
268
303
|
* Array of allowed string values for enum-type arguments.
|
|
269
304
|
*
|
|
270
|
-
* Required when `type: 'enum'
|
|
271
|
-
*
|
|
305
|
+
* Required when `type: 'enum'`, unless the argument has a `parse` function: if `choices` is
|
|
306
|
+
* missing or not an array, {@link resolveArgs} and `parse()` throw a `TypeError`, whether or not
|
|
307
|
+
* the argument is given. With a `parse` function and no `choices`, the value is passed to the
|
|
308
|
+
* function as for a `string` argument with a `parse` function.
|
|
309
|
+
*
|
|
310
|
+
* The argument value must be one of these choices, otherwise the error is an `ArgResolveError`
|
|
311
|
+
* with type 'type' and the code `err:arg:invalid-choice`
|
|
312
|
+
* ({@link ArgsValidationErrorKeys}.invalidChoice).
|
|
313
|
+
*
|
|
314
|
+
* The value is checked before `parse` is called, so a `parse` function receives only one of
|
|
315
|
+
* these choices and can change it, for example to upper case.
|
|
272
316
|
*
|
|
273
317
|
* Supports both mutable arrays and readonly arrays for type safety.
|
|
274
318
|
*
|
|
@@ -292,18 +336,37 @@ interface ArgSchema {
|
|
|
292
336
|
*/
|
|
293
337
|
choices?: string[] | readonly string[];
|
|
294
338
|
/**
|
|
295
|
-
* Default value used when the argument is not provided.
|
|
339
|
+
* Default value used when the argument is not provided. An option that is given without a value,
|
|
340
|
+
* or with a value that is rejected, also gets its default, along with the error.
|
|
296
341
|
*
|
|
297
342
|
* The type must match the argument's `type` property:
|
|
298
343
|
* - `string` type: string default
|
|
299
344
|
* - `boolean` type: boolean default
|
|
300
345
|
* - `number` type: number default
|
|
301
|
-
* - `enum` type:
|
|
346
|
+
* - `enum` type: one of the `choices` values, or a value that a `parse` of your own returns
|
|
302
347
|
* - `positional`/`custom` type: string, boolean, or number default
|
|
303
348
|
*
|
|
304
|
-
*
|
|
305
|
-
* value
|
|
306
|
-
*
|
|
349
|
+
* The default is used as is and never goes through `parse`, including when an option is given
|
|
350
|
+
* without a value. An explicit empty value, such as `--name=` or `-n ''`, is a value, not a
|
|
351
|
+
* missing one: a `string` option without `parse` gets `''` instead of the default, unless it is
|
|
352
|
+
* `required`. What `parse` returns is a value too, even `undefined` or `null`, and the default
|
|
353
|
+
* does not replace it.
|
|
354
|
+
*
|
|
355
|
+
* The value of a `multiple` argument is an array, so its default becomes the only element of the
|
|
356
|
+
* array: `default: 'latest'` gives `['latest']`.
|
|
357
|
+
*
|
|
358
|
+
* The default of an `enum` option with `choices` is checked when it would be used, that is, when
|
|
359
|
+
* no value from the command line is used: one that is not one of the choices is reported as an
|
|
360
|
+
* `ArgResolveError` with type 'type' and the code `err:arg:invalid-default`
|
|
361
|
+
* ({@link ArgsValidationErrorKeys}.invalidDefault), and is not used. With a `parse` function of
|
|
362
|
+
* your own, or a `map()` transform, the default is a value that the function returns, which need
|
|
363
|
+
* not be one of the choices, and it is not checked. `choice()` returns the value as is, so its
|
|
364
|
+
* default is checked.
|
|
365
|
+
*
|
|
366
|
+
* For positional arguments, `multiple` ones included, the default is used when no value is left
|
|
367
|
+
* for the argument: when the positional values run out, or when the remaining ones are preserved
|
|
368
|
+
* for later required positional arguments. With `required: true`, the default is not used, and
|
|
369
|
+
* the argument is reported as required instead.
|
|
307
370
|
*
|
|
308
371
|
* @example
|
|
309
372
|
* Default values by type:
|
|
@@ -336,7 +399,8 @@ interface ArgSchema {
|
|
|
336
399
|
* When `true`, a property like `maxCount` becomes available as `--max-count`.
|
|
337
400
|
* This allows [CAC](https://github.com/cacjs/cac) user-friendly property names while maintaining CLI conventions.
|
|
338
401
|
*
|
|
339
|
-
*
|
|
402
|
+
* The `toKebab` option of {@link resolveArgs} and `parse()` applies it to all arguments, as in
|
|
403
|
+
* `resolveArgs(args, tokens, { toKebab: true })`.
|
|
340
404
|
*
|
|
341
405
|
* Note: Only `true` is allowed (not `false`) to make intent explicit.
|
|
342
406
|
*
|
|
@@ -360,8 +424,19 @@ interface ArgSchema {
|
|
|
360
424
|
/**
|
|
361
425
|
* Names of other options that conflict with this option.
|
|
362
426
|
*
|
|
363
|
-
* When this option is used together with any of the conflicting options,
|
|
364
|
-
*
|
|
427
|
+
* When this option is used together with any of the conflicting options, the error is an
|
|
428
|
+
* `ArgResolveError` with type 'conflict' and the code `err:arg:conflict`
|
|
429
|
+
* ({@link ArgsValidationErrorKeys}.conflict). Its `values` has the `displayName` and `name` of
|
|
430
|
+
* the argument whose `conflicts` names the other one, and the `conflictDisplayName` and
|
|
431
|
+
* `conflictName` of the other one. `displayName` and `conflictDisplayName` show an option as
|
|
432
|
+
* it was written, such as `'-p'`, and `name` and `conflictName` are the schema keys. When both
|
|
433
|
+
* name each other, `name` is the one that comes first in the schema.
|
|
434
|
+
*
|
|
435
|
+
* A positional argument can be on either side, such as `[file]` and `--stdin`. It conflicts when
|
|
436
|
+
* it is given, and it is shown by its name, such as `'file'` (in kebab-case with `toKebab`), as
|
|
437
|
+
* in its other errors. The message starts with the kind of the argument given as `name`:
|
|
438
|
+
* `Positional argument 'file' conflicts with '--stdin'` or
|
|
439
|
+
* `Optional argument '--stdin' conflicts with 'file'`.
|
|
365
440
|
*
|
|
366
441
|
* Conflicts only need to be defined on one side - if option A defines a conflict
|
|
367
442
|
* with option B, the conflict is automatically detected when both are used,
|
|
@@ -399,7 +474,7 @@ interface ArgSchema {
|
|
|
399
474
|
* // No conflicts defined, but still cannot use with --summer
|
|
400
475
|
* }
|
|
401
476
|
* }
|
|
402
|
-
* // Usage: --summer --autumn
|
|
477
|
+
* // Usage: --summer --autumn reports an error
|
|
403
478
|
* // Error: "Optional argument '--summer' conflicts with '--autumn'"
|
|
404
479
|
* ```
|
|
405
480
|
*
|
|
@@ -451,6 +526,10 @@ interface ArgSchema {
|
|
|
451
526
|
* Particularly useful for `type: 'custom'` arguments where the type
|
|
452
527
|
* name would otherwise be unhelpful.
|
|
453
528
|
*
|
|
529
|
+
* For a `custom` option that is given without a value, it is also the `expected` in the `values`
|
|
530
|
+
* of the `err:arg:missing-value` error ({@link ArgsValidationErrorKeys}.missingValue), which is
|
|
531
|
+
* `'custom'` without it.
|
|
532
|
+
*
|
|
454
533
|
* @example
|
|
455
534
|
* Metavar usage:
|
|
456
535
|
* ```ts
|
|
@@ -467,13 +546,37 @@ interface ArgSchema {
|
|
|
467
546
|
*/
|
|
468
547
|
metavar?: string;
|
|
469
548
|
/**
|
|
470
|
-
* Custom parsing function
|
|
549
|
+
* Custom parsing function, used in place of the parsing of the argument's `type`.
|
|
471
550
|
*
|
|
472
|
-
* Required when `type: 'custom'
|
|
473
|
-
*
|
|
551
|
+
* Required when `type: 'custom'`: if it is missing or not a function, {@link resolveArgs} and
|
|
552
|
+
* `parse()` throw a `TypeError`, whether or not the argument is given. The function receives the
|
|
553
|
+
* raw string value and must return the parsed result. It should throw an Error (or subclass) if
|
|
554
|
+
* parsing fails.
|
|
474
555
|
*
|
|
475
556
|
* The function's return type becomes the resolved argument type.
|
|
476
557
|
*
|
|
558
|
+
* `parse` is called synchronously, and what it returns becomes the value, even `undefined` or
|
|
559
|
+
* `null`, so throw to reject a value. An `async` function returns a promise, and the promise
|
|
560
|
+
* becomes the value as is: it is not awaited, and its rejection is not reported as an error.
|
|
561
|
+
* Await the value, or each of its elements with `multiple` (for example with `Promise.all()`),
|
|
562
|
+
* and handle the rejection yourself: a rejection that nothing handles is an unhandled rejection,
|
|
563
|
+
* which ends a Node.js process by default.
|
|
564
|
+
*
|
|
565
|
+
* A `boolean` option calls `parse` with `'true'`, or `'false'` for the negated form. Other
|
|
566
|
+
* options call it only with a value from the command line: when the option is given without a
|
|
567
|
+
* value, `parse` is not called and the missing value is reported as `err:arg:missing-value`
|
|
568
|
+
* ({@link ArgsValidationErrorKeys}.missingValue). When that error may suggest the argument after
|
|
569
|
+
* the option as its value, such as `--port=-5` for `--port -5`, a `parse` function of your own
|
|
570
|
+
* is not called to check it either: only the `parse` functions of `string()`, `number()`,
|
|
571
|
+
* `integer()`, `float()` and `choice()`, which have no side effects, are called for that.
|
|
572
|
+
* An explicit empty value, such as `--name=` or `-n=`, is passed as `''` unless
|
|
573
|
+
* `required: true` is set.
|
|
574
|
+
*
|
|
575
|
+
* An `enum` option with `choices` calls it only with one of them. Any other value, an explicit
|
|
576
|
+
* empty one included, is reported as `err:arg:invalid-choice`
|
|
577
|
+
* ({@link ArgsValidationErrorKeys}.invalidChoice), except that a required option reports an
|
|
578
|
+
* explicit empty value as required.
|
|
579
|
+
*
|
|
477
580
|
* @param value - Raw string value from command line
|
|
478
581
|
* @returns Parsed value of any type
|
|
479
582
|
* @throws {Error} Error or subclass when value is invalid
|
|
@@ -508,13 +611,6 @@ interface ArgSchema {
|
|
|
508
611
|
*/
|
|
509
612
|
parse?: (value: string) => any;
|
|
510
613
|
}
|
|
511
|
-
/**
|
|
512
|
-
* Machine-readable error codes for {@link ArgsValidationError}.
|
|
513
|
-
*
|
|
514
|
-
* Each code identifies a validation failure category and is also suitable as an
|
|
515
|
-
* i18n resource key for localized rendering.
|
|
516
|
-
*/
|
|
517
|
-
|
|
518
614
|
/**
|
|
519
615
|
* An object that contains {@link ArgSchema | argument schema}.
|
|
520
616
|
*
|
|
@@ -526,6 +622,20 @@ interface Args {
|
|
|
526
622
|
/**
|
|
527
623
|
* An object that contains the values of the arguments.
|
|
528
624
|
*
|
|
625
|
+
* The value of an argument whose schema is a union, such as
|
|
626
|
+
* `strict ? multiple(integer()) : string()` with the combinators, is typed as a value of any of its
|
|
627
|
+
* schemas: `number[] | string`. The argument is typed as present only when each schema of the
|
|
628
|
+
* union gives it a value, with a default, with `required: true` or as a required positional
|
|
629
|
+
* argument.
|
|
630
|
+
*
|
|
631
|
+
* When the type of one schema covers that of the other, as the type of `integer()` covers that of
|
|
632
|
+
* `multiple(integer())`, TypeScript types a `?:` expression of them as the covering schema alone:
|
|
633
|
+
* the value of `strict ? multiple(integer()) : integer()` is typed without the array, and
|
|
634
|
+
* `strict ? multiple(positional()) : positional()` as present, although it may be missing. Give
|
|
635
|
+
* each schema its own argument instead, or give the expression the type of both schemas with `as`,
|
|
636
|
+
* as in `(strict ? many : one) as typeof many | typeof one` for `const many = multiple(integer())`
|
|
637
|
+
* and `const one = integer()`.
|
|
638
|
+
*
|
|
529
639
|
* @typeParam T - {@link Args | Arguments} which is an object that defines the command line arguments.
|
|
530
640
|
*/
|
|
531
641
|
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
@@ -533,22 +643,41 @@ type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: Ext
|
|
|
533
643
|
};
|
|
534
644
|
/**
|
|
535
645
|
* Extracts the value type from the argument schema.
|
|
646
|
+
* For a union of schemas, it is the union of the value types of its schemas.
|
|
536
647
|
*
|
|
537
648
|
* @typeParam A - {@link ArgSchema | Argument schema} which is an object that defines command line arguments.
|
|
538
649
|
*
|
|
539
650
|
* @internal
|
|
540
651
|
*/
|
|
541
|
-
type ExtractOptionValue<A extends ArgSchema> =
|
|
652
|
+
type ExtractOptionValue<A extends ArgSchema> = A extends ArgSchema ? ExtractSchemaValue<A> : never;
|
|
653
|
+
/**
|
|
654
|
+
* The value type of one schema, which {@link ExtractOptionValue} takes for each schema of a union.
|
|
655
|
+
*/
|
|
656
|
+
type ExtractSchemaValue<A extends ArgSchema> = undefined extends A['parse'] ? A['type'] extends 'string' ? ResolveOptionValue<A, string> : A['type'] extends 'boolean' ? ResolveOptionValue<A, boolean> : A['type'] extends 'number' ? ResolveOptionValue<A, number> : A['type'] extends 'positional' ? ResolveOptionValue<A, string> : A['type'] extends 'enum' ? A['choices'] extends string[] | readonly string[] ? ResolveOptionValue<A, A['choices'][number]> : never : A['type'] extends 'custom' ? never : ResolveOptionValue<A, string | boolean | number> : A['parse'] extends ((value: string) => infer R) ? ResolveOptionValue<A, R> : never;
|
|
542
657
|
type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T[] : T;
|
|
543
658
|
/**
|
|
544
659
|
* Resolved argument values.
|
|
545
660
|
*
|
|
661
|
+
* Each argument is mapped as optional, and the ones that are present, as {@link IsPresentArg}
|
|
662
|
+
* says, are mapped again as present. The optional mapping types a present argument as `unknown`:
|
|
663
|
+
* TypeScript does not reduce an intersection such as
|
|
664
|
+
* `(string | number[] | undefined) & (string | number[])`, of a value type that is a union with an
|
|
665
|
+
* object type. It maps all the keys without renaming them, which lets TypeScript relate the values
|
|
666
|
+
* of related type parameters, so that a conversion such as `values as ArgValues<A>` compiles.
|
|
667
|
+
*
|
|
546
668
|
* @typeParam A - {@link Arguments | Args} which is an object that defines the command line arguments.
|
|
547
669
|
* @typeParam V - Resolvable argument values.
|
|
548
670
|
*
|
|
549
671
|
* @internal
|
|
550
672
|
*/
|
|
551
|
-
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: V[Arg] } &
|
|
673
|
+
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: IsPresentArg<A[Arg]> extends true ? unknown : V[Arg] } & { -readonly [Arg in keyof A as IsPresentArg<A[Arg]> extends true ? Arg : never]: V[Arg] } extends (infer P) ? { [K in keyof P]: P[K] } : never;
|
|
674
|
+
/**
|
|
675
|
+
* Whether an argument with the schema `S` has a value: with a default, with `required: true` or as
|
|
676
|
+
* a required positional argument. For a union of schemas, only when each schema of the union has
|
|
677
|
+
* one. A `default` or `required` typed as `any`, and a schema typed as `any`, count as giving a
|
|
678
|
+
* value, as {@link FilterArgs} does.
|
|
679
|
+
*/
|
|
680
|
+
type IsPresentArg<S extends ArgSchema> = S extends ArgSchema ? true extends (S['default'] extends {} ? true : S['required'] extends true ? true : false) ? true : IsRequiredPositionalArg<S> : never;
|
|
552
681
|
/**
|
|
553
682
|
* Filters the arguments based on their default values.
|
|
554
683
|
*
|
|
@@ -558,17 +687,17 @@ type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -r
|
|
|
558
687
|
*
|
|
559
688
|
* @internal
|
|
560
689
|
*/
|
|
561
|
-
|
|
690
|
+
|
|
691
|
+
type IsRequiredPositionalArg<A extends ArgSchema> = A['type'] extends 'positional' ? A['multiple'] extends true ? A['required'] extends true ? true : false : MayBeUnrequired<A> extends true ? A['default'] extends {} ? true : false : true : false;
|
|
562
692
|
/**
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
*
|
|
568
|
-
* @internal
|
|
693
|
+
* Whether the `required` of `A` may be `false`. An optional `required` of type `boolean`, as in
|
|
694
|
+
* {@link ArgSchema}, says nothing, and is read as missing, so that a positional argument with it is
|
|
695
|
+
* required. A `required` that `A` always has may be `false` when its type is `boolean` or
|
|
696
|
+
* `boolean | undefined`.
|
|
569
697
|
*/
|
|
570
|
-
type
|
|
571
|
-
|
|
698
|
+
type MayBeUnrequired<A extends ArgSchema> = false extends A['required'] ? [A] extends [{
|
|
699
|
+
required: unknown;
|
|
700
|
+
}] ? true : boolean | undefined extends A['required'] ? false : true : false;
|
|
572
701
|
/**
|
|
573
702
|
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
574
703
|
*/
|
|
@@ -576,44 +705,16 @@ type IsRequiredPositionalArg<A extends ArgSchema> = A['type'] extends 'positiona
|
|
|
576
705
|
/**
|
|
577
706
|
* Tracks which arguments were explicitly provided by the user.
|
|
578
707
|
*
|
|
579
|
-
* Each property
|
|
580
|
-
*
|
|
708
|
+
* Each property is `true` when the argument is given on the command line, and `false` when it is
|
|
709
|
+
* not, whether it has a default or not. An option is given when one of its forms is, such as
|
|
710
|
+
* `--port`, `-p` or `--no-color`, even if its value is missing or rejected: the error is reported,
|
|
711
|
+
* and if no value from the command line is used, its default, if any, fills in the value. A
|
|
712
|
+
* positional argument is given when a positional value is used for it, even if `parse` rejects
|
|
713
|
+
* the value: the error is reported, and its default is not used.
|
|
581
714
|
*
|
|
582
715
|
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
583
716
|
*/
|
|
584
717
|
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean };
|
|
585
|
-
/**
|
|
586
|
-
* Resolve command line arguments.
|
|
587
|
-
*
|
|
588
|
-
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
589
|
-
*
|
|
590
|
-
* @param args - An arguments that contains {@link ArgSchema | arguments schema}.
|
|
591
|
-
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
592
|
-
* @param resolveArgs - An arguments that contains {@link ResolveArgs | resolve arguments}.
|
|
593
|
-
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, {@link AggregateError | validation errors}, and explicit provision status.
|
|
594
|
-
*
|
|
595
|
-
* @example
|
|
596
|
-
* ```typescript
|
|
597
|
-
* // passed tokens: --port 3000
|
|
598
|
-
*
|
|
599
|
-
* const { values, explicit } = resolveArgs({
|
|
600
|
-
* port: {
|
|
601
|
-
* type: 'number',
|
|
602
|
-
* default: 8080
|
|
603
|
-
* },
|
|
604
|
-
* host: {
|
|
605
|
-
* type: 'string',
|
|
606
|
-
* default: 'localhost'
|
|
607
|
-
* }
|
|
608
|
-
* }, parsedTokens)
|
|
609
|
-
*
|
|
610
|
-
* values.port // 3000
|
|
611
|
-
* values.host // 'localhost'
|
|
612
|
-
*
|
|
613
|
-
* explicit.port // true (explicitly provided)
|
|
614
|
-
* explicit.host // false (not provided, fallback to default)
|
|
615
|
-
* ```
|
|
616
|
-
*/
|
|
617
718
|
//#endregion
|
|
618
719
|
//#region ../gunshi/src/plugin/context.d.ts
|
|
619
720
|
/**
|
|
@@ -923,7 +1024,11 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
|
|
|
923
1024
|
*/
|
|
924
1025
|
usageOptionValue: boolean;
|
|
925
1026
|
/**
|
|
926
|
-
* Whether to
|
|
1027
|
+
* Whether to keep usage, header, version, and validation-error text off the terminal
|
|
1028
|
+
* (they are still returned as strings) and to silence {@linkcode CommandContext.log}.
|
|
1029
|
+
*
|
|
1030
|
+
* Output that must still appear — machine-readable results for an agent, warnings for the
|
|
1031
|
+
* CLI author — should use `console.log` / `console.warn`, not `ctx.log`.
|
|
927
1032
|
*
|
|
928
1033
|
* @default false
|
|
929
1034
|
* @see {@linkcode CliOptions.usageSilent}
|
|
@@ -942,6 +1047,26 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
|
|
|
942
1047
|
* @see {@linkcode CliOptions.subCommands}
|
|
943
1048
|
*/
|
|
944
1049
|
subCommands: Map<string, Command<any> | LazyCommand<any>> | undefined;
|
|
1050
|
+
/**
|
|
1051
|
+
* The entry command of the CLI, marked with `entry: true`.
|
|
1052
|
+
*
|
|
1053
|
+
* It is available whether or not the CLI has sub-commands, while {@linkcode CommandEnvironment.subCommands}
|
|
1054
|
+
* includes the entry command only when the sub-commands are given with {@linkcode CliOptions.subCommands}.
|
|
1055
|
+
* It is not frozen, like the commands in {@linkcode CommandEnvironment.subCommands}.
|
|
1056
|
+
*/
|
|
1057
|
+
entryCommand?: Command<any> | LazyCommand<any>;
|
|
1058
|
+
/**
|
|
1059
|
+
* The global options that are in effect for the command being executed.
|
|
1060
|
+
*
|
|
1061
|
+
* A plugin registers a global option with `addGlobalOption`, and gunshi merges it into the arguments
|
|
1062
|
+
* of the command that runs. An argument that the command declares under the same name replaces it,
|
|
1063
|
+
* and the option is then not in effect: the value under that name is the command's, and the plugin
|
|
1064
|
+
* that registered it must not read it as its own.
|
|
1065
|
+
*
|
|
1066
|
+
* The schema is the one in effect, which may have given up its short name to an argument of the
|
|
1067
|
+
* command that claims the same letter.
|
|
1068
|
+
*/
|
|
1069
|
+
globalOptions?: ReadonlyMap<string, ArgSchema>;
|
|
945
1070
|
/**
|
|
946
1071
|
* Render function the command usage.
|
|
947
1072
|
*/
|
|
@@ -1020,7 +1145,13 @@ interface CliOptions<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
|
1020
1145
|
*/
|
|
1021
1146
|
usageOptionValue?: boolean;
|
|
1022
1147
|
/**
|
|
1023
|
-
* Whether to
|
|
1148
|
+
* Whether to keep usage, header, version, and validation-error text off the terminal
|
|
1149
|
+
* (they are still returned as strings) and to silence {@linkcode CommandContext.log}.
|
|
1150
|
+
*
|
|
1151
|
+
* Output that must still appear — machine-readable results for an agent, warnings for the
|
|
1152
|
+
* CLI author — should use `console.log` / `console.warn`, not `ctx.log`.
|
|
1153
|
+
*
|
|
1154
|
+
* @default false
|
|
1024
1155
|
*/
|
|
1025
1156
|
usageSilent?: boolean;
|
|
1026
1157
|
/**
|
|
@@ -1682,7 +1813,8 @@ declare function lazy<A extends Args>(loader: CommandLoader<{
|
|
|
1682
1813
|
* @typeParam D - A partial {@link Command} definition type with required `args`
|
|
1683
1814
|
*
|
|
1684
1815
|
* @param loader - A {@link CommandLoader | command loader} function that returns a command definition
|
|
1685
|
-
* @param definition - A {@link Command | command} definition
|
|
1816
|
+
* @param definition - A {@link Command | command} definition. Its `name` may be omitted,
|
|
1817
|
+
* including when the lazy command is used as the CLI entry command.
|
|
1686
1818
|
* @returns A {@link LazyCommand | lazy command} that can be executed later
|
|
1687
1819
|
*/
|
|
1688
1820
|
declare function lazy<D extends {
|
|
@@ -1704,7 +1836,8 @@ declare function lazy<D extends {
|
|
|
1704
1836
|
* @typeParam D - A partial {@link Command} definition type
|
|
1705
1837
|
*
|
|
1706
1838
|
* @param loader - A {@link CommandLoader | command loader} function that returns a command definition
|
|
1707
|
-
* @param definition - An optional {@link Command | command} definition
|
|
1839
|
+
* @param definition - An optional {@link Command | command} definition. Its `name` may be omitted,
|
|
1840
|
+
* including when the lazy command is used as the CLI entry command.
|
|
1708
1841
|
* @returns A {@link LazyCommand | lazy command} that can be executed later
|
|
1709
1842
|
*/
|
|
1710
1843
|
declare function lazy<G extends GunshiParamsConstraint = DefaultGunshiParams, D extends Partial<Command<G>> = Partial<Command<G>>>(loader: CommandLoader<G>, definition?: D): LazyCommand<G, D>;
|
package/lib/index.js
CHANGED
|
@@ -36,10 +36,13 @@ const CLI_OPTIONS_DEFAULT = {
|
|
|
36
36
|
* Check if the given command is a {@link LazyCommand}.
|
|
37
37
|
*
|
|
38
38
|
* @param cmd - A command to check
|
|
39
|
-
* @returns `true` if the command is a {@link LazyCommand}, otherwise `false
|
|
39
|
+
* @returns `true` if the command is a {@link LazyCommand}, otherwise `false`
|
|
40
|
+
*
|
|
41
|
+
* A lazy command is identified by its `commandName` property. The property may be
|
|
42
|
+
* `undefined` when the lazy command has no definition name.
|
|
40
43
|
*/
|
|
41
44
|
function isLazyCommand(cmd) {
|
|
42
|
-
return typeof cmd === "function" && "commandName" in cmd
|
|
45
|
+
return typeof cmd === "function" && "commandName" in cmd;
|
|
43
46
|
}
|
|
44
47
|
/**
|
|
45
48
|
* Create an object with the specified prototype. A shorthand for `Object.create`.
|
|
@@ -155,7 +158,7 @@ async function createCommandContext({ args = {}, explicit = {}, values = {}, pos
|
|
|
155
158
|
if (extension.onFactory) await extension.onFactory(core, command);
|
|
156
159
|
}
|
|
157
160
|
}
|
|
158
|
-
return deepFreeze(core, ["extensions"]);
|
|
161
|
+
return deepFreeze(core, ["extensions", "entryCommand"]);
|
|
159
162
|
}
|
|
160
163
|
function getCommandName(cmd) {
|
|
161
164
|
if (isLazyCommand(cmd)) return cmd.commandName || cmd.name || "(anonymous)";
|
|
@@ -217,11 +220,14 @@ function defineWithTypes() {
|
|
|
217
220
|
* Define a {@link LazyCommand | lazy command} with or without definition.
|
|
218
221
|
*
|
|
219
222
|
* @param loader - A {@link CommandLoader | command loader} function that returns a command definition
|
|
220
|
-
* @param definition - An optional {@link Command | command} definition
|
|
223
|
+
* @param definition - An optional {@link Command | command} definition. Its `name` may be omitted,
|
|
224
|
+
* including when the lazy command is used as the CLI entry command.
|
|
221
225
|
* @returns A {@link LazyCommand | lazy command} that can be executed later
|
|
222
226
|
*/
|
|
223
227
|
function lazy(loader, definition) {
|
|
224
|
-
|
|
228
|
+
let lazyCommand = loader;
|
|
229
|
+
if ((definition != null || !("commandName" in lazyCommand)) && !Object.isExtensible(lazyCommand)) lazyCommand = Object.assign(() => loader(), loader);
|
|
230
|
+
if (definition == null && !("commandName" in lazyCommand)) lazyCommand.commandName = void 0;
|
|
225
231
|
if (definition != null) {
|
|
226
232
|
lazyCommand.commandName = definition.name;
|
|
227
233
|
lazyCommand.description = definition.description;
|
|
@@ -230,6 +236,7 @@ function lazy(loader, definition) {
|
|
|
230
236
|
lazyCommand.internal = definition.internal;
|
|
231
237
|
lazyCommand.entry = definition.entry;
|
|
232
238
|
lazyCommand.toKebab = definition.toKebab;
|
|
239
|
+
lazyCommand.rendering = definition.rendering;
|
|
233
240
|
if (definition.subCommands) lazyCommand.subCommands = definition.subCommands;
|
|
234
241
|
if ("resource" in definition) lazyCommand.resource = definition.resource;
|
|
235
242
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gunshi/definition",
|
|
3
3
|
"description": "utilities for gunshi command definition",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0-beta.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"jsr-exports-lint": "^0.4.2",
|
|
58
58
|
"publint": "^0.3.20",
|
|
59
59
|
"tsdown": "0.21.0",
|
|
60
|
-
"gunshi": "0.
|
|
60
|
+
"gunshi": "1.0.0-beta.0"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "tsdown",
|