@gunshi/plugin 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 +304 -121
- package/lib/index.js +84 -8
- 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
|
|
@@ -513,6 +613,10 @@ interface ArgSchema {
|
|
|
513
613
|
*
|
|
514
614
|
* Each code identifies a validation failure category and is also suitable as an
|
|
515
615
|
* i18n resource key for localized rendering.
|
|
616
|
+
*
|
|
617
|
+
* {@link resolveArgs} and `parse()` do not report `unknownOption`. An option that is not in the
|
|
618
|
+
* schema takes the argument after it as its value, unless that argument is an option, and the value
|
|
619
|
+
* is dropped: with no `foo` in the schema, `--foo bar` gives no positional argument.
|
|
516
620
|
*/
|
|
517
621
|
declare const ArgsValidationErrorKeys: {
|
|
518
622
|
readonly requiredOption: 'err:arg:required-option';
|
|
@@ -521,6 +625,10 @@ declare const ArgsValidationErrorKeys: {
|
|
|
521
625
|
readonly invalidChoice: 'err:arg:invalid-choice';
|
|
522
626
|
readonly customParse: 'err:arg:custom-parse';
|
|
523
627
|
readonly unknownOption: 'err:arg:unknown-option';
|
|
628
|
+
readonly unexpectedValue: 'err:arg:unexpected-value';
|
|
629
|
+
readonly missingValue: 'err:arg:missing-value';
|
|
630
|
+
readonly conflict: 'err:arg:conflict';
|
|
631
|
+
readonly invalidDefault: 'err:arg:invalid-default';
|
|
524
632
|
};
|
|
525
633
|
/**
|
|
526
634
|
* A machine-readable argument validation error code.
|
|
@@ -546,7 +654,7 @@ declare class ArgsValidationError extends Error {
|
|
|
546
654
|
*
|
|
547
655
|
* This code can also be used as an i18n resource key.
|
|
548
656
|
*/
|
|
549
|
-
readonly code?: ArgsValidationErrorCode;
|
|
657
|
+
readonly code?: ArgsValidationErrorCode | undefined;
|
|
550
658
|
/**
|
|
551
659
|
* Interpolation values for `code`.
|
|
552
660
|
*/
|
|
@@ -574,62 +682,85 @@ interface Args {
|
|
|
574
682
|
/**
|
|
575
683
|
* An object that contains the values of the arguments.
|
|
576
684
|
*
|
|
685
|
+
* The value of an argument whose schema is a union, such as
|
|
686
|
+
* `strict ? multiple(integer()) : string()` with the combinators, is typed as a value of any of its
|
|
687
|
+
* schemas: `number[] | string`. The argument is typed as present only when each schema of the
|
|
688
|
+
* union gives it a value, with a default, with `required: true` or as a required positional
|
|
689
|
+
* argument.
|
|
690
|
+
*
|
|
691
|
+
* When the type of one schema covers that of the other, as the type of `integer()` covers that of
|
|
692
|
+
* `multiple(integer())`, TypeScript types a `?:` expression of them as the covering schema alone:
|
|
693
|
+
* the value of `strict ? multiple(integer()) : integer()` is typed without the array, and
|
|
694
|
+
* `strict ? multiple(positional()) : positional()` as present, although it may be missing. Give
|
|
695
|
+
* each schema its own argument instead, or give the expression the type of both schemas with `as`,
|
|
696
|
+
* as in `(strict ? many : one) as typeof many | typeof one` for `const many = multiple(integer())`
|
|
697
|
+
* and `const one = integer()`.
|
|
698
|
+
*
|
|
577
699
|
* @typeParam T - {@link Args | Arguments} which is an object that defines the command line arguments.
|
|
578
700
|
*/
|
|
579
|
-
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]
|
|
701
|
+
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]>; }> : {
|
|
580
702
|
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
581
703
|
};
|
|
582
704
|
/**
|
|
583
705
|
* Extracts the value type from the argument schema.
|
|
706
|
+
* For a union of schemas, it is the union of the value types of its schemas.
|
|
584
707
|
*
|
|
585
708
|
* @typeParam A - {@link ArgSchema | Argument schema} which is an object that defines command line arguments.
|
|
586
709
|
*
|
|
587
710
|
* @internal
|
|
588
711
|
*/
|
|
589
|
-
type ExtractOptionValue<A extends ArgSchema> =
|
|
712
|
+
type ExtractOptionValue<A extends ArgSchema> = A extends ArgSchema ? ExtractSchemaValue<A> : never;
|
|
713
|
+
/**
|
|
714
|
+
* The value type of one schema, which {@link ExtractOptionValue} takes for each schema of a union.
|
|
715
|
+
*/
|
|
716
|
+
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;
|
|
590
717
|
type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T[] : T;
|
|
591
718
|
/**
|
|
592
719
|
* Resolved argument values.
|
|
593
720
|
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
/**
|
|
601
|
-
* Filters the arguments based on their default values.
|
|
721
|
+
* Each argument is mapped as optional, and the ones that are present, as {@link IsPresentArg}
|
|
722
|
+
* says, are mapped again as present. The optional mapping types a present argument as `unknown`:
|
|
723
|
+
* TypeScript does not reduce an intersection such as
|
|
724
|
+
* `(string | number[] | undefined) & (string | number[])`, of a value type that is a union with an
|
|
725
|
+
* object type. It maps all the keys without renaming them, which lets TypeScript relate the values
|
|
726
|
+
* of related type parameters, so that a conversion such as `values as ArgValues<A>` compiles.
|
|
602
727
|
*
|
|
603
|
-
* @typeParam A - {@link
|
|
728
|
+
* @typeParam A - {@link Arguments | Args} which is an object that defines the command line arguments.
|
|
604
729
|
* @typeParam V - Resolvable argument values.
|
|
605
|
-
* @typeParam K - Key of the {@link ArgSchema | argument schema} to filter by.
|
|
606
730
|
*
|
|
607
731
|
* @internal
|
|
608
732
|
*/
|
|
609
|
-
type
|
|
733
|
+
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;
|
|
610
734
|
/**
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
614
|
-
*
|
|
615
|
-
*
|
|
616
|
-
* @internal
|
|
735
|
+
* Whether an argument with the schema `S` has a value: with a default, with `required: true` or as
|
|
736
|
+
* a required positional argument. For a union of schemas, only when each schema of the union has
|
|
737
|
+
* one. A `default` or `required` typed as `any`, and a schema typed as `any`, count as giving a
|
|
738
|
+
* value, as {@link FilterArgs} does.
|
|
617
739
|
*/
|
|
618
|
-
type
|
|
619
|
-
type IsRequiredPositionalArg<A extends ArgSchema> = A['type'] extends 'positional' ? A['multiple'] extends true ? A['required'] extends true ? true : false : A
|
|
740
|
+
type IsPresentArg<S extends ArgSchema> = S extends ArgSchema ? true extends (S['default'] extends {} ? true : S['required'] extends true ? true : false) ? true : IsRequiredPositionalArg<S> : never;
|
|
741
|
+
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;
|
|
620
742
|
/**
|
|
621
|
-
* An
|
|
743
|
+
* Whether the `required` of `A` may be `false`. An optional `required` of type `boolean`, as in
|
|
744
|
+
* {@link ArgSchema}, says nothing, and is read as missing, so that a positional argument with it is
|
|
745
|
+
* required. A `required` that `A` always has may be `false` when its type is `boolean` or
|
|
746
|
+
* `boolean | undefined`.
|
|
622
747
|
*/
|
|
623
|
-
|
|
748
|
+
type MayBeUnrequired<A extends ArgSchema> = false extends A['required'] ? [A] extends [{
|
|
749
|
+
required: unknown;
|
|
750
|
+
}] ? true : boolean | undefined extends A['required'] ? false : true : false;
|
|
624
751
|
/**
|
|
625
752
|
* Tracks which arguments were explicitly provided by the user.
|
|
626
753
|
*
|
|
627
|
-
* Each property
|
|
628
|
-
*
|
|
754
|
+
* Each property is `true` when the argument is given on the command line, and `false` when it is
|
|
755
|
+
* not, whether it has a default or not. An option is given when one of its forms is, such as
|
|
756
|
+
* `--port`, `-p` or `--no-color`, even if its value is missing or rejected: the error is reported,
|
|
757
|
+
* and if no value from the command line is used, its default, if any, fills in the value. A
|
|
758
|
+
* positional argument is given when a positional value is used for it, even if `parse` rejects
|
|
759
|
+
* the value: the error is reported, and its default is not used.
|
|
629
760
|
*
|
|
630
761
|
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
631
762
|
*/
|
|
632
|
-
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean };
|
|
763
|
+
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean; };
|
|
633
764
|
//#endregion
|
|
634
765
|
//#region ../gunshi/src/plugin/context.d.ts
|
|
635
766
|
/**
|
|
@@ -710,12 +841,12 @@ interface PluginContext<G extends GunshiParamsConstraint = DefaultGunshiParams>
|
|
|
710
841
|
}
|
|
711
842
|
//#endregion
|
|
712
843
|
//#region ../gunshi/src/plugin/core.d.ts
|
|
713
|
-
type ProcessDependency<D, A extends ExtendContext> = D extends string ? D extends keyof A ? { [K in D]: A[K] } : {} : D extends {
|
|
844
|
+
type ProcessDependency<D, A extends ExtendContext> = D extends string ? D extends keyof A ? { [K in D]: A[K]; } : {} : D extends {
|
|
714
845
|
id: infer ID;
|
|
715
846
|
optional?: any;
|
|
716
847
|
} ? ID extends string ? ID extends keyof A ? D extends {
|
|
717
848
|
optional: true;
|
|
718
|
-
} ? { [K in ID]: A[K] | undefined } : { [K in ID]: A[K] } : {} : never : never;
|
|
849
|
+
} ? { [K in ID]: A[K] | undefined; } : { [K in ID]: A[K]; } : {} : never : never;
|
|
719
850
|
/**
|
|
720
851
|
* Helper type to infer dependency extensions with optional support
|
|
721
852
|
*
|
|
@@ -771,7 +902,7 @@ type PluginExtension<T = Record<string, unknown>, G extends GunshiParams = Defau
|
|
|
771
902
|
*/
|
|
772
903
|
type OnPluginExtension<G extends GunshiParams = DefaultGunshiParams> = (ctx: Readonly<CommandContext<G>>, cmd: Readonly<Command<G>>) => Awaitable<void>;
|
|
773
904
|
type IsStringLiteral<S extends string> = string extends S ? false : true;
|
|
774
|
-
type MergeExtension<Id, ResolvedDepExt extends ExtendContext, PluginExt extends ExtendContext> = Id extends infer I ? I extends string ? IsStringLiteral<I> extends true ? ResolvedDepExt & { [K in I]: PluginExt } : ResolvedDepExt : ResolvedDepExt : ResolvedDepExt;
|
|
905
|
+
type MergeExtension<Id, ResolvedDepExt extends ExtendContext, PluginExt extends ExtendContext> = Id extends (infer I) ? I extends string ? IsStringLiteral<I> extends true ? ResolvedDepExt & { [K in I]: PluginExt; } : ResolvedDepExt : ResolvedDepExt : ResolvedDepExt;
|
|
775
906
|
/**
|
|
776
907
|
* Extensions resolved from the declared plugin dependencies.
|
|
777
908
|
*
|
|
@@ -807,17 +938,15 @@ type MergedPluginParams<Id extends string, Deps extends ReadonlyArray<PluginDepe
|
|
|
807
938
|
*
|
|
808
939
|
* @since v0.27.0
|
|
809
940
|
*/
|
|
810
|
-
interface PluginOptions<DepExt extends ExtendContext = DefaultGunshiParams['extensions']
|
|
811
|
-
// for plugin
|
|
812
|
-
|
|
813
|
-
// for plugin
|
|
814
|
-
Deps extends
|
|
815
|
-
// for plugin dependencies
|
|
816
|
-
Ext extends Record<string, unknown> = {},
|
|
817
|
-
// for plugin extension type
|
|
818
|
-
ResolvedDepExt extends GunshiParams = DependencyParams<Deps, DepExt>, PluginExt extends PluginExtension<Ext, ResolvedDepExt> = PluginExtension<Ext, ResolvedDepExt>, MergedExt extends GunshiParams = MergedPluginParams<Id, Deps, DepExt, Awaited<ReturnType<PluginExt>>>> {
|
|
941
|
+
interface PluginOptions<DepExt extends ExtendContext = DefaultGunshiParams['extensions'] // for plugin dependency extensions
|
|
942
|
+
, Id extends string = string // for plugin id
|
|
943
|
+
, Deps extends ReadonlyArray<PluginDependency | string> = (PluginDependency | string)[] // for plugin dependencies
|
|
944
|
+
, Ext extends Record<string, unknown> = {} // for plugin extension type
|
|
945
|
+
, ResolvedDepExt extends GunshiParams = DependencyParams<Deps, DepExt>, PluginExt extends PluginExtension<Ext, ResolvedDepExt> = PluginExtension<Ext, ResolvedDepExt>, MergedExt extends GunshiParams = MergedPluginParams<Id, Deps, DepExt, Awaited<ReturnType<PluginExt>>>> {
|
|
819
946
|
/**
|
|
820
|
-
* Plugin unique identifier
|
|
947
|
+
* Plugin unique identifier.
|
|
948
|
+
*
|
|
949
|
+
* Must be a non-empty string. {@linkcode plugin} and the installer both reject an empty id.
|
|
821
950
|
*/
|
|
822
951
|
id: Id;
|
|
823
952
|
/**
|
|
@@ -916,15 +1045,11 @@ interface PluginWithoutExtension<E extends GunshiParams['extensions'] = DefaultG
|
|
|
916
1045
|
*
|
|
917
1046
|
* @since v0.27.0
|
|
918
1047
|
*/
|
|
919
|
-
declare function plugin<Context extends ExtendContext = DefaultGunshiParams['extensions']
|
|
920
|
-
// for plugin
|
|
921
|
-
|
|
922
|
-
// for plugin
|
|
923
|
-
Deps extends
|
|
924
|
-
// for plugin dependencies
|
|
925
|
-
Extension extends {} = {},
|
|
926
|
-
// for plugin extension type
|
|
927
|
-
ResolvedDepExtensions extends GunshiParams = DependencyParams<Deps, Context>, PluginExt extends PluginExtension<Extension, DefaultGunshiParams> = PluginExtension<Extension, ResolvedDepExtensions>, MergedExtensions extends GunshiParams = MergedPluginParams<Id, Deps, Context, Awaited<ReturnType<PluginExt>>>>(options: {
|
|
1048
|
+
declare function plugin<Context extends ExtendContext = DefaultGunshiParams['extensions'] // for plugin dependency extensions
|
|
1049
|
+
, Id extends string = string // for plugin id
|
|
1050
|
+
, Deps extends ReadonlyArray<PluginDependency | string> = [] // for plugin dependencies
|
|
1051
|
+
, Extension extends {} = {} // for plugin extension type
|
|
1052
|
+
, ResolvedDepExtensions extends GunshiParams = DependencyParams<Deps, Context>, PluginExt extends PluginExtension<Extension, DefaultGunshiParams> = PluginExtension<Extension, ResolvedDepExtensions>, MergedExtensions extends GunshiParams = MergedPluginParams<Id, Deps, Context, Awaited<ReturnType<PluginExt>>>>(options: {
|
|
928
1053
|
id: Id;
|
|
929
1054
|
name?: string;
|
|
930
1055
|
dependencies?: Deps;
|
|
@@ -945,15 +1070,11 @@ ResolvedDepExtensions extends GunshiParams = DependencyParams<Deps, Context>, Pl
|
|
|
945
1070
|
*
|
|
946
1071
|
* @since v0.27.0
|
|
947
1072
|
*/
|
|
948
|
-
declare function plugin<Context extends ExtendContext = DefaultGunshiParams['extensions']
|
|
949
|
-
// for plugin
|
|
950
|
-
|
|
951
|
-
// for plugin
|
|
952
|
-
Deps extends
|
|
953
|
-
// for plugin dependencies
|
|
954
|
-
Extension extends Record<string, unknown> = {},
|
|
955
|
-
// for plugin extension type
|
|
956
|
-
ResolvedDepExtensions extends GunshiParams = DependencyParams<Deps, Context>, PluginExt extends PluginExtension<Extension, DefaultGunshiParams> = PluginExtension<Extension, ResolvedDepExtensions>, MergedExtensions extends GunshiParams = MergedPluginParams<Id, Deps, Context, Awaited<ReturnType<PluginExt>>>>(options: {
|
|
1073
|
+
declare function plugin<Context extends ExtendContext = DefaultGunshiParams['extensions'] // for plugin dependency extensions
|
|
1074
|
+
, Id extends string = string // for plugin id
|
|
1075
|
+
, Deps extends ReadonlyArray<PluginDependency | string> = [] // for plugin dependencies
|
|
1076
|
+
, Extension extends Record<string, unknown> = {} // for plugin extension type
|
|
1077
|
+
, ResolvedDepExtensions extends GunshiParams = DependencyParams<Deps, Context>, PluginExt extends PluginExtension<Extension, DefaultGunshiParams> = PluginExtension<Extension, ResolvedDepExtensions>, MergedExtensions extends GunshiParams = MergedPluginParams<Id, Deps, Context, Awaited<ReturnType<PluginExt>>>>(options: {
|
|
957
1078
|
id: Id;
|
|
958
1079
|
name?: string;
|
|
959
1080
|
dependencies?: Deps;
|
|
@@ -973,7 +1094,7 @@ type Awaitable<T> = T | Promise<T>;
|
|
|
973
1094
|
*
|
|
974
1095
|
* @typeParam T - The type to be prettified.
|
|
975
1096
|
*/
|
|
976
|
-
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
1097
|
+
type Prettify<T> = { [K in keyof T]: T[K]; } & {};
|
|
977
1098
|
/**
|
|
978
1099
|
* Extend command context type. This type is used to extend the command context with additional properties at {@linkcode CommandContext.extensions}.
|
|
979
1100
|
*
|
|
@@ -1149,7 +1270,11 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
|
|
|
1149
1270
|
*/
|
|
1150
1271
|
usageOptionValue: boolean;
|
|
1151
1272
|
/**
|
|
1152
|
-
* Whether to
|
|
1273
|
+
* Whether to keep usage, header, version, and validation-error text off the terminal
|
|
1274
|
+
* (they are still returned as strings) and to silence {@linkcode CommandContext.log}.
|
|
1275
|
+
*
|
|
1276
|
+
* Output that must still appear — machine-readable results for an agent, warnings for the
|
|
1277
|
+
* CLI author — should use `console.log` / `console.warn`, not `ctx.log`.
|
|
1153
1278
|
*
|
|
1154
1279
|
* @default false
|
|
1155
1280
|
* @see {@linkcode CliOptions.usageSilent}
|
|
@@ -1168,6 +1293,26 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
|
|
|
1168
1293
|
* @see {@linkcode CliOptions.subCommands}
|
|
1169
1294
|
*/
|
|
1170
1295
|
subCommands: Map<string, Command<any> | LazyCommand<any>> | undefined;
|
|
1296
|
+
/**
|
|
1297
|
+
* The entry command of the CLI, marked with `entry: true`.
|
|
1298
|
+
*
|
|
1299
|
+
* It is available whether or not the CLI has sub-commands, while {@linkcode CommandEnvironment.subCommands}
|
|
1300
|
+
* includes the entry command only when the sub-commands are given with {@linkcode CliOptions.subCommands}.
|
|
1301
|
+
* It is not frozen, like the commands in {@linkcode CommandEnvironment.subCommands}.
|
|
1302
|
+
*/
|
|
1303
|
+
entryCommand?: Command<any> | LazyCommand<any>;
|
|
1304
|
+
/**
|
|
1305
|
+
* The global options that are in effect for the command being executed.
|
|
1306
|
+
*
|
|
1307
|
+
* A plugin registers a global option with `addGlobalOption`, and gunshi merges it into the arguments
|
|
1308
|
+
* of the command that runs. An argument that the command declares under the same name replaces it,
|
|
1309
|
+
* and the option is then not in effect: the value under that name is the command's, and the plugin
|
|
1310
|
+
* that registered it must not read it as its own.
|
|
1311
|
+
*
|
|
1312
|
+
* The schema is the one in effect, which may have given up its short name to an argument of the
|
|
1313
|
+
* command that claims the same letter.
|
|
1314
|
+
*/
|
|
1315
|
+
globalOptions?: ReadonlyMap<string, ArgSchema>;
|
|
1171
1316
|
/**
|
|
1172
1317
|
* Render function the command usage.
|
|
1173
1318
|
*/
|
|
@@ -1246,7 +1391,13 @@ interface CliOptions<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
|
1246
1391
|
*/
|
|
1247
1392
|
usageOptionValue?: boolean;
|
|
1248
1393
|
/**
|
|
1249
|
-
* Whether to
|
|
1394
|
+
* Whether to keep usage, header, version, and validation-error text off the terminal
|
|
1395
|
+
* (they are still returned as strings) and to silence {@linkcode CommandContext.log}.
|
|
1396
|
+
*
|
|
1397
|
+
* Output that must still appear — machine-readable results for an agent, warnings for the
|
|
1398
|
+
* CLI author — should use `console.log` / `console.warn`, not `ctx.log`.
|
|
1399
|
+
*
|
|
1400
|
+
* @default false
|
|
1250
1401
|
*/
|
|
1251
1402
|
usageSilent?: boolean;
|
|
1252
1403
|
/**
|
|
@@ -1696,7 +1847,7 @@ declare const CLI_OPTIONS_DEFAULT: CliOptions<DefaultGunshiParams>;
|
|
|
1696
1847
|
*
|
|
1697
1848
|
* @internal
|
|
1698
1849
|
*/
|
|
1699
|
-
type ExtractExtensionValues<E extends Record<string, CommandContextExtension>> = { [K in keyof E]: E[K] extends CommandContextExtension<infer T> ? T : never };
|
|
1850
|
+
type ExtractExtensionValues<E extends Record<string, CommandContextExtension>> = { [K in keyof E]: E[K] extends CommandContextExtension<infer T> ? T : never; };
|
|
1700
1851
|
/**
|
|
1701
1852
|
* Return type of {@link createCommandContext}
|
|
1702
1853
|
*
|
|
@@ -1777,22 +1928,7 @@ interface CommandContextParams<G extends GunshiParams | {
|
|
|
1777
1928
|
* @param param - A {@link CommandContextParams | parameters} to create a command context.
|
|
1778
1929
|
* @returns A {@link CommandContext | command context}, which is readonly.
|
|
1779
1930
|
*/
|
|
1780
|
-
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> = {}>({
|
|
1781
|
-
args,
|
|
1782
|
-
explicit,
|
|
1783
|
-
values,
|
|
1784
|
-
positionals,
|
|
1785
|
-
rest,
|
|
1786
|
-
argv,
|
|
1787
|
-
tokens,
|
|
1788
|
-
command,
|
|
1789
|
-
extensions,
|
|
1790
|
-
cliOptions,
|
|
1791
|
-
callMode,
|
|
1792
|
-
commandPath,
|
|
1793
|
-
omitted,
|
|
1794
|
-
validationError
|
|
1795
|
-
}: CommandContextParams<G, V, C, E>): Promise<CommandContextResult<G, E>>;
|
|
1931
|
+
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>>;
|
|
1796
1932
|
//#endregion
|
|
1797
1933
|
//#region ../gunshi/src/error.d.ts
|
|
1798
1934
|
/**
|
|
@@ -1805,6 +1941,20 @@ declare const CommandNotFoundErrorKeys: {
|
|
|
1805
1941
|
* Command not found error code.
|
|
1806
1942
|
*/
|
|
1807
1943
|
type CommandNotFoundErrorCode = (typeof CommandNotFoundErrorKeys)[keyof typeof CommandNotFoundErrorKeys];
|
|
1944
|
+
/**
|
|
1945
|
+
* Command resolution error resource keys.
|
|
1946
|
+
*
|
|
1947
|
+
* These errors describe an ambiguity or inconsistency while choosing a command from arguments.
|
|
1948
|
+
* They are kept separate from {@link CommandNotFoundError} so consumers can distinguish a typo
|
|
1949
|
+
* from an input whose command interpretation is not unique.
|
|
1950
|
+
*/
|
|
1951
|
+
declare const CommandResolutionErrorKeys: {
|
|
1952
|
+
readonly ambiguous: "err:cmd:ambiguous";
|
|
1953
|
+
readonly inconsistentOptions: "err:cmd:inconsistent-options";
|
|
1954
|
+
readonly lazySchemaMismatch: "err:cmd:lazy-schema-mismatch";
|
|
1955
|
+
};
|
|
1956
|
+
/** Command resolution error code. */
|
|
1957
|
+
type CommandResolutionErrorCode = (typeof CommandResolutionErrorKeys)[keyof typeof CommandResolutionErrorKeys];
|
|
1808
1958
|
/**
|
|
1809
1959
|
* Options for {@link CommandNotFoundError}.
|
|
1810
1960
|
*/
|
|
@@ -1834,6 +1984,19 @@ type CommandNotFoundErrorOptions = {
|
|
|
1834
1984
|
*/
|
|
1835
1985
|
cause?: unknown;
|
|
1836
1986
|
};
|
|
1987
|
+
/** Options for {@link CommandResolutionError}. */
|
|
1988
|
+
type CommandResolutionErrorOptions = {
|
|
1989
|
+
/** Localization resource key for the resolution failure. */
|
|
1990
|
+
code?: CommandResolutionErrorCode;
|
|
1991
|
+
/** Values used when localizing the message. */
|
|
1992
|
+
values?: Record<string, unknown>;
|
|
1993
|
+
/** Command path at which the resolution failed. */
|
|
1994
|
+
commandPath?: readonly string[];
|
|
1995
|
+
/** Candidate command paths considered by the resolver. */
|
|
1996
|
+
candidatePaths?: readonly (readonly string[])[];
|
|
1997
|
+
/** Underlying cause. */
|
|
1998
|
+
cause?: unknown;
|
|
1999
|
+
};
|
|
1837
2000
|
/**
|
|
1838
2001
|
* Error raised when a command cannot be resolved.
|
|
1839
2002
|
*
|
|
@@ -1855,6 +2018,26 @@ declare class CommandNotFoundError extends Error {
|
|
|
1855
2018
|
*/
|
|
1856
2019
|
constructor(message: string, options: CommandNotFoundErrorOptions);
|
|
1857
2020
|
}
|
|
2021
|
+
/**
|
|
2022
|
+
* Error raised when command routing cannot produce one consistent command.
|
|
2023
|
+
*
|
|
2024
|
+
* The own registry brand is the cross-bundle contract. Unlike the legacy command-not-found
|
|
2025
|
+
* guard, the new guard deliberately has no name-based fallback because this error type is new and
|
|
2026
|
+
* must not classify arbitrary errors from older versions.
|
|
2027
|
+
*/
|
|
2028
|
+
declare class CommandResolutionError extends Error {
|
|
2029
|
+
readonly code?: CommandResolutionErrorCode;
|
|
2030
|
+
readonly values: Record<string, unknown>;
|
|
2031
|
+
readonly commandPath: readonly string[];
|
|
2032
|
+
readonly candidatePaths: readonly (readonly string[])[];
|
|
2033
|
+
/**
|
|
2034
|
+
* Create a command resolution error with structured routing metadata.
|
|
2035
|
+
*
|
|
2036
|
+
* @param message - Human-readable fallback message
|
|
2037
|
+
* @param options - Error code and command-routing metadata
|
|
2038
|
+
*/
|
|
2039
|
+
constructor(message: string, options?: CommandResolutionErrorOptions);
|
|
2040
|
+
}
|
|
1858
2041
|
/**
|
|
1859
2042
|
* Check whether an error is a {@link CommandNotFoundError}.
|
|
1860
2043
|
*
|
|
@@ -1867,6 +2050,13 @@ declare class CommandNotFoundError extends Error {
|
|
|
1867
2050
|
* @returns `true` if the error is a {@link CommandNotFoundError}
|
|
1868
2051
|
*/
|
|
1869
2052
|
declare function isCommandNotFoundError(error: unknown): error is CommandNotFoundError;
|
|
2053
|
+
/**
|
|
2054
|
+
* Check whether an error is a {@link CommandResolutionError}.
|
|
2055
|
+
*
|
|
2056
|
+
* @param error - An unknown error
|
|
2057
|
+
* @returns `true` when the error has the command-resolution brand and shape
|
|
2058
|
+
*/
|
|
2059
|
+
declare function isCommandResolutionError(error: unknown): error is CommandResolutionError;
|
|
1870
2060
|
/**
|
|
1871
2061
|
* Check whether an error is an {@link ArgsValidationError}.
|
|
1872
2062
|
*
|
|
@@ -1894,11 +2084,4 @@ declare function isArgsValidationError(error: unknown): error is ArgsValidationE
|
|
|
1894
2084
|
*/
|
|
1895
2085
|
declare function hasPriorityValidationError(error: AggregateError | undefined): boolean;
|
|
1896
2086
|
//#endregion
|
|
1897
|
-
|
|
1898
|
-
/**
|
|
1899
|
-
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
1900
|
-
* @license MIT
|
|
1901
|
-
*/
|
|
1902
|
-
|
|
1903
|
-
//#endregion
|
|
1904
|
-
export { ANONYMOUS_COMMAND_NAME, type ArgSchema, type ArgToken, type ArgValues, type Args, ArgsValidationError, ArgsValidationErrorCode, ArgsValidationErrorKeys, Awaitable, CLI_OPTIONS_DEFAULT, Command, CommandContext, CommandContextCore, CommandContextExtension, CommandContextParams, CommandDecorator, CommandExamplesFetcher, CommandNotFoundError, CommandNotFoundErrorCode, CommandNotFoundErrorKeys, CommandNotFoundErrorOptions, CommandRunner, DefaultGunshiParams, ExtendContext, ExtractArgs, ExtractExtensions, GunshiParams, GunshiParamsConstraint, LazyCommand, MergeGunshiExtensions, NormalizeToGunshiParams, OnPluginExtension, Plugin, PluginContext, PluginDependency, PluginExtension, PluginFunction, PluginOptions, PluginWithExtension, PluginWithoutExtension, Prettify, RendererDecorator, ValidationErrorsDecorator, createCommandContext, hasPriorityValidationError, isArgsValidationError, isCommandNotFoundError, plugin };
|
|
2087
|
+
export { ANONYMOUS_COMMAND_NAME, type ArgSchema, type ArgToken, type ArgValues, type Args, ArgsValidationError, type ArgsValidationErrorCode, ArgsValidationErrorKeys, type Awaitable, CLI_OPTIONS_DEFAULT, type Command, type CommandContext, type CommandContextCore, type CommandContextExtension, type CommandContextParams, type CommandDecorator, type CommandExamplesFetcher, CommandNotFoundError, type CommandNotFoundErrorCode, CommandNotFoundErrorKeys, type CommandNotFoundErrorOptions, CommandResolutionError, type CommandResolutionErrorCode, CommandResolutionErrorKeys, type CommandResolutionErrorOptions, type CommandRunner, type DefaultGunshiParams, type ExtendContext, type ExtractArgs, type ExtractExtensions, type GunshiParams, type GunshiParamsConstraint, type LazyCommand, type MergeGunshiExtensions, type NormalizeToGunshiParams, type OnPluginExtension, type Plugin, type PluginContext, type PluginDependency, type PluginExtension, type PluginFunction, type PluginOptions, type PluginWithExtension, type PluginWithoutExtension, type Prettify, type RendererDecorator, type ValidationErrorsDecorator, createCommandContext, hasPriorityValidationError, isArgsValidationError, isCommandNotFoundError, isCommandResolutionError, plugin };
|
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)";
|
|
@@ -173,7 +176,11 @@ function getCommandName(cmd) {
|
|
|
173
176
|
"-".codePointAt(0);
|
|
174
177
|
"=".codePointAt(0);
|
|
175
178
|
//#endregion
|
|
176
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
179
|
+
//#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/resolver-CAlgxe_R.js
|
|
180
|
+
/**
|
|
181
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
182
|
+
* @license MIT
|
|
183
|
+
*/
|
|
177
184
|
/**
|
|
178
185
|
* Entry point of argument options resolver.
|
|
179
186
|
*
|
|
@@ -188,6 +195,10 @@ function getCommandName(cmd) {
|
|
|
188
195
|
*
|
|
189
196
|
* Each code identifies a validation failure category and is also suitable as an
|
|
190
197
|
* i18n resource key for localized rendering.
|
|
198
|
+
*
|
|
199
|
+
* {@link resolveArgs} and `parse()` do not report `unknownOption`. An option that is not in the
|
|
200
|
+
* schema takes the argument after it as its value, unless that argument is an option, and the value
|
|
201
|
+
* is dropped: with no `foo` in the schema, `--foo bar` gives no positional argument.
|
|
191
202
|
*/
|
|
192
203
|
const ArgsValidationErrorKeys = {
|
|
193
204
|
requiredOption: "err:arg:required-option",
|
|
@@ -195,7 +206,11 @@ const ArgsValidationErrorKeys = {
|
|
|
195
206
|
invalidType: "err:arg:invalid-type",
|
|
196
207
|
invalidChoice: "err:arg:invalid-choice",
|
|
197
208
|
customParse: "err:arg:custom-parse",
|
|
198
|
-
unknownOption: "err:arg:unknown-option"
|
|
209
|
+
unknownOption: "err:arg:unknown-option",
|
|
210
|
+
unexpectedValue: "err:arg:unexpected-value",
|
|
211
|
+
missingValue: "err:arg:missing-value",
|
|
212
|
+
conflict: "err:arg:conflict",
|
|
213
|
+
invalidDefault: "err:arg:invalid-default"
|
|
199
214
|
};
|
|
200
215
|
/**
|
|
201
216
|
* Brand that marks {@link ArgsValidationError} instances.
|
|
@@ -270,7 +285,7 @@ function isArgsValidationError$1(error) {
|
|
|
270
285
|
return typeof values === "object" && values !== null;
|
|
271
286
|
}
|
|
272
287
|
//#endregion
|
|
273
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
288
|
+
//#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/index.js
|
|
274
289
|
/**
|
|
275
290
|
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
276
291
|
* @license MIT
|
|
@@ -290,6 +305,18 @@ function isArgsValidationError$1(error) {
|
|
|
290
305
|
*/
|
|
291
306
|
const CommandNotFoundErrorKeys = { notFound: "err:cmd:not-found" };
|
|
292
307
|
/**
|
|
308
|
+
* Command resolution error resource keys.
|
|
309
|
+
*
|
|
310
|
+
* These errors describe an ambiguity or inconsistency while choosing a command from arguments.
|
|
311
|
+
* They are kept separate from {@link CommandNotFoundError} so consumers can distinguish a typo
|
|
312
|
+
* from an input whose command interpretation is not unique.
|
|
313
|
+
*/
|
|
314
|
+
const CommandResolutionErrorKeys = {
|
|
315
|
+
ambiguous: "err:cmd:ambiguous",
|
|
316
|
+
inconsistentOptions: "err:cmd:inconsistent-options",
|
|
317
|
+
lazySchemaMismatch: "err:cmd:lazy-schema-mismatch"
|
|
318
|
+
};
|
|
319
|
+
/**
|
|
293
320
|
* Brand that marks {@link CommandNotFoundError} instances.
|
|
294
321
|
*
|
|
295
322
|
* The brand is looked up in the global symbol registry with `Symbol.for`, so it stays
|
|
@@ -298,6 +325,7 @@ const CommandNotFoundErrorKeys = { notFound: "err:cmd:not-found" };
|
|
|
298
325
|
* another copy, where `instanceof` cannot match.
|
|
299
326
|
*/
|
|
300
327
|
const COMMAND_NOT_FOUND_ERROR_BRAND = Symbol.for("gunshi.CommandNotFoundError");
|
|
328
|
+
const COMMAND_RESOLUTION_ERROR_BRAND = Symbol.for("gunshi.CommandResolutionError");
|
|
301
329
|
/**
|
|
302
330
|
* Error raised when a command cannot be resolved.
|
|
303
331
|
*
|
|
@@ -334,6 +362,39 @@ var CommandNotFoundError = class extends Error {
|
|
|
334
362
|
}
|
|
335
363
|
};
|
|
336
364
|
/**
|
|
365
|
+
* Error raised when command routing cannot produce one consistent command.
|
|
366
|
+
*
|
|
367
|
+
* The own registry brand is the cross-bundle contract. Unlike the legacy command-not-found
|
|
368
|
+
* guard, the new guard deliberately has no name-based fallback because this error type is new and
|
|
369
|
+
* must not classify arbitrary errors from older versions.
|
|
370
|
+
*/
|
|
371
|
+
var CommandResolutionError = class extends Error {
|
|
372
|
+
code;
|
|
373
|
+
values;
|
|
374
|
+
commandPath;
|
|
375
|
+
candidatePaths;
|
|
376
|
+
/**
|
|
377
|
+
* Create a command resolution error with structured routing metadata.
|
|
378
|
+
*
|
|
379
|
+
* @param message - Human-readable fallback message
|
|
380
|
+
* @param options - Error code and command-routing metadata
|
|
381
|
+
*/
|
|
382
|
+
constructor(message, options = {}) {
|
|
383
|
+
super(message, { cause: options.cause });
|
|
384
|
+
this.name = "CommandResolutionError";
|
|
385
|
+
this.code = options.code;
|
|
386
|
+
this.values = options.values || {};
|
|
387
|
+
this.commandPath = options.commandPath || [];
|
|
388
|
+
this.candidatePaths = options.candidatePaths || [];
|
|
389
|
+
Object.defineProperty(this, COMMAND_RESOLUTION_ERROR_BRAND, {
|
|
390
|
+
value: true,
|
|
391
|
+
enumerable: false,
|
|
392
|
+
writable: false,
|
|
393
|
+
configurable: false
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
/**
|
|
337
398
|
* Check whether an error is a {@link CommandNotFoundError}.
|
|
338
399
|
*
|
|
339
400
|
* `instanceof` alone is not enough: `@gunshi/plugin` is bundled with its own copy of this
|
|
@@ -351,6 +412,17 @@ function isCommandNotFoundError(error) {
|
|
|
351
412
|
return error instanceof Error && error.name === "CommandNotFoundError";
|
|
352
413
|
}
|
|
353
414
|
/**
|
|
415
|
+
* Check whether an error is a {@link CommandResolutionError}.
|
|
416
|
+
*
|
|
417
|
+
* @param error - An unknown error
|
|
418
|
+
* @returns `true` when the error has the command-resolution brand and shape
|
|
419
|
+
*/
|
|
420
|
+
function isCommandResolutionError(error) {
|
|
421
|
+
if (error instanceof CommandResolutionError) return true;
|
|
422
|
+
if (!isRecord(error) || !hasCommandResolutionErrorShape(error)) return false;
|
|
423
|
+
return Object.hasOwn(error, COMMAND_RESOLUTION_ERROR_BRAND) && error[COMMAND_RESOLUTION_ERROR_BRAND] === true;
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
354
426
|
* Check whether an error is an {@link ArgsValidationError}.
|
|
355
427
|
*
|
|
356
428
|
* Prefer this over the `args-tokens` guard of the same name. Both recognize errors from another
|
|
@@ -394,6 +466,9 @@ function hasOptionalStringCode(error) {
|
|
|
394
466
|
function hasCommandNotFoundErrorShape(error) {
|
|
395
467
|
return hasOptionalStringCode(error) && isRecord(error.values) && typeof error.commandName === "string" && isStringArray(error.candidates) && isStringArray(error.commandPath);
|
|
396
468
|
}
|
|
469
|
+
function hasCommandResolutionErrorShape(error) {
|
|
470
|
+
return hasOptionalStringCode(error) && isRecord(error.values) && isStringArray(error.commandPath) && Array.isArray(error.candidatePaths) && error.candidatePaths.every((path) => isStringArray(path));
|
|
471
|
+
}
|
|
397
472
|
function hasArgsValidationErrorShape(error) {
|
|
398
473
|
return hasOptionalStringCode(error) && isRecord(error.values);
|
|
399
474
|
}
|
|
@@ -404,7 +479,7 @@ function hasArgsValidationErrorShape(error) {
|
|
|
404
479
|
* @returns `true` if the validation error must be handled with priority
|
|
405
480
|
*/
|
|
406
481
|
function hasPriorityValidationError(error) {
|
|
407
|
-
return error?.errors.some((error) => isCommandNotFoundError(error) || isArgsValidationError(error) && error.code === ArgsValidationErrorKeys.unknownOption) ?? false;
|
|
482
|
+
return error?.errors.some((error) => isCommandNotFoundError(error) || isCommandResolutionError(error) || isArgsValidationError(error) && error.code === ArgsValidationErrorKeys.unknownOption) ?? false;
|
|
408
483
|
}
|
|
409
484
|
//#endregion
|
|
410
485
|
//#region ../gunshi/src/plugin/core.ts
|
|
@@ -425,6 +500,7 @@ const NOOP_EXTENSION = () => {
|
|
|
425
500
|
*/
|
|
426
501
|
function plugin(options = {}) {
|
|
427
502
|
const { id, name, setup, onExtension, dependencies } = options;
|
|
503
|
+
if (typeof id !== "string" || id.length === 0) throw new Error("Plugin id must be a non-empty string");
|
|
428
504
|
const extension = options.extension || NOOP_EXTENSION;
|
|
429
505
|
const pluginFn = async (ctx) => {
|
|
430
506
|
if (setup) await setup(ctx);
|
|
@@ -470,4 +546,4 @@ function plugin(options = {}) {
|
|
|
470
546
|
* @license MIT
|
|
471
547
|
*/
|
|
472
548
|
//#endregion
|
|
473
|
-
export { ANONYMOUS_COMMAND_NAME, ArgsValidationError, ArgsValidationErrorKeys, CLI_OPTIONS_DEFAULT, CommandNotFoundError, CommandNotFoundErrorKeys, createCommandContext, hasPriorityValidationError, isArgsValidationError, isCommandNotFoundError, plugin };
|
|
549
|
+
export { ANONYMOUS_COMMAND_NAME, ArgsValidationError, ArgsValidationErrorKeys, CLI_OPTIONS_DEFAULT, CommandNotFoundError, CommandNotFoundErrorKeys, CommandResolutionError, CommandResolutionErrorKeys, createCommandContext, hasPriorityValidationError, isArgsValidationError, isCommandNotFoundError, isCommandResolutionError, plugin };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gunshi/plugin",
|
|
3
3
|
"description": "plugin development kit for gunshi",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0-beta.1",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -52,11 +52,11 @@
|
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"deno": "^2.7.14",
|
|
55
|
+
"gunshi": "1.0.0-beta.1",
|
|
55
56
|
"jsr": "^0.14.3",
|
|
56
57
|
"jsr-exports-lint": "^0.4.2",
|
|
57
58
|
"publint": "^0.3.20",
|
|
58
|
-
"tsdown": "0.
|
|
59
|
-
"gunshi": "0.37.3"
|
|
59
|
+
"tsdown": "0.22.14"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"build": "tsdown",
|