@gunshi/definition 0.37.3 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +223 -90
- package/lib/index.js +12 -5
- package/package.json +3 -3
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
1
|
+
//#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/parser-DhESN1gR.d.ts
|
|
2
2
|
//#region src/parser.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* Entry point of argument parser.
|
|
@@ -42,20 +42,21 @@ 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
|
-
/**
|
|
55
|
-
* Parser Options.
|
|
56
|
-
*/
|
|
57
58
|
//#endregion
|
|
58
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
59
|
+
//#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/resolver.d.ts
|
|
59
60
|
//#region src/resolver.d.ts
|
|
60
61
|
/**
|
|
61
62
|
* An argument schema definition for command-line argument parsing.
|
|
@@ -101,19 +102,25 @@ interface ArgSchema {
|
|
|
101
102
|
/**
|
|
102
103
|
* Type of the argument value.
|
|
103
104
|
*
|
|
104
|
-
* - `'string'`: Text value
|
|
105
|
-
* - `'boolean'`: `true`/`false` flag (can be negatable with `--no-` prefix)
|
|
105
|
+
* - `'string'`: Text value
|
|
106
|
+
* - `'boolean'`: `true`/`false` flag (can be negatable with `--no-` prefix). `--flag=true` and
|
|
107
|
+
* `--flag=false` set the value explicitly; any other value after `=` is a type error
|
|
106
108
|
* - `'number'`: Numeric value (parsed as integer or float)
|
|
107
|
-
* - `'enum'`: One of predefined string values (requires `choices` property
|
|
109
|
+
* - `'enum'`: One of predefined string values (requires `choices` property, unless the argument
|
|
110
|
+
* has a `parse` function)
|
|
108
111
|
* - `'positional'`: Non-option argument by position
|
|
109
112
|
* - `'custom'`: Custom parsing with user-defined `parse` function
|
|
110
113
|
*
|
|
114
|
+
* Any other `type`, or no `type`, is a mistake that only untyped code can make: if the argument
|
|
115
|
+
* has no `parse` function, {@link resolveArgs} and `parse()` throw an `Error`, whether or not the
|
|
116
|
+
* argument is given.
|
|
117
|
+
*
|
|
111
118
|
* @example
|
|
112
119
|
* Different argument types:
|
|
113
120
|
* ```ts
|
|
114
121
|
* {
|
|
115
122
|
* name: { type: 'string' }, // --name value
|
|
116
|
-
* verbose: { type: 'boolean' }, // --verbose
|
|
123
|
+
* verbose: { type: 'boolean' }, // --verbose (--no-verbose needs negatable: true)
|
|
117
124
|
* port: { type: 'number' }, // --port 3000
|
|
118
125
|
* level: { type: 'enum', choices: ['debug', 'info'] },
|
|
119
126
|
* file: { type: 'positional' }, // first positional arg
|
|
@@ -188,14 +195,32 @@ interface ArgSchema {
|
|
|
188
195
|
/**
|
|
189
196
|
* Marks the argument as required.
|
|
190
197
|
*
|
|
191
|
-
* When `true`, the argument must be provided by the user.
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
198
|
+
* When `true`, the argument must be provided by the user. If it is missing, the error is an
|
|
199
|
+
* `ArgResolveError` with type 'required' and the code `err:arg:required-option`
|
|
200
|
+
* ({@link ArgsValidationErrorKeys}.requiredOption), or `err:arg:required-positional`
|
|
201
|
+
* ({@link ArgsValidationErrorKeys}.requiredPositional) for a positional argument. Its
|
|
202
|
+
* `values` has the `displayName` as in the message, such as `'--name'` for an option
|
|
203
|
+
* (`'--name' or '-n'` with the short name `n`) and `'file'` for a positional argument, and
|
|
204
|
+
* the `name`, which is the schema key.
|
|
205
|
+
* An option that is given without a value is reported as `err:arg:missing-value`
|
|
206
|
+
* ({@link ArgsValidationErrorKeys}.missingValue) instead, because the option itself was given.
|
|
207
|
+
* An explicit empty value, such as `--name=` or `-n ''`, is still reported as required, but the
|
|
208
|
+
* option counts as given: it is `true` in `explicit` and takes part in conflicts, as any other
|
|
209
|
+
* given option does.
|
|
210
|
+
*
|
|
211
|
+
* For single-value positional arguments, omitting `required` keeps the argument required for
|
|
212
|
+
* compatibility, unless it has a `default`, which makes it optional. A `multiple` positional
|
|
213
|
+
* argument is optional unless `required: true` is set. Set `required: false` to make a positional
|
|
214
|
+
* argument explicitly optional. Optional positional arguments leave enough input values for later
|
|
197
215
|
* required positional arguments before consuming a value.
|
|
198
216
|
*
|
|
217
|
+
* In the type of the values, such as {@link ArgValues}, a single-value positional argument whose
|
|
218
|
+
* `required` may be `false`, such as one of type `boolean`, is optional, as it may be missing,
|
|
219
|
+
* unless it has a `default`. An optional `required` of type `boolean`, as declared here, says
|
|
220
|
+
* nothing, and keeps it required. TypeScript widens a `required: true` to `boolean` in a schema
|
|
221
|
+
* written apart from the call, such as `{ type: 'positional' as const, required: true }`, which
|
|
222
|
+
* types the argument as optional: write such a schema `as const`.
|
|
223
|
+
*
|
|
199
224
|
* @example
|
|
200
225
|
* Required arguments:
|
|
201
226
|
* ```ts
|
|
@@ -216,8 +241,9 @@ interface ArgSchema {
|
|
|
216
241
|
/**
|
|
217
242
|
* Allows the argument to accept multiple values.
|
|
218
243
|
*
|
|
219
|
-
* When `true`, the resolved value becomes an array.
|
|
220
|
-
*
|
|
244
|
+
* When `true`, the resolved value becomes an array. When the argument is not given and has no
|
|
245
|
+
* `default`, its value is `undefined`, not an empty array.
|
|
246
|
+
* For options: can be specified multiple times (--tags foo --tags bar)
|
|
221
247
|
* For positional: collects remaining positional arguments after preserving values for
|
|
222
248
|
* later required positional arguments.
|
|
223
249
|
*
|
|
@@ -249,6 +275,12 @@ interface ArgSchema {
|
|
|
249
275
|
*
|
|
250
276
|
* Only applicable to `type: 'boolean'` arguments.
|
|
251
277
|
*
|
|
278
|
+
* The negated name is always `no-` followed by the full option name. An option named
|
|
279
|
+
* `no-cache` is negated by `--no-no-cache`, and `--no-cache` sets it to `true`.
|
|
280
|
+
*
|
|
281
|
+
* The negated form does not take a value. `--no-flag=<value>` is reported as
|
|
282
|
+
* `err:arg:unexpected-value` ({@link ArgsValidationErrorKeys}.unexpectedValue).
|
|
283
|
+
*
|
|
252
284
|
* @example
|
|
253
285
|
* Negatable boolean:
|
|
254
286
|
* ```ts
|
|
@@ -267,8 +299,17 @@ interface ArgSchema {
|
|
|
267
299
|
/**
|
|
268
300
|
* Array of allowed string values for enum-type arguments.
|
|
269
301
|
*
|
|
270
|
-
* Required when `type: 'enum'
|
|
271
|
-
*
|
|
302
|
+
* Required when `type: 'enum'`, unless the argument has a `parse` function: if `choices` is
|
|
303
|
+
* missing or not an array, {@link resolveArgs} and `parse()` throw a `TypeError`, whether or not
|
|
304
|
+
* the argument is given. With a `parse` function and no `choices`, the value is passed to the
|
|
305
|
+
* function as for a `string` argument with a `parse` function.
|
|
306
|
+
*
|
|
307
|
+
* The argument value must be one of these choices, otherwise the error is an `ArgResolveError`
|
|
308
|
+
* with type 'type' and the code `err:arg:invalid-choice`
|
|
309
|
+
* ({@link ArgsValidationErrorKeys}.invalidChoice).
|
|
310
|
+
*
|
|
311
|
+
* The value is checked before `parse` is called, so a `parse` function receives only one of
|
|
312
|
+
* these choices and can change it, for example to upper case.
|
|
272
313
|
*
|
|
273
314
|
* Supports both mutable arrays and readonly arrays for type safety.
|
|
274
315
|
*
|
|
@@ -292,18 +333,37 @@ interface ArgSchema {
|
|
|
292
333
|
*/
|
|
293
334
|
choices?: string[] | readonly string[];
|
|
294
335
|
/**
|
|
295
|
-
* Default value used when the argument is not provided.
|
|
336
|
+
* Default value used when the argument is not provided. An option that is given without a value,
|
|
337
|
+
* or with a value that is rejected, also gets its default, along with the error.
|
|
296
338
|
*
|
|
297
339
|
* The type must match the argument's `type` property:
|
|
298
340
|
* - `string` type: string default
|
|
299
341
|
* - `boolean` type: boolean default
|
|
300
342
|
* - `number` type: number default
|
|
301
|
-
* - `enum` type:
|
|
343
|
+
* - `enum` type: one of the `choices` values, or a value that a `parse` of your own returns
|
|
302
344
|
* - `positional`/`custom` type: string, boolean, or number default
|
|
303
345
|
*
|
|
304
|
-
*
|
|
305
|
-
* value
|
|
306
|
-
*
|
|
346
|
+
* The default is used as is and never goes through `parse`, including when an option is given
|
|
347
|
+
* without a value. An explicit empty value, such as `--name=` or `-n ''`, is a value, not a
|
|
348
|
+
* missing one: a `string` option without `parse` gets `''` instead of the default, unless it is
|
|
349
|
+
* `required`. What `parse` returns is a value too, even `undefined` or `null`, and the default
|
|
350
|
+
* does not replace it.
|
|
351
|
+
*
|
|
352
|
+
* The value of a `multiple` argument is an array, so its default becomes the only element of the
|
|
353
|
+
* array: `default: 'latest'` gives `['latest']`.
|
|
354
|
+
*
|
|
355
|
+
* The default of an `enum` option with `choices` is checked when it would be used, that is, when
|
|
356
|
+
* no value from the command line is used: one that is not one of the choices is reported as an
|
|
357
|
+
* `ArgResolveError` with type 'type' and the code `err:arg:invalid-default`
|
|
358
|
+
* ({@link ArgsValidationErrorKeys}.invalidDefault), and is not used. With a `parse` function of
|
|
359
|
+
* your own, or a `map()` transform, the default is a value that the function returns, which need
|
|
360
|
+
* not be one of the choices, and it is not checked. `choice()` returns the value as is, so its
|
|
361
|
+
* default is checked.
|
|
362
|
+
*
|
|
363
|
+
* For positional arguments, `multiple` ones included, the default is used when no value is left
|
|
364
|
+
* for the argument: when the positional values run out, or when the remaining ones are preserved
|
|
365
|
+
* for later required positional arguments. With `required: true`, the default is not used, and
|
|
366
|
+
* the argument is reported as required instead.
|
|
307
367
|
*
|
|
308
368
|
* @example
|
|
309
369
|
* Default values by type:
|
|
@@ -336,7 +396,8 @@ interface ArgSchema {
|
|
|
336
396
|
* When `true`, a property like `maxCount` becomes available as `--max-count`.
|
|
337
397
|
* This allows [CAC](https://github.com/cacjs/cac) user-friendly property names while maintaining CLI conventions.
|
|
338
398
|
*
|
|
339
|
-
*
|
|
399
|
+
* The `toKebab` option of {@link resolveArgs} and `parse()` applies it to all arguments, as in
|
|
400
|
+
* `resolveArgs(args, tokens, { toKebab: true })`.
|
|
340
401
|
*
|
|
341
402
|
* Note: Only `true` is allowed (not `false`) to make intent explicit.
|
|
342
403
|
*
|
|
@@ -360,8 +421,19 @@ interface ArgSchema {
|
|
|
360
421
|
/**
|
|
361
422
|
* Names of other options that conflict with this option.
|
|
362
423
|
*
|
|
363
|
-
* When this option is used together with any of the conflicting options,
|
|
364
|
-
*
|
|
424
|
+
* When this option is used together with any of the conflicting options, the error is an
|
|
425
|
+
* `ArgResolveError` with type 'conflict' and the code `err:arg:conflict`
|
|
426
|
+
* ({@link ArgsValidationErrorKeys}.conflict). Its `values` has the `displayName` and `name` of
|
|
427
|
+
* the argument whose `conflicts` names the other one, and the `conflictDisplayName` and
|
|
428
|
+
* `conflictName` of the other one. `displayName` and `conflictDisplayName` show an option as
|
|
429
|
+
* it was written, such as `'-p'`, and `name` and `conflictName` are the schema keys. When both
|
|
430
|
+
* name each other, `name` is the one that comes first in the schema.
|
|
431
|
+
*
|
|
432
|
+
* A positional argument can be on either side, such as `[file]` and `--stdin`. It conflicts when
|
|
433
|
+
* it is given, and it is shown by its name, such as `'file'` (in kebab-case with `toKebab`), as
|
|
434
|
+
* in its other errors. The message starts with the kind of the argument given as `name`:
|
|
435
|
+
* `Positional argument 'file' conflicts with '--stdin'` or
|
|
436
|
+
* `Optional argument '--stdin' conflicts with 'file'`.
|
|
365
437
|
*
|
|
366
438
|
* Conflicts only need to be defined on one side - if option A defines a conflict
|
|
367
439
|
* with option B, the conflict is automatically detected when both are used,
|
|
@@ -399,7 +471,7 @@ interface ArgSchema {
|
|
|
399
471
|
* // No conflicts defined, but still cannot use with --summer
|
|
400
472
|
* }
|
|
401
473
|
* }
|
|
402
|
-
* // Usage: --summer --autumn
|
|
474
|
+
* // Usage: --summer --autumn reports an error
|
|
403
475
|
* // Error: "Optional argument '--summer' conflicts with '--autumn'"
|
|
404
476
|
* ```
|
|
405
477
|
*
|
|
@@ -451,6 +523,10 @@ interface ArgSchema {
|
|
|
451
523
|
* Particularly useful for `type: 'custom'` arguments where the type
|
|
452
524
|
* name would otherwise be unhelpful.
|
|
453
525
|
*
|
|
526
|
+
* For a `custom` option that is given without a value, it is also the `expected` in the `values`
|
|
527
|
+
* of the `err:arg:missing-value` error ({@link ArgsValidationErrorKeys}.missingValue), which is
|
|
528
|
+
* `'custom'` without it.
|
|
529
|
+
*
|
|
454
530
|
* @example
|
|
455
531
|
* Metavar usage:
|
|
456
532
|
* ```ts
|
|
@@ -467,13 +543,37 @@ interface ArgSchema {
|
|
|
467
543
|
*/
|
|
468
544
|
metavar?: string;
|
|
469
545
|
/**
|
|
470
|
-
* Custom parsing function
|
|
546
|
+
* Custom parsing function, used in place of the parsing of the argument's `type`.
|
|
471
547
|
*
|
|
472
|
-
* Required when `type: 'custom'
|
|
473
|
-
*
|
|
548
|
+
* Required when `type: 'custom'`: if it is missing or not a function, {@link resolveArgs} and
|
|
549
|
+
* `parse()` throw a `TypeError`, whether or not the argument is given. The function receives the
|
|
550
|
+
* raw string value and must return the parsed result. It should throw an Error (or subclass) if
|
|
551
|
+
* parsing fails.
|
|
474
552
|
*
|
|
475
553
|
* The function's return type becomes the resolved argument type.
|
|
476
554
|
*
|
|
555
|
+
* `parse` is called synchronously, and what it returns becomes the value, even `undefined` or
|
|
556
|
+
* `null`, so throw to reject a value. An `async` function returns a promise, and the promise
|
|
557
|
+
* becomes the value as is: it is not awaited, and its rejection is not reported as an error.
|
|
558
|
+
* Await the value, or each of its elements with `multiple` (for example with `Promise.all()`),
|
|
559
|
+
* and handle the rejection yourself: a rejection that nothing handles is an unhandled rejection,
|
|
560
|
+
* which ends a Node.js process by default.
|
|
561
|
+
*
|
|
562
|
+
* A `boolean` option calls `parse` with `'true'`, or `'false'` for the negated form. Other
|
|
563
|
+
* options call it only with a value from the command line: when the option is given without a
|
|
564
|
+
* value, `parse` is not called and the missing value is reported as `err:arg:missing-value`
|
|
565
|
+
* ({@link ArgsValidationErrorKeys}.missingValue). When that error may suggest the argument after
|
|
566
|
+
* the option as its value, such as `--port=-5` for `--port -5`, a `parse` function of your own
|
|
567
|
+
* is not called to check it either: only the `parse` functions of `string()`, `number()`,
|
|
568
|
+
* `integer()`, `float()` and `choice()`, which have no side effects, are called for that.
|
|
569
|
+
* An explicit empty value, such as `--name=` or `-n=`, is passed as `''` unless
|
|
570
|
+
* `required: true` is set.
|
|
571
|
+
*
|
|
572
|
+
* An `enum` option with `choices` calls it only with one of them. Any other value, an explicit
|
|
573
|
+
* empty one included, is reported as `err:arg:invalid-choice`
|
|
574
|
+
* ({@link ArgsValidationErrorKeys}.invalidChoice), except that a required option reports an
|
|
575
|
+
* explicit empty value as required.
|
|
576
|
+
*
|
|
477
577
|
* @param value - Raw string value from command line
|
|
478
578
|
* @returns Parsed value of any type
|
|
479
579
|
* @throws {Error} Error or subclass when value is invalid
|
|
@@ -519,62 +619,85 @@ interface Args {
|
|
|
519
619
|
/**
|
|
520
620
|
* An object that contains the values of the arguments.
|
|
521
621
|
*
|
|
622
|
+
* The value of an argument whose schema is a union, such as
|
|
623
|
+
* `strict ? multiple(integer()) : string()` with the combinators, is typed as a value of any of its
|
|
624
|
+
* schemas: `number[] | string`. The argument is typed as present only when each schema of the
|
|
625
|
+
* union gives it a value, with a default, with `required: true` or as a required positional
|
|
626
|
+
* argument.
|
|
627
|
+
*
|
|
628
|
+
* When the type of one schema covers that of the other, as the type of `integer()` covers that of
|
|
629
|
+
* `multiple(integer())`, TypeScript types a `?:` expression of them as the covering schema alone:
|
|
630
|
+
* the value of `strict ? multiple(integer()) : integer()` is typed without the array, and
|
|
631
|
+
* `strict ? multiple(positional()) : positional()` as present, although it may be missing. Give
|
|
632
|
+
* each schema its own argument instead, or give the expression the type of both schemas with `as`,
|
|
633
|
+
* as in `(strict ? many : one) as typeof many | typeof one` for `const many = multiple(integer())`
|
|
634
|
+
* and `const one = integer()`.
|
|
635
|
+
*
|
|
522
636
|
* @typeParam T - {@link Args | Arguments} which is an object that defines the command line arguments.
|
|
523
637
|
*/
|
|
524
|
-
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]
|
|
638
|
+
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]>; }> : {
|
|
525
639
|
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
526
640
|
};
|
|
527
641
|
/**
|
|
528
642
|
* Extracts the value type from the argument schema.
|
|
643
|
+
* For a union of schemas, it is the union of the value types of its schemas.
|
|
529
644
|
*
|
|
530
645
|
* @typeParam A - {@link ArgSchema | Argument schema} which is an object that defines command line arguments.
|
|
531
646
|
*
|
|
532
647
|
* @internal
|
|
533
648
|
*/
|
|
534
|
-
type ExtractOptionValue<A extends ArgSchema> =
|
|
649
|
+
type ExtractOptionValue<A extends ArgSchema> = A extends ArgSchema ? ExtractSchemaValue<A> : never;
|
|
650
|
+
/**
|
|
651
|
+
* The value type of one schema, which {@link ExtractOptionValue} takes for each schema of a union.
|
|
652
|
+
*/
|
|
653
|
+
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;
|
|
535
654
|
type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T[] : T;
|
|
536
655
|
/**
|
|
537
656
|
* Resolved argument values.
|
|
538
657
|
*
|
|
539
|
-
*
|
|
540
|
-
*
|
|
541
|
-
*
|
|
542
|
-
*
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
/**
|
|
546
|
-
* Filters the arguments based on their default values.
|
|
658
|
+
* Each argument is mapped as optional, and the ones that are present, as {@link IsPresentArg}
|
|
659
|
+
* says, are mapped again as present. The optional mapping types a present argument as `unknown`:
|
|
660
|
+
* TypeScript does not reduce an intersection such as
|
|
661
|
+
* `(string | number[] | undefined) & (string | number[])`, of a value type that is a union with an
|
|
662
|
+
* object type. It maps all the keys without renaming them, which lets TypeScript relate the values
|
|
663
|
+
* of related type parameters, so that a conversion such as `values as ArgValues<A>` compiles.
|
|
547
664
|
*
|
|
548
|
-
* @typeParam A - {@link
|
|
665
|
+
* @typeParam A - {@link Arguments | Args} which is an object that defines the command line arguments.
|
|
549
666
|
* @typeParam V - Resolvable argument values.
|
|
550
|
-
* @typeParam K - Key of the {@link ArgSchema | argument schema} to filter by.
|
|
551
667
|
*
|
|
552
668
|
* @internal
|
|
553
669
|
*/
|
|
554
|
-
type
|
|
670
|
+
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;
|
|
555
671
|
/**
|
|
556
|
-
*
|
|
557
|
-
*
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
*
|
|
561
|
-
* @internal
|
|
672
|
+
* Whether an argument with the schema `S` has a value: with a default, with `required: true` or as
|
|
673
|
+
* a required positional argument. For a union of schemas, only when each schema of the union has
|
|
674
|
+
* one. A `default` or `required` typed as `any`, and a schema typed as `any`, count as giving a
|
|
675
|
+
* value, as {@link FilterArgs} does.
|
|
562
676
|
*/
|
|
563
|
-
type
|
|
564
|
-
type IsRequiredPositionalArg<A extends ArgSchema> = A['type'] extends 'positional' ? A['multiple'] extends true ? A['required'] extends true ? true : false : A
|
|
677
|
+
type IsPresentArg<S extends ArgSchema> = S extends ArgSchema ? true extends (S['default'] extends {} ? true : S['required'] extends true ? true : false) ? true : IsRequiredPositionalArg<S> : never;
|
|
678
|
+
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;
|
|
565
679
|
/**
|
|
566
|
-
* An
|
|
680
|
+
* Whether the `required` of `A` may be `false`. An optional `required` of type `boolean`, as in
|
|
681
|
+
* {@link ArgSchema}, says nothing, and is read as missing, so that a positional argument with it is
|
|
682
|
+
* required. A `required` that `A` always has may be `false` when its type is `boolean` or
|
|
683
|
+
* `boolean | undefined`.
|
|
567
684
|
*/
|
|
568
|
-
|
|
685
|
+
type MayBeUnrequired<A extends ArgSchema> = false extends A['required'] ? [A] extends [{
|
|
686
|
+
required: unknown;
|
|
687
|
+
}] ? true : boolean | undefined extends A['required'] ? false : true : false;
|
|
569
688
|
/**
|
|
570
689
|
* Tracks which arguments were explicitly provided by the user.
|
|
571
690
|
*
|
|
572
|
-
* Each property
|
|
573
|
-
*
|
|
691
|
+
* Each property is `true` when the argument is given on the command line, and `false` when it is
|
|
692
|
+
* not, whether it has a default or not. An option is given when one of its forms is, such as
|
|
693
|
+
* `--port`, `-p` or `--no-color`, even if its value is missing or rejected: the error is reported,
|
|
694
|
+
* and if no value from the command line is used, its default, if any, fills in the value. A
|
|
695
|
+
* positional argument is given when a positional value is used for it, even if `parse` rejects
|
|
696
|
+
* the value: the error is reported, and its default is not used.
|
|
574
697
|
*
|
|
575
698
|
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
576
699
|
*/
|
|
577
|
-
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean };
|
|
700
|
+
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean; };
|
|
578
701
|
//#endregion
|
|
579
702
|
//#region ../gunshi/src/plugin/context.d.ts
|
|
580
703
|
/**
|
|
@@ -708,7 +831,7 @@ type Awaitable<T> = T | Promise<T>;
|
|
|
708
831
|
*
|
|
709
832
|
* @typeParam T - The type to be prettified.
|
|
710
833
|
*/
|
|
711
|
-
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
834
|
+
type Prettify<T> = { [K in keyof T]: T[K]; } & {};
|
|
712
835
|
/**
|
|
713
836
|
* Extend command context type. This type is used to extend the command context with additional properties at {@linkcode CommandContext.extensions}.
|
|
714
837
|
*
|
|
@@ -884,7 +1007,11 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
|
|
|
884
1007
|
*/
|
|
885
1008
|
usageOptionValue: boolean;
|
|
886
1009
|
/**
|
|
887
|
-
* Whether to
|
|
1010
|
+
* Whether to keep usage, header, version, and validation-error text off the terminal
|
|
1011
|
+
* (they are still returned as strings) and to silence {@linkcode CommandContext.log}.
|
|
1012
|
+
*
|
|
1013
|
+
* Output that must still appear — machine-readable results for an agent, warnings for the
|
|
1014
|
+
* CLI author — should use `console.log` / `console.warn`, not `ctx.log`.
|
|
888
1015
|
*
|
|
889
1016
|
* @default false
|
|
890
1017
|
* @see {@linkcode CliOptions.usageSilent}
|
|
@@ -903,6 +1030,26 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
|
|
|
903
1030
|
* @see {@linkcode CliOptions.subCommands}
|
|
904
1031
|
*/
|
|
905
1032
|
subCommands: Map<string, Command<any> | LazyCommand<any>> | undefined;
|
|
1033
|
+
/**
|
|
1034
|
+
* The entry command of the CLI, marked with `entry: true`.
|
|
1035
|
+
*
|
|
1036
|
+
* It is available whether or not the CLI has sub-commands, while {@linkcode CommandEnvironment.subCommands}
|
|
1037
|
+
* includes the entry command only when the sub-commands are given with {@linkcode CliOptions.subCommands}.
|
|
1038
|
+
* It is not frozen, like the commands in {@linkcode CommandEnvironment.subCommands}.
|
|
1039
|
+
*/
|
|
1040
|
+
entryCommand?: Command<any> | LazyCommand<any>;
|
|
1041
|
+
/**
|
|
1042
|
+
* The global options that are in effect for the command being executed.
|
|
1043
|
+
*
|
|
1044
|
+
* A plugin registers a global option with `addGlobalOption`, and gunshi merges it into the arguments
|
|
1045
|
+
* of the command that runs. An argument that the command declares under the same name replaces it,
|
|
1046
|
+
* and the option is then not in effect: the value under that name is the command's, and the plugin
|
|
1047
|
+
* that registered it must not read it as its own.
|
|
1048
|
+
*
|
|
1049
|
+
* The schema is the one in effect, which may have given up its short name to an argument of the
|
|
1050
|
+
* command that claims the same letter.
|
|
1051
|
+
*/
|
|
1052
|
+
globalOptions?: ReadonlyMap<string, ArgSchema>;
|
|
906
1053
|
/**
|
|
907
1054
|
* Render function the command usage.
|
|
908
1055
|
*/
|
|
@@ -981,7 +1128,13 @@ interface CliOptions<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
|
981
1128
|
*/
|
|
982
1129
|
usageOptionValue?: boolean;
|
|
983
1130
|
/**
|
|
984
|
-
* Whether to
|
|
1131
|
+
* Whether to keep usage, header, version, and validation-error text off the terminal
|
|
1132
|
+
* (they are still returned as strings) and to silence {@linkcode CommandContext.log}.
|
|
1133
|
+
*
|
|
1134
|
+
* Output that must still appear — machine-readable results for an agent, warnings for the
|
|
1135
|
+
* CLI author — should use `console.log` / `console.warn`, not `ctx.log`.
|
|
1136
|
+
*
|
|
1137
|
+
* @default false
|
|
985
1138
|
*/
|
|
986
1139
|
usageSilent?: boolean;
|
|
987
1140
|
/**
|
|
@@ -1396,7 +1549,7 @@ type CommandLoader<G extends GunshiParamsConstraint = DefaultGunshiParams> = ()
|
|
|
1396
1549
|
*
|
|
1397
1550
|
* @internal
|
|
1398
1551
|
*/
|
|
1399
|
-
type ExtractExtensionValues<E extends Record<string, CommandContextExtension>> = { [K in keyof E]: E[K] extends CommandContextExtension<infer T> ? T : never };
|
|
1552
|
+
type ExtractExtensionValues<E extends Record<string, CommandContextExtension>> = { [K in keyof E]: E[K] extends CommandContextExtension<infer T> ? T : never; };
|
|
1400
1553
|
/**
|
|
1401
1554
|
* Return type of {@link createCommandContext}
|
|
1402
1555
|
*
|
|
@@ -1477,22 +1630,7 @@ interface CommandContextParams<G extends GunshiParams | {
|
|
|
1477
1630
|
* @param param - A {@link CommandContextParams | parameters} to create a command context.
|
|
1478
1631
|
* @returns A {@link CommandContext | command context}, which is readonly.
|
|
1479
1632
|
*/
|
|
1480
|
-
declare function createCommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams, V extends ArgValues<ExtractArgs<G>> = ArgValues<ExtractArgs<G>>, C extends Command<G> | LazyCommand<G> = Command<G>, E extends Record<string, CommandContextExtension> = {}>({
|
|
1481
|
-
args,
|
|
1482
|
-
explicit,
|
|
1483
|
-
values,
|
|
1484
|
-
positionals,
|
|
1485
|
-
rest,
|
|
1486
|
-
argv,
|
|
1487
|
-
tokens,
|
|
1488
|
-
command,
|
|
1489
|
-
extensions,
|
|
1490
|
-
cliOptions,
|
|
1491
|
-
callMode,
|
|
1492
|
-
commandPath,
|
|
1493
|
-
omitted,
|
|
1494
|
-
validationError
|
|
1495
|
-
}: CommandContextParams<G, V, C, E>): Promise<CommandContextResult<G, E>>;
|
|
1633
|
+
declare function createCommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams, V extends ArgValues<ExtractArgs<G>> = ArgValues<ExtractArgs<G>>, C extends Command<G> | LazyCommand<G> = Command<G>, E extends Record<string, CommandContextExtension> = {}>({ args, explicit, values, positionals, rest, argv, tokens, command, extensions, cliOptions, callMode, commandPath, omitted, validationError }: CommandContextParams<G, V, C, E>): Promise<CommandContextResult<G, E>>;
|
|
1496
1634
|
//#endregion
|
|
1497
1635
|
//#region ../gunshi/src/definition.d.ts
|
|
1498
1636
|
/**
|
|
@@ -1643,7 +1781,8 @@ declare function lazy<A extends Args>(loader: CommandLoader<{
|
|
|
1643
1781
|
* @typeParam D - A partial {@link Command} definition type with required `args`
|
|
1644
1782
|
*
|
|
1645
1783
|
* @param loader - A {@link CommandLoader | command loader} function that returns a command definition
|
|
1646
|
-
* @param definition - A {@link Command | command} definition
|
|
1784
|
+
* @param definition - A {@link Command | command} definition. Its `name` may be omitted,
|
|
1785
|
+
* including when the lazy command is used as the CLI entry command.
|
|
1647
1786
|
* @returns A {@link LazyCommand | lazy command} that can be executed later
|
|
1648
1787
|
*/
|
|
1649
1788
|
declare function lazy<D extends {
|
|
@@ -1665,7 +1804,8 @@ declare function lazy<D extends {
|
|
|
1665
1804
|
* @typeParam D - A partial {@link Command} definition type
|
|
1666
1805
|
*
|
|
1667
1806
|
* @param loader - A {@link CommandLoader | command loader} function that returns a command definition
|
|
1668
|
-
* @param definition - An optional {@link Command | command} definition
|
|
1807
|
+
* @param definition - An optional {@link Command | command} definition. Its `name` may be omitted,
|
|
1808
|
+
* including when the lazy command is used as the CLI entry command.
|
|
1669
1809
|
* @returns A {@link LazyCommand | lazy command} that can be executed later
|
|
1670
1810
|
*/
|
|
1671
1811
|
declare function lazy<G extends GunshiParamsConstraint = DefaultGunshiParams, D extends Partial<Command<G>> = Partial<Command<G>>>(loader: CommandLoader<G>, definition?: D): LazyCommand<G, D>;
|
|
@@ -1712,12 +1852,5 @@ type LazyWithTypesReturn<FullG extends GunshiParamsConstraint> = <D extends Part
|
|
|
1712
1852
|
* @since v0.27.0
|
|
1713
1853
|
*/
|
|
1714
1854
|
declare function lazyWithTypes<G extends GunshiParamsConstraint>(): LazyWithTypesReturn<NormalizeToGunshiParams<G>>;
|
|
1715
|
-
//#endregion
|
|
1716
|
-
//#region src/index.d.ts
|
|
1717
|
-
/**
|
|
1718
|
-
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
1719
|
-
* @license MIT
|
|
1720
|
-
*/
|
|
1721
|
-
|
|
1722
1855
|
//#endregion
|
|
1723
1856
|
export { type ArgSchema, type ArgValues, type Args, type Command, type CommandContextParams, type CommandLoader, type CommandRunner, type DefaultGunshiParams, type ExtendContext, type GunshiParams, type LazyCommand, createCommandContext, define, defineWithTypes, lazy, lazyWithTypes };
|
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.1",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -53,11 +53,11 @@
|
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"deno": "^2.7.14",
|
|
56
|
+
"gunshi": "1.0.0-beta.1",
|
|
56
57
|
"jsr": "^0.14.3",
|
|
57
58
|
"jsr-exports-lint": "^0.4.2",
|
|
58
59
|
"publint": "^0.3.20",
|
|
59
|
-
"tsdown": "0.
|
|
60
|
-
"gunshi": "0.37.3"
|
|
60
|
+
"tsdown": "0.22.14"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "tsdown",
|