@gunshi/shared 0.37.2 → 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +261 -96
- package/lib/index.js +114 -20
- 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,24 @@ interface ArgToken {
|
|
|
42
42
|
*/
|
|
43
43
|
rawName?: string;
|
|
44
44
|
/**
|
|
45
|
-
* Option value, e.g. `--foo=bar` => `bar
|
|
45
|
+
* Option value, e.g. `--foo=bar` => `bar`. The value of a short option, such as `bar` of
|
|
46
|
+
* `-x=bar`, `-1` of `-x=-1` or `-x-1`, and `''` of `-x=`, is in a token of its own after the
|
|
47
|
+
* option, which has no `name` and `rawName`.
|
|
46
48
|
* If the `allowCompatible` option is `true`, short option value will be same as Node.js `parseArgs` behavior.
|
|
47
49
|
*/
|
|
48
|
-
value?: string;
|
|
50
|
+
value?: string | undefined;
|
|
49
51
|
/**
|
|
50
|
-
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true
|
|
52
|
+
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true`, `-x-1` => `false`, since no `=`
|
|
53
|
+
* is written. Unlike Node.js `parseArgs`, `false` does not mean that the value is the next
|
|
54
|
+
* argument: the value token of `-x-1` has the `index` of that argument.
|
|
51
55
|
*/
|
|
52
|
-
inlineValue?: boolean;
|
|
56
|
+
inlineValue?: boolean | undefined;
|
|
53
57
|
}
|
|
54
58
|
/**
|
|
55
59
|
* Parser Options.
|
|
56
60
|
*/
|
|
57
61
|
//#endregion
|
|
58
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
62
|
+
//#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/resolver.d.ts
|
|
59
63
|
//#region src/resolver.d.ts
|
|
60
64
|
/**
|
|
61
65
|
* An argument schema definition for command-line argument parsing.
|
|
@@ -101,19 +105,25 @@ interface ArgSchema {
|
|
|
101
105
|
/**
|
|
102
106
|
* Type of the argument value.
|
|
103
107
|
*
|
|
104
|
-
* - `'string'`: Text value
|
|
105
|
-
* - `'boolean'`: `true`/`false` flag (can be negatable with `--no-` prefix)
|
|
108
|
+
* - `'string'`: Text value
|
|
109
|
+
* - `'boolean'`: `true`/`false` flag (can be negatable with `--no-` prefix). `--flag=true` and
|
|
110
|
+
* `--flag=false` set the value explicitly; any other value after `=` is a type error
|
|
106
111
|
* - `'number'`: Numeric value (parsed as integer or float)
|
|
107
|
-
* - `'enum'`: One of predefined string values (requires `choices` property
|
|
112
|
+
* - `'enum'`: One of predefined string values (requires `choices` property, unless the argument
|
|
113
|
+
* has a `parse` function)
|
|
108
114
|
* - `'positional'`: Non-option argument by position
|
|
109
115
|
* - `'custom'`: Custom parsing with user-defined `parse` function
|
|
110
116
|
*
|
|
117
|
+
* Any other `type`, or no `type`, is a mistake that only untyped code can make: if the argument
|
|
118
|
+
* has no `parse` function, {@link resolveArgs} and `parse()` throw an `Error`, whether or not the
|
|
119
|
+
* argument is given.
|
|
120
|
+
*
|
|
111
121
|
* @example
|
|
112
122
|
* Different argument types:
|
|
113
123
|
* ```ts
|
|
114
124
|
* {
|
|
115
125
|
* name: { type: 'string' }, // --name value
|
|
116
|
-
* verbose: { type: 'boolean' }, // --verbose
|
|
126
|
+
* verbose: { type: 'boolean' }, // --verbose (--no-verbose needs negatable: true)
|
|
117
127
|
* port: { type: 'number' }, // --port 3000
|
|
118
128
|
* level: { type: 'enum', choices: ['debug', 'info'] },
|
|
119
129
|
* file: { type: 'positional' }, // first positional arg
|
|
@@ -188,14 +198,32 @@ interface ArgSchema {
|
|
|
188
198
|
/**
|
|
189
199
|
* Marks the argument as required.
|
|
190
200
|
*
|
|
191
|
-
* When `true`, the argument must be provided by the user.
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
201
|
+
* When `true`, the argument must be provided by the user. If it is missing, the error is an
|
|
202
|
+
* `ArgResolveError` with type 'required' and the code `err:arg:required-option`
|
|
203
|
+
* ({@link ArgsValidationErrorKeys}.requiredOption), or `err:arg:required-positional`
|
|
204
|
+
* ({@link ArgsValidationErrorKeys}.requiredPositional) for a positional argument. Its
|
|
205
|
+
* `values` has the `displayName` as in the message, such as `'--name'` for an option
|
|
206
|
+
* (`'--name' or '-n'` with the short name `n`) and `'file'` for a positional argument, and
|
|
207
|
+
* the `name`, which is the schema key.
|
|
208
|
+
* An option that is given without a value is reported as `err:arg:missing-value`
|
|
209
|
+
* ({@link ArgsValidationErrorKeys}.missingValue) instead, because the option itself was given.
|
|
210
|
+
* An explicit empty value, such as `--name=` or `-n ''`, is still reported as required, but the
|
|
211
|
+
* option counts as given: it is `true` in `explicit` and takes part in conflicts, as any other
|
|
212
|
+
* given option does.
|
|
213
|
+
*
|
|
214
|
+
* For single-value positional arguments, omitting `required` keeps the argument required for
|
|
215
|
+
* compatibility, unless it has a `default`, which makes it optional. A `multiple` positional
|
|
216
|
+
* argument is optional unless `required: true` is set. Set `required: false` to make a positional
|
|
217
|
+
* argument explicitly optional. Optional positional arguments leave enough input values for later
|
|
197
218
|
* required positional arguments before consuming a value.
|
|
198
219
|
*
|
|
220
|
+
* In the type of the values, such as {@link ArgValues}, a single-value positional argument whose
|
|
221
|
+
* `required` may be `false`, such as one of type `boolean`, is optional, as it may be missing,
|
|
222
|
+
* unless it has a `default`. An optional `required` of type `boolean`, as declared here, says
|
|
223
|
+
* nothing, and keeps it required. TypeScript widens a `required: true` to `boolean` in a schema
|
|
224
|
+
* written apart from the call, such as `{ type: 'positional' as const, required: true }`, which
|
|
225
|
+
* types the argument as optional: write such a schema `as const`.
|
|
226
|
+
*
|
|
199
227
|
* @example
|
|
200
228
|
* Required arguments:
|
|
201
229
|
* ```ts
|
|
@@ -216,8 +244,9 @@ interface ArgSchema {
|
|
|
216
244
|
/**
|
|
217
245
|
* Allows the argument to accept multiple values.
|
|
218
246
|
*
|
|
219
|
-
* When `true`, the resolved value becomes an array.
|
|
220
|
-
*
|
|
247
|
+
* When `true`, the resolved value becomes an array. When the argument is not given and has no
|
|
248
|
+
* `default`, its value is `undefined`, not an empty array.
|
|
249
|
+
* For options: can be specified multiple times (--tags foo --tags bar)
|
|
221
250
|
* For positional: collects remaining positional arguments after preserving values for
|
|
222
251
|
* later required positional arguments.
|
|
223
252
|
*
|
|
@@ -249,6 +278,12 @@ interface ArgSchema {
|
|
|
249
278
|
*
|
|
250
279
|
* Only applicable to `type: 'boolean'` arguments.
|
|
251
280
|
*
|
|
281
|
+
* The negated name is always `no-` followed by the full option name. An option named
|
|
282
|
+
* `no-cache` is negated by `--no-no-cache`, and `--no-cache` sets it to `true`.
|
|
283
|
+
*
|
|
284
|
+
* The negated form does not take a value. `--no-flag=<value>` is reported as
|
|
285
|
+
* `err:arg:unexpected-value` ({@link ArgsValidationErrorKeys}.unexpectedValue).
|
|
286
|
+
*
|
|
252
287
|
* @example
|
|
253
288
|
* Negatable boolean:
|
|
254
289
|
* ```ts
|
|
@@ -267,8 +302,17 @@ interface ArgSchema {
|
|
|
267
302
|
/**
|
|
268
303
|
* Array of allowed string values for enum-type arguments.
|
|
269
304
|
*
|
|
270
|
-
* Required when `type: 'enum'
|
|
271
|
-
*
|
|
305
|
+
* Required when `type: 'enum'`, unless the argument has a `parse` function: if `choices` is
|
|
306
|
+
* missing or not an array, {@link resolveArgs} and `parse()` throw a `TypeError`, whether or not
|
|
307
|
+
* the argument is given. With a `parse` function and no `choices`, the value is passed to the
|
|
308
|
+
* function as for a `string` argument with a `parse` function.
|
|
309
|
+
*
|
|
310
|
+
* The argument value must be one of these choices, otherwise the error is an `ArgResolveError`
|
|
311
|
+
* with type 'type' and the code `err:arg:invalid-choice`
|
|
312
|
+
* ({@link ArgsValidationErrorKeys}.invalidChoice).
|
|
313
|
+
*
|
|
314
|
+
* The value is checked before `parse` is called, so a `parse` function receives only one of
|
|
315
|
+
* these choices and can change it, for example to upper case.
|
|
272
316
|
*
|
|
273
317
|
* Supports both mutable arrays and readonly arrays for type safety.
|
|
274
318
|
*
|
|
@@ -292,18 +336,37 @@ interface ArgSchema {
|
|
|
292
336
|
*/
|
|
293
337
|
choices?: string[] | readonly string[];
|
|
294
338
|
/**
|
|
295
|
-
* Default value used when the argument is not provided.
|
|
339
|
+
* Default value used when the argument is not provided. An option that is given without a value,
|
|
340
|
+
* or with a value that is rejected, also gets its default, along with the error.
|
|
296
341
|
*
|
|
297
342
|
* The type must match the argument's `type` property:
|
|
298
343
|
* - `string` type: string default
|
|
299
344
|
* - `boolean` type: boolean default
|
|
300
345
|
* - `number` type: number default
|
|
301
|
-
* - `enum` type:
|
|
346
|
+
* - `enum` type: one of the `choices` values, or a value that a `parse` of your own returns
|
|
302
347
|
* - `positional`/`custom` type: string, boolean, or number default
|
|
303
348
|
*
|
|
304
|
-
*
|
|
305
|
-
* value
|
|
306
|
-
*
|
|
349
|
+
* The default is used as is and never goes through `parse`, including when an option is given
|
|
350
|
+
* without a value. An explicit empty value, such as `--name=` or `-n ''`, is a value, not a
|
|
351
|
+
* missing one: a `string` option without `parse` gets `''` instead of the default, unless it is
|
|
352
|
+
* `required`. What `parse` returns is a value too, even `undefined` or `null`, and the default
|
|
353
|
+
* does not replace it.
|
|
354
|
+
*
|
|
355
|
+
* The value of a `multiple` argument is an array, so its default becomes the only element of the
|
|
356
|
+
* array: `default: 'latest'` gives `['latest']`.
|
|
357
|
+
*
|
|
358
|
+
* The default of an `enum` option with `choices` is checked when it would be used, that is, when
|
|
359
|
+
* no value from the command line is used: one that is not one of the choices is reported as an
|
|
360
|
+
* `ArgResolveError` with type 'type' and the code `err:arg:invalid-default`
|
|
361
|
+
* ({@link ArgsValidationErrorKeys}.invalidDefault), and is not used. With a `parse` function of
|
|
362
|
+
* your own, or a `map()` transform, the default is a value that the function returns, which need
|
|
363
|
+
* not be one of the choices, and it is not checked. `choice()` returns the value as is, so its
|
|
364
|
+
* default is checked.
|
|
365
|
+
*
|
|
366
|
+
* For positional arguments, `multiple` ones included, the default is used when no value is left
|
|
367
|
+
* for the argument: when the positional values run out, or when the remaining ones are preserved
|
|
368
|
+
* for later required positional arguments. With `required: true`, the default is not used, and
|
|
369
|
+
* the argument is reported as required instead.
|
|
307
370
|
*
|
|
308
371
|
* @example
|
|
309
372
|
* Default values by type:
|
|
@@ -336,7 +399,8 @@ interface ArgSchema {
|
|
|
336
399
|
* When `true`, a property like `maxCount` becomes available as `--max-count`.
|
|
337
400
|
* This allows [CAC](https://github.com/cacjs/cac) user-friendly property names while maintaining CLI conventions.
|
|
338
401
|
*
|
|
339
|
-
*
|
|
402
|
+
* The `toKebab` option of {@link resolveArgs} and `parse()` applies it to all arguments, as in
|
|
403
|
+
* `resolveArgs(args, tokens, { toKebab: true })`.
|
|
340
404
|
*
|
|
341
405
|
* Note: Only `true` is allowed (not `false`) to make intent explicit.
|
|
342
406
|
*
|
|
@@ -360,8 +424,19 @@ interface ArgSchema {
|
|
|
360
424
|
/**
|
|
361
425
|
* Names of other options that conflict with this option.
|
|
362
426
|
*
|
|
363
|
-
* When this option is used together with any of the conflicting options,
|
|
364
|
-
*
|
|
427
|
+
* When this option is used together with any of the conflicting options, the error is an
|
|
428
|
+
* `ArgResolveError` with type 'conflict' and the code `err:arg:conflict`
|
|
429
|
+
* ({@link ArgsValidationErrorKeys}.conflict). Its `values` has the `displayName` and `name` of
|
|
430
|
+
* the argument whose `conflicts` names the other one, and the `conflictDisplayName` and
|
|
431
|
+
* `conflictName` of the other one. `displayName` and `conflictDisplayName` show an option as
|
|
432
|
+
* it was written, such as `'-p'`, and `name` and `conflictName` are the schema keys. When both
|
|
433
|
+
* name each other, `name` is the one that comes first in the schema.
|
|
434
|
+
*
|
|
435
|
+
* A positional argument can be on either side, such as `[file]` and `--stdin`. It conflicts when
|
|
436
|
+
* it is given, and it is shown by its name, such as `'file'` (in kebab-case with `toKebab`), as
|
|
437
|
+
* in its other errors. The message starts with the kind of the argument given as `name`:
|
|
438
|
+
* `Positional argument 'file' conflicts with '--stdin'` or
|
|
439
|
+
* `Optional argument '--stdin' conflicts with 'file'`.
|
|
365
440
|
*
|
|
366
441
|
* Conflicts only need to be defined on one side - if option A defines a conflict
|
|
367
442
|
* with option B, the conflict is automatically detected when both are used,
|
|
@@ -399,7 +474,7 @@ interface ArgSchema {
|
|
|
399
474
|
* // No conflicts defined, but still cannot use with --summer
|
|
400
475
|
* }
|
|
401
476
|
* }
|
|
402
|
-
* // Usage: --summer --autumn
|
|
477
|
+
* // Usage: --summer --autumn reports an error
|
|
403
478
|
* // Error: "Optional argument '--summer' conflicts with '--autumn'"
|
|
404
479
|
* ```
|
|
405
480
|
*
|
|
@@ -451,6 +526,10 @@ interface ArgSchema {
|
|
|
451
526
|
* Particularly useful for `type: 'custom'` arguments where the type
|
|
452
527
|
* name would otherwise be unhelpful.
|
|
453
528
|
*
|
|
529
|
+
* For a `custom` option that is given without a value, it is also the `expected` in the `values`
|
|
530
|
+
* of the `err:arg:missing-value` error ({@link ArgsValidationErrorKeys}.missingValue), which is
|
|
531
|
+
* `'custom'` without it.
|
|
532
|
+
*
|
|
454
533
|
* @example
|
|
455
534
|
* Metavar usage:
|
|
456
535
|
* ```ts
|
|
@@ -467,13 +546,37 @@ interface ArgSchema {
|
|
|
467
546
|
*/
|
|
468
547
|
metavar?: string;
|
|
469
548
|
/**
|
|
470
|
-
* Custom parsing function
|
|
549
|
+
* Custom parsing function, used in place of the parsing of the argument's `type`.
|
|
471
550
|
*
|
|
472
|
-
* Required when `type: 'custom'
|
|
473
|
-
*
|
|
551
|
+
* Required when `type: 'custom'`: if it is missing or not a function, {@link resolveArgs} and
|
|
552
|
+
* `parse()` throw a `TypeError`, whether or not the argument is given. The function receives the
|
|
553
|
+
* raw string value and must return the parsed result. It should throw an Error (or subclass) if
|
|
554
|
+
* parsing fails.
|
|
474
555
|
*
|
|
475
556
|
* The function's return type becomes the resolved argument type.
|
|
476
557
|
*
|
|
558
|
+
* `parse` is called synchronously, and what it returns becomes the value, even `undefined` or
|
|
559
|
+
* `null`, so throw to reject a value. An `async` function returns a promise, and the promise
|
|
560
|
+
* becomes the value as is: it is not awaited, and its rejection is not reported as an error.
|
|
561
|
+
* Await the value, or each of its elements with `multiple` (for example with `Promise.all()`),
|
|
562
|
+
* and handle the rejection yourself: a rejection that nothing handles is an unhandled rejection,
|
|
563
|
+
* which ends a Node.js process by default.
|
|
564
|
+
*
|
|
565
|
+
* A `boolean` option calls `parse` with `'true'`, or `'false'` for the negated form. Other
|
|
566
|
+
* options call it only with a value from the command line: when the option is given without a
|
|
567
|
+
* value, `parse` is not called and the missing value is reported as `err:arg:missing-value`
|
|
568
|
+
* ({@link ArgsValidationErrorKeys}.missingValue). When that error may suggest the argument after
|
|
569
|
+
* the option as its value, such as `--port=-5` for `--port -5`, a `parse` function of your own
|
|
570
|
+
* is not called to check it either: only the `parse` functions of `string()`, `number()`,
|
|
571
|
+
* `integer()`, `float()` and `choice()`, which have no side effects, are called for that.
|
|
572
|
+
* An explicit empty value, such as `--name=` or `-n=`, is passed as `''` unless
|
|
573
|
+
* `required: true` is set.
|
|
574
|
+
*
|
|
575
|
+
* An `enum` option with `choices` calls it only with one of them. Any other value, an explicit
|
|
576
|
+
* empty one included, is reported as `err:arg:invalid-choice`
|
|
577
|
+
* ({@link ArgsValidationErrorKeys}.invalidChoice), except that a required option reports an
|
|
578
|
+
* explicit empty value as required.
|
|
579
|
+
*
|
|
477
580
|
* @param value - Raw string value from command line
|
|
478
581
|
* @returns Parsed value of any type
|
|
479
582
|
* @throws {Error} Error or subclass when value is invalid
|
|
@@ -508,13 +611,6 @@ interface ArgSchema {
|
|
|
508
611
|
*/
|
|
509
612
|
parse?: (value: string) => any;
|
|
510
613
|
}
|
|
511
|
-
/**
|
|
512
|
-
* Machine-readable error codes for {@link ArgsValidationError}.
|
|
513
|
-
*
|
|
514
|
-
* Each code identifies a validation failure category and is also suitable as an
|
|
515
|
-
* i18n resource key for localized rendering.
|
|
516
|
-
*/
|
|
517
|
-
|
|
518
614
|
/**
|
|
519
615
|
* An object that contains {@link ArgSchema | argument schema}.
|
|
520
616
|
*
|
|
@@ -526,6 +622,20 @@ interface Args {
|
|
|
526
622
|
/**
|
|
527
623
|
* An object that contains the values of the arguments.
|
|
528
624
|
*
|
|
625
|
+
* The value of an argument whose schema is a union, such as
|
|
626
|
+
* `strict ? multiple(integer()) : string()` with the combinators, is typed as a value of any of its
|
|
627
|
+
* schemas: `number[] | string`. The argument is typed as present only when each schema of the
|
|
628
|
+
* union gives it a value, with a default, with `required: true` or as a required positional
|
|
629
|
+
* argument.
|
|
630
|
+
*
|
|
631
|
+
* When the type of one schema covers that of the other, as the type of `integer()` covers that of
|
|
632
|
+
* `multiple(integer())`, TypeScript types a `?:` expression of them as the covering schema alone:
|
|
633
|
+
* the value of `strict ? multiple(integer()) : integer()` is typed without the array, and
|
|
634
|
+
* `strict ? multiple(positional()) : positional()` as present, although it may be missing. Give
|
|
635
|
+
* each schema its own argument instead, or give the expression the type of both schemas with `as`,
|
|
636
|
+
* as in `(strict ? many : one) as typeof many | typeof one` for `const many = multiple(integer())`
|
|
637
|
+
* and `const one = integer()`.
|
|
638
|
+
*
|
|
529
639
|
* @typeParam T - {@link Args | Arguments} which is an object that defines the command line arguments.
|
|
530
640
|
*/
|
|
531
641
|
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
@@ -533,22 +643,41 @@ type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: Ext
|
|
|
533
643
|
};
|
|
534
644
|
/**
|
|
535
645
|
* Extracts the value type from the argument schema.
|
|
646
|
+
* For a union of schemas, it is the union of the value types of its schemas.
|
|
536
647
|
*
|
|
537
648
|
* @typeParam A - {@link ArgSchema | Argument schema} which is an object that defines command line arguments.
|
|
538
649
|
*
|
|
539
650
|
* @internal
|
|
540
651
|
*/
|
|
541
|
-
type ExtractOptionValue<A extends ArgSchema> =
|
|
652
|
+
type ExtractOptionValue<A extends ArgSchema> = A extends ArgSchema ? ExtractSchemaValue<A> : never;
|
|
653
|
+
/**
|
|
654
|
+
* The value type of one schema, which {@link ExtractOptionValue} takes for each schema of a union.
|
|
655
|
+
*/
|
|
656
|
+
type ExtractSchemaValue<A extends ArgSchema> = undefined extends A['parse'] ? A['type'] extends 'string' ? ResolveOptionValue<A, string> : A['type'] extends 'boolean' ? ResolveOptionValue<A, boolean> : A['type'] extends 'number' ? ResolveOptionValue<A, number> : A['type'] extends 'positional' ? ResolveOptionValue<A, string> : A['type'] extends 'enum' ? A['choices'] extends string[] | readonly string[] ? ResolveOptionValue<A, A['choices'][number]> : never : A['type'] extends 'custom' ? never : ResolveOptionValue<A, string | boolean | number> : A['parse'] extends ((value: string) => infer R) ? ResolveOptionValue<A, R> : never;
|
|
542
657
|
type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T[] : T;
|
|
543
658
|
/**
|
|
544
659
|
* Resolved argument values.
|
|
545
660
|
*
|
|
661
|
+
* Each argument is mapped as optional, and the ones that are present, as {@link IsPresentArg}
|
|
662
|
+
* says, are mapped again as present. The optional mapping types a present argument as `unknown`:
|
|
663
|
+
* TypeScript does not reduce an intersection such as
|
|
664
|
+
* `(string | number[] | undefined) & (string | number[])`, of a value type that is a union with an
|
|
665
|
+
* object type. It maps all the keys without renaming them, which lets TypeScript relate the values
|
|
666
|
+
* of related type parameters, so that a conversion such as `values as ArgValues<A>` compiles.
|
|
667
|
+
*
|
|
546
668
|
* @typeParam A - {@link Arguments | Args} which is an object that defines the command line arguments.
|
|
547
669
|
* @typeParam V - Resolvable argument values.
|
|
548
670
|
*
|
|
549
671
|
* @internal
|
|
550
672
|
*/
|
|
551
|
-
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: V[Arg] } &
|
|
673
|
+
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: IsPresentArg<A[Arg]> extends true ? unknown : V[Arg] } & { -readonly [Arg in keyof A as IsPresentArg<A[Arg]> extends true ? Arg : never]: V[Arg] } extends (infer P) ? { [K in keyof P]: P[K] } : never;
|
|
674
|
+
/**
|
|
675
|
+
* Whether an argument with the schema `S` has a value: with a default, with `required: true` or as
|
|
676
|
+
* a required positional argument. For a union of schemas, only when each schema of the union has
|
|
677
|
+
* one. A `default` or `required` typed as `any`, and a schema typed as `any`, count as giving a
|
|
678
|
+
* value, as {@link FilterArgs} does.
|
|
679
|
+
*/
|
|
680
|
+
type IsPresentArg<S extends ArgSchema> = S extends ArgSchema ? true extends (S['default'] extends {} ? true : S['required'] extends true ? true : false) ? true : IsRequiredPositionalArg<S> : never;
|
|
552
681
|
/**
|
|
553
682
|
* Filters the arguments based on their default values.
|
|
554
683
|
*
|
|
@@ -558,17 +687,17 @@ type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -r
|
|
|
558
687
|
*
|
|
559
688
|
* @internal
|
|
560
689
|
*/
|
|
561
|
-
|
|
690
|
+
|
|
691
|
+
type IsRequiredPositionalArg<A extends ArgSchema> = A['type'] extends 'positional' ? A['multiple'] extends true ? A['required'] extends true ? true : false : MayBeUnrequired<A> extends true ? A['default'] extends {} ? true : false : true : false;
|
|
562
692
|
/**
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
*
|
|
568
|
-
* @internal
|
|
693
|
+
* Whether the `required` of `A` may be `false`. An optional `required` of type `boolean`, as in
|
|
694
|
+
* {@link ArgSchema}, says nothing, and is read as missing, so that a positional argument with it is
|
|
695
|
+
* required. A `required` that `A` always has may be `false` when its type is `boolean` or
|
|
696
|
+
* `boolean | undefined`.
|
|
569
697
|
*/
|
|
570
|
-
type
|
|
571
|
-
|
|
698
|
+
type MayBeUnrequired<A extends ArgSchema> = false extends A['required'] ? [A] extends [{
|
|
699
|
+
required: unknown;
|
|
700
|
+
}] ? true : boolean | undefined extends A['required'] ? false : true : false;
|
|
572
701
|
/**
|
|
573
702
|
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
574
703
|
*/
|
|
@@ -576,44 +705,16 @@ type IsRequiredPositionalArg<A extends ArgSchema> = A['type'] extends 'positiona
|
|
|
576
705
|
/**
|
|
577
706
|
* Tracks which arguments were explicitly provided by the user.
|
|
578
707
|
*
|
|
579
|
-
* Each property
|
|
580
|
-
*
|
|
708
|
+
* Each property is `true` when the argument is given on the command line, and `false` when it is
|
|
709
|
+
* not, whether it has a default or not. An option is given when one of its forms is, such as
|
|
710
|
+
* `--port`, `-p` or `--no-color`, even if its value is missing or rejected: the error is reported,
|
|
711
|
+
* and if no value from the command line is used, its default, if any, fills in the value. A
|
|
712
|
+
* positional argument is given when a positional value is used for it, even if `parse` rejects
|
|
713
|
+
* the value: the error is reported, and its default is not used.
|
|
581
714
|
*
|
|
582
715
|
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
583
716
|
*/
|
|
584
717
|
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean };
|
|
585
|
-
/**
|
|
586
|
-
* Resolve command line arguments.
|
|
587
|
-
*
|
|
588
|
-
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
589
|
-
*
|
|
590
|
-
* @param args - An arguments that contains {@link ArgSchema | arguments schema}.
|
|
591
|
-
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
592
|
-
* @param resolveArgs - An arguments that contains {@link ResolveArgs | resolve arguments}.
|
|
593
|
-
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, {@link AggregateError | validation errors}, and explicit provision status.
|
|
594
|
-
*
|
|
595
|
-
* @example
|
|
596
|
-
* ```typescript
|
|
597
|
-
* // passed tokens: --port 3000
|
|
598
|
-
*
|
|
599
|
-
* const { values, explicit } = resolveArgs({
|
|
600
|
-
* port: {
|
|
601
|
-
* type: 'number',
|
|
602
|
-
* default: 8080
|
|
603
|
-
* },
|
|
604
|
-
* host: {
|
|
605
|
-
* type: 'string',
|
|
606
|
-
* default: 'localhost'
|
|
607
|
-
* }
|
|
608
|
-
* }, parsedTokens)
|
|
609
|
-
*
|
|
610
|
-
* values.port // 3000
|
|
611
|
-
* values.host // 'localhost'
|
|
612
|
-
*
|
|
613
|
-
* explicit.port // true (explicitly provided)
|
|
614
|
-
* explicit.host // false (not provided, fallback to default)
|
|
615
|
-
* ```
|
|
616
|
-
*/
|
|
617
718
|
//#endregion
|
|
618
719
|
//#region ../gunshi/src/types.d.ts
|
|
619
720
|
/**
|
|
@@ -760,7 +861,11 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
|
|
|
760
861
|
*/
|
|
761
862
|
usageOptionValue: boolean;
|
|
762
863
|
/**
|
|
763
|
-
* Whether to
|
|
864
|
+
* Whether to keep usage, header, version, and validation-error text off the terminal
|
|
865
|
+
* (they are still returned as strings) and to silence {@linkcode CommandContext.log}.
|
|
866
|
+
*
|
|
867
|
+
* Output that must still appear — machine-readable results for an agent, warnings for the
|
|
868
|
+
* CLI author — should use `console.log` / `console.warn`, not `ctx.log`.
|
|
764
869
|
*
|
|
765
870
|
* @default false
|
|
766
871
|
* @see {@linkcode CliOptions.usageSilent}
|
|
@@ -779,6 +884,26 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
|
|
|
779
884
|
* @see {@linkcode CliOptions.subCommands}
|
|
780
885
|
*/
|
|
781
886
|
subCommands: Map<string, Command<any> | LazyCommand<any>> | undefined;
|
|
887
|
+
/**
|
|
888
|
+
* The entry command of the CLI, marked with `entry: true`.
|
|
889
|
+
*
|
|
890
|
+
* It is available whether or not the CLI has sub-commands, while {@linkcode CommandEnvironment.subCommands}
|
|
891
|
+
* includes the entry command only when the sub-commands are given with {@linkcode CliOptions.subCommands}.
|
|
892
|
+
* It is not frozen, like the commands in {@linkcode CommandEnvironment.subCommands}.
|
|
893
|
+
*/
|
|
894
|
+
entryCommand?: Command<any> | LazyCommand<any>;
|
|
895
|
+
/**
|
|
896
|
+
* The global options that are in effect for the command being executed.
|
|
897
|
+
*
|
|
898
|
+
* A plugin registers a global option with `addGlobalOption`, and gunshi merges it into the arguments
|
|
899
|
+
* of the command that runs. An argument that the command declares under the same name replaces it,
|
|
900
|
+
* and the option is then not in effect: the value under that name is the command's, and the plugin
|
|
901
|
+
* that registered it must not read it as its own.
|
|
902
|
+
*
|
|
903
|
+
* The schema is the one in effect, which may have given up its short name to an argument of the
|
|
904
|
+
* command that claims the same letter.
|
|
905
|
+
*/
|
|
906
|
+
globalOptions?: ReadonlyMap<string, ArgSchema>;
|
|
782
907
|
/**
|
|
783
908
|
* Render function the command usage.
|
|
784
909
|
*/
|
|
@@ -1126,7 +1251,7 @@ type CommandExamplesFetcher<G extends GunshiParamsConstraint = DefaultGunshiPara
|
|
|
1126
1251
|
*/
|
|
1127
1252
|
type CommandRunner<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ctx: Readonly<CommandContext<G>>) => Awaitable<string | void>;
|
|
1128
1253
|
//#endregion
|
|
1129
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
1254
|
+
//#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/utils.d.ts
|
|
1130
1255
|
//#region src/utils.d.ts
|
|
1131
1256
|
/**
|
|
1132
1257
|
* Entry point of utils.
|
|
@@ -1146,24 +1271,24 @@ type CommandRunner<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ct
|
|
|
1146
1271
|
* @returns Converted string into kebab-case.
|
|
1147
1272
|
*/
|
|
1148
1273
|
declare function kebabnize(str: string): string;
|
|
1149
|
-
/**
|
|
1150
|
-
* Format allowed argument choices for validation error values and fallback messages.
|
|
1151
|
-
*
|
|
1152
|
-
* @param choices - Choice values to format
|
|
1153
|
-
* @returns JSON-quoted choices joined with `, `.
|
|
1154
|
-
*/
|
|
1155
1274
|
//#endregion
|
|
1156
1275
|
//#region ../gunshi/src/utils.d.ts
|
|
1157
1276
|
/**
|
|
1158
1277
|
* Check if the given command is a {@link LazyCommand}.
|
|
1159
1278
|
*
|
|
1160
1279
|
* @param cmd - A command to check
|
|
1161
|
-
* @returns `true` if the command is a {@link LazyCommand}, otherwise `false
|
|
1280
|
+
* @returns `true` if the command is a {@link LazyCommand}, otherwise `false`
|
|
1281
|
+
*
|
|
1282
|
+
* A lazy command is identified by its `commandName` property. The property may be
|
|
1283
|
+
* `undefined` when the lazy command has no definition name.
|
|
1162
1284
|
*/
|
|
1163
1285
|
declare function isLazyCommand<G extends GunshiParamsConstraint = DefaultGunshiParams>(cmd: unknown): cmd is LazyCommand<G>;
|
|
1164
1286
|
/**
|
|
1165
1287
|
* Resolve a lazy command to a {@link Command}.
|
|
1166
1288
|
*
|
|
1289
|
+
* If the loader returns a command, the properties of the loaded command take precedence,
|
|
1290
|
+
* and the properties that it does not define fall back to the definition that is given to `lazy`.
|
|
1291
|
+
*
|
|
1167
1292
|
* @param cmd - A {@link Commandable} or {@link LazyCommand} to resolve
|
|
1168
1293
|
* @param name - Optional name of the command, if not provided, it will use the name from the command itself.
|
|
1169
1294
|
* @param needRunResolving - Whether to run the resolving function of the lazy command.
|
|
@@ -1177,6 +1302,19 @@ declare function resolveLazyCommand<G extends GunshiParamsConstraint = DefaultGu
|
|
|
1177
1302
|
* @returns A new object with the specified prototype
|
|
1178
1303
|
*/
|
|
1179
1304
|
declare function create<T>(obj?: object | null): T;
|
|
1305
|
+
/**
|
|
1306
|
+
* Resolve the arguments of a command, the way gunshi parses and renders them.
|
|
1307
|
+
*
|
|
1308
|
+
* The global options belong to no command definition, so they are merged into the arguments of the
|
|
1309
|
+
* command that runs. An argument of the command shadows the global option of the same name, and it
|
|
1310
|
+
* shadows the same way by short name: a global option gives up its short name to an argument of the
|
|
1311
|
+
* command that claims the same letter, and keeps its long name.
|
|
1312
|
+
*
|
|
1313
|
+
* @param globalOptions - The global options that plugins registered with `addGlobalOption`
|
|
1314
|
+
* @param args - The arguments that the command declares
|
|
1315
|
+
* @returns The merged arguments
|
|
1316
|
+
*/
|
|
1317
|
+
declare function resolveCommandArgs<A extends Args = Args>(globalOptions?: ReadonlyMap<string, ArgSchema>, args?: A): A;
|
|
1180
1318
|
/**
|
|
1181
1319
|
* Log a message to the console.
|
|
1182
1320
|
*
|
|
@@ -1227,8 +1365,8 @@ type CommonArgType = {
|
|
|
1227
1365
|
};
|
|
1228
1366
|
declare const COMMON_ARGS: CommonArgType;
|
|
1229
1367
|
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "ARGUMENTS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES"];
|
|
1230
|
-
declare const ARG_ERROR_RESOURCE_KEYS: readonly ["err:arg:required-option", "err:arg:required-positional", "err:arg:invalid-type", "err:arg:invalid-choice", "err:arg:custom-parse", "err:arg:unknown-option"];
|
|
1231
|
-
declare const COMMAND_ERROR_RESOURCE_KEYS: readonly ["err:cmd:not-found"];
|
|
1368
|
+
declare const ARG_ERROR_RESOURCE_KEYS: readonly ["err:arg:required-option", "err:arg:required-positional", "err:arg:invalid-type", "err:arg:invalid-choice", "err:arg:custom-parse", "err:arg:unknown-option", "err:arg:unexpected-value", "err:arg:missing-value", "err:arg:conflict", "err:arg:invalid-default"];
|
|
1369
|
+
declare const COMMAND_ERROR_RESOURCE_KEYS: readonly ["err:cmd:not-found", "err:cmd:ambiguous", "err:cmd:inconsistent-options", "err:cmd:lazy-schema-mismatch"];
|
|
1232
1370
|
declare const SUGGESTION_ERROR_RESOURCE_KEYS: readonly ["err:suggestion:did-you-mean"];
|
|
1233
1371
|
//#endregion
|
|
1234
1372
|
//#region src/types.d.ts
|
|
@@ -1375,7 +1513,14 @@ declare const _exports: {
|
|
|
1375
1513
|
"err:arg:invalid-choice": string;
|
|
1376
1514
|
"err:arg:custom-parse": string;
|
|
1377
1515
|
"err:arg:unknown-option": string;
|
|
1516
|
+
"err:arg:unexpected-value": string;
|
|
1517
|
+
"err:arg:missing-value": string;
|
|
1518
|
+
"err:arg:conflict": string;
|
|
1519
|
+
"err:arg:invalid-default": string;
|
|
1378
1520
|
"err:cmd:not-found": string;
|
|
1521
|
+
"err:cmd:ambiguous": string;
|
|
1522
|
+
"err:cmd:inconsistent-options": string;
|
|
1523
|
+
"err:cmd:lazy-schema-mismatch": string;
|
|
1379
1524
|
"err:suggestion:did-you-mean": string;
|
|
1380
1525
|
};
|
|
1381
1526
|
//#endregion
|
|
@@ -1444,6 +1589,26 @@ declare function namespacedId<K extends string>(id: K): GenerateNamespacedKey<K,
|
|
|
1444
1589
|
* @returns A string representing the short and long option pair.
|
|
1445
1590
|
*/
|
|
1446
1591
|
declare function makeShortLongOptionPair(schema: ArgSchema, name: string, toKebab?: boolean): string;
|
|
1592
|
+
/**
|
|
1593
|
+
* Resolve the name that an argument is parsed and rendered under.
|
|
1594
|
+
*
|
|
1595
|
+
* @param name - The key of the argument.
|
|
1596
|
+
* @param schema - The {@linkcode ArgSchema | argument schema}.
|
|
1597
|
+
* @param toKebab - Whether to convert the name to kebab-case.
|
|
1598
|
+
* @returns The name of the argument, kebab-cased when `toKebab` asks for it.
|
|
1599
|
+
*/
|
|
1600
|
+
declare function resolveDisplayName(name: string, schema: ArgSchema, toKebab?: boolean): string;
|
|
1601
|
+
/**
|
|
1602
|
+
* Collect the names that the arguments are parsed and rendered under.
|
|
1603
|
+
*
|
|
1604
|
+
* A positional argument is left out: it is never spelled as an option, so it cannot take the name of
|
|
1605
|
+
* one. A `hidden` argument is counted, because it still owns the name that it parses.
|
|
1606
|
+
*
|
|
1607
|
+
* @param args - The {@linkcode Args | arguments} of a command, global options included.
|
|
1608
|
+
* @param toKebab - Whether the command converts its names to kebab-case.
|
|
1609
|
+
* @returns The set of names.
|
|
1610
|
+
*/
|
|
1611
|
+
declare function resolveOptionNames(args: Args, toKebab?: boolean): Set<string>;
|
|
1447
1612
|
//#endregion
|
|
1448
1613
|
//#region src/index.d.ts
|
|
1449
1614
|
/**
|
|
@@ -1452,4 +1617,4 @@ declare function makeShortLongOptionPair(schema: ArgSchema, name: string, toKeba
|
|
|
1452
1617
|
*/
|
|
1453
1618
|
|
|
1454
1619
|
//#endregion
|
|
1455
|
-
export { ARG_ERROR_RESOURCE_KEYS, ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, ArgErrorResourceKeys, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, BuiltinResource, BuiltinResourceKeys, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_ERROR_RESOURCE_KEYS, COMMON_ARGS, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandErrorResourceKeys, DeepWriteable, _exports as DefaultResource, ERROR_PREFIX, ERROR_PREFIX_AND_KEY_SEPARATOR, ErrorResourceKeys, GenerateNamespacedKey, KeyOfArgs, Localization, PLUGIN_PREFIX, RemovedIndex, ResolveTranslationKeys, SUGGESTION_ERROR_RESOURCE_KEYS, SuggestionErrorResourceKeys, Translation, create, deepFreeze, formatResource, getCommandSubCommands, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveKey, resolveLazyCommand };
|
|
1620
|
+
export { ARG_ERROR_RESOURCE_KEYS, ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, ArgErrorResourceKeys, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, BuiltinResource, BuiltinResourceKeys, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_ERROR_RESOURCE_KEYS, COMMON_ARGS, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandErrorResourceKeys, DeepWriteable, _exports as DefaultResource, ERROR_PREFIX, ERROR_PREFIX_AND_KEY_SEPARATOR, ErrorResourceKeys, GenerateNamespacedKey, KeyOfArgs, Localization, PLUGIN_PREFIX, RemovedIndex, ResolveTranslationKeys, SUGGESTION_ERROR_RESOURCE_KEYS, SuggestionErrorResourceKeys, Translation, create, deepFreeze, formatResource, getCommandSubCommands, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveCommandArgs, resolveDisplayName, resolveExamples, resolveKey, resolveLazyCommand, resolveOptionNames };
|
package/lib/index.js
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/utils.js
|
|
2
2
|
/**
|
|
3
3
|
* Entry point of utils.
|
|
4
4
|
*
|
|
@@ -29,14 +29,20 @@ function kebabnize(str) {
|
|
|
29
29
|
* Check if the given command is a {@link LazyCommand}.
|
|
30
30
|
*
|
|
31
31
|
* @param cmd - A command to check
|
|
32
|
-
* @returns `true` if the command is a {@link LazyCommand}, otherwise `false
|
|
32
|
+
* @returns `true` if the command is a {@link LazyCommand}, otherwise `false`
|
|
33
|
+
*
|
|
34
|
+
* A lazy command is identified by its `commandName` property. The property may be
|
|
35
|
+
* `undefined` when the lazy command has no definition name.
|
|
33
36
|
*/
|
|
34
37
|
function isLazyCommand(cmd) {
|
|
35
|
-
return typeof cmd === "function" && "commandName" in cmd
|
|
38
|
+
return typeof cmd === "function" && "commandName" in cmd;
|
|
36
39
|
}
|
|
37
40
|
/**
|
|
38
41
|
* Resolve a lazy command to a {@link Command}.
|
|
39
42
|
*
|
|
43
|
+
* If the loader returns a command, the properties of the loaded command take precedence,
|
|
44
|
+
* and the properties that it does not define fall back to the definition that is given to `lazy`.
|
|
45
|
+
*
|
|
40
46
|
* @param cmd - A {@link Commandable} or {@link LazyCommand} to resolve
|
|
41
47
|
* @param name - Optional name of the command, if not provided, it will use the name from the command itself.
|
|
42
48
|
* @param needRunResolving - Whether to run the resolving function of the lazy command.
|
|
@@ -52,6 +58,8 @@ async function resolveLazyCommand(cmd, name, needRunResolving = false) {
|
|
|
52
58
|
examples: cmd.examples,
|
|
53
59
|
internal: cmd.internal,
|
|
54
60
|
entry: cmd.entry,
|
|
61
|
+
toKebab: cmd.toKebab,
|
|
62
|
+
rendering: cmd.rendering,
|
|
55
63
|
subCommands: cmd.subCommands
|
|
56
64
|
};
|
|
57
65
|
if ("resource" in cmd && cmd.resource) baseCommand.resource = cmd.resource;
|
|
@@ -61,13 +69,20 @@ async function resolveLazyCommand(cmd, name, needRunResolving = false) {
|
|
|
61
69
|
if (typeof loaded === "function") command.run = loaded;
|
|
62
70
|
else if (typeof loaded === "object") {
|
|
63
71
|
if (loaded.run == null) throw new TypeError(`'run' is required in command: ${cmd.name || name}`);
|
|
72
|
+
/**
|
|
73
|
+
* NOTE(kazupon): the loaded command wins, and the definition that is given to `lazy` is the fallback
|
|
74
|
+
* for what the loaded command does not define. A loader that returns `{ run }` keeps the whole definition,
|
|
75
|
+
* like a loader that returns the runner itself.
|
|
76
|
+
*/
|
|
64
77
|
command.run = loaded.run;
|
|
65
|
-
command.name = loaded.name;
|
|
66
|
-
command.description = loaded.description;
|
|
67
|
-
command.args = loaded.args;
|
|
68
|
-
command.examples = loaded.examples;
|
|
69
|
-
command.internal = loaded.internal;
|
|
70
|
-
command.entry = loaded.entry;
|
|
78
|
+
command.name = loaded.name ?? cmd.commandName;
|
|
79
|
+
command.description = loaded.description ?? cmd.description;
|
|
80
|
+
command.args = loaded.args ?? cmd.args;
|
|
81
|
+
command.examples = loaded.examples ?? cmd.examples;
|
|
82
|
+
command.internal = loaded.internal ?? cmd.internal;
|
|
83
|
+
command.entry = loaded.entry ?? cmd.entry;
|
|
84
|
+
command.toKebab = loaded.toKebab ?? cmd.toKebab;
|
|
85
|
+
command.rendering = loaded.rendering ?? cmd.rendering;
|
|
71
86
|
command.subCommands = loaded.subCommands || cmd.subCommands;
|
|
72
87
|
if ("resource" in loaded && loaded.resource) command.resource = loaded.resource;
|
|
73
88
|
} else throw new TypeError(`Cannot resolve command: ${cmd.name || name}`);
|
|
@@ -86,6 +101,37 @@ function create(obj = null) {
|
|
|
86
101
|
return Object.create(obj);
|
|
87
102
|
}
|
|
88
103
|
/**
|
|
104
|
+
* Resolve the arguments of a command, the way gunshi parses and renders them.
|
|
105
|
+
*
|
|
106
|
+
* The global options belong to no command definition, so they are merged into the arguments of the
|
|
107
|
+
* command that runs. An argument of the command shadows the global option of the same name, and it
|
|
108
|
+
* shadows the same way by short name: a global option gives up its short name to an argument of the
|
|
109
|
+
* command that claims the same letter, and keeps its long name.
|
|
110
|
+
*
|
|
111
|
+
* @param globalOptions - The global options that plugins registered with `addGlobalOption`
|
|
112
|
+
* @param args - The arguments that the command declares
|
|
113
|
+
* @returns The merged arguments
|
|
114
|
+
*/
|
|
115
|
+
function resolveCommandArgs(globalOptions, args) {
|
|
116
|
+
return Object.assign(create(), resolveGlobalOptions(globalOptions, args), args);
|
|
117
|
+
}
|
|
118
|
+
function resolveGlobalOptions(globalOptions, args) {
|
|
119
|
+
if (!globalOptions) return;
|
|
120
|
+
const shortNames = /* @__PURE__ */ new Set();
|
|
121
|
+
for (const schema of Object.values(args || {})) if (schema.type !== "positional" && schema.short) shortNames.add(schema.short);
|
|
122
|
+
const resolved = create();
|
|
123
|
+
for (const [name, schema] of globalOptions)
|
|
124
|
+
/**
|
|
125
|
+
* NOTE(kazupon): a copy, because the schema is the one that the plugin registered, which every
|
|
126
|
+
* command of the CLI shares. Only this command gives up the short name.
|
|
127
|
+
*/
|
|
128
|
+
resolved[name] = schema.short && shortNames.has(schema.short) ? {
|
|
129
|
+
...schema,
|
|
130
|
+
short: void 0
|
|
131
|
+
} : schema;
|
|
132
|
+
return resolved;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
89
135
|
* Log a message to the console.
|
|
90
136
|
*
|
|
91
137
|
* @param args - Arguments to log
|
|
@@ -173,9 +219,18 @@ const ARG_ERROR_RESOURCE_KEYS = [
|
|
|
173
219
|
"err:arg:invalid-type",
|
|
174
220
|
"err:arg:invalid-choice",
|
|
175
221
|
"err:arg:custom-parse",
|
|
176
|
-
"err:arg:unknown-option"
|
|
222
|
+
"err:arg:unknown-option",
|
|
223
|
+
"err:arg:unexpected-value",
|
|
224
|
+
"err:arg:missing-value",
|
|
225
|
+
"err:arg:conflict",
|
|
226
|
+
"err:arg:invalid-default"
|
|
227
|
+
];
|
|
228
|
+
const COMMAND_ERROR_RESOURCE_KEYS = [
|
|
229
|
+
"err:cmd:not-found",
|
|
230
|
+
"err:cmd:ambiguous",
|
|
231
|
+
"err:cmd:inconsistent-options",
|
|
232
|
+
"err:cmd:lazy-schema-mismatch"
|
|
177
233
|
];
|
|
178
|
-
const COMMAND_ERROR_RESOURCE_KEYS = ["err:cmd:not-found"];
|
|
179
234
|
const SUGGESTION_ERROR_RESOURCE_KEYS = ["err:suggestion:did-you-mean"];
|
|
180
235
|
//#endregion
|
|
181
236
|
//#region ../resources/locales/en-US.json
|
|
@@ -199,7 +254,14 @@ var en_US_default = {
|
|
|
199
254
|
"err:arg:invalid-choice": "Invalid value for {$displayName}: expected one of {$choices}",
|
|
200
255
|
"err:arg:custom-parse": "Invalid value for {$displayName}: {$reason}",
|
|
201
256
|
"err:arg:unknown-option": "Unknown option: {$rawName}",
|
|
257
|
+
"err:arg:unexpected-value": "Optional argument {$displayName} does not take a value",
|
|
258
|
+
"err:arg:missing-value": "Optional argument {$displayName} requires a value",
|
|
259
|
+
"err:arg:conflict": "{$displayName} conflicts with {$conflictDisplayName}",
|
|
260
|
+
"err:arg:invalid-default": "Invalid default for {$displayName}: {$actual} is not one of {$choices}",
|
|
202
261
|
"err:cmd:not-found": "Command not found: {$commandName}",
|
|
262
|
+
"err:cmd:ambiguous": "The command is ambiguous. Move options after the command name: {$candidatePaths}",
|
|
263
|
+
"err:cmd:inconsistent-options": "The options do not identify a consistent command. Move options after the command name.",
|
|
264
|
+
"err:cmd:lazy-schema-mismatch": "The lazy command definition does not match its loaded options. Declare leading options in the lazy definition or move options after the command name: {$commandPath}",
|
|
203
265
|
"err:suggestion:did-you-mean": "Did you mean {$name}?"
|
|
204
266
|
};
|
|
205
267
|
//#endregion
|
|
@@ -282,10 +344,36 @@ function namespacedId(id) {
|
|
|
282
344
|
* @returns A string representing the short and long option pair.
|
|
283
345
|
*/
|
|
284
346
|
function makeShortLongOptionPair(schema, name, toKebab) {
|
|
285
|
-
let key = `--${
|
|
347
|
+
let key = `--${resolveDisplayName(name, schema, toKebab)}`;
|
|
286
348
|
if (schema.short) key = `-${schema.short}, ${key}`;
|
|
287
349
|
return key;
|
|
288
350
|
}
|
|
351
|
+
/**
|
|
352
|
+
* Resolve the name that an argument is parsed and rendered under.
|
|
353
|
+
*
|
|
354
|
+
* @param name - The key of the argument.
|
|
355
|
+
* @param schema - The {@linkcode ArgSchema | argument schema}.
|
|
356
|
+
* @param toKebab - Whether to convert the name to kebab-case.
|
|
357
|
+
* @returns The name of the argument, kebab-cased when `toKebab` asks for it.
|
|
358
|
+
*/
|
|
359
|
+
function resolveDisplayName(name, schema, toKebab) {
|
|
360
|
+
return toKebab || schema.toKebab ? kebabnize(name) : name;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Collect the names that the arguments are parsed and rendered under.
|
|
364
|
+
*
|
|
365
|
+
* A positional argument is left out: it is never spelled as an option, so it cannot take the name of
|
|
366
|
+
* one. A `hidden` argument is counted, because it still owns the name that it parses.
|
|
367
|
+
*
|
|
368
|
+
* @param args - The {@linkcode Args | arguments} of a command, global options included.
|
|
369
|
+
* @param toKebab - Whether the command converts its names to kebab-case.
|
|
370
|
+
* @returns The set of names.
|
|
371
|
+
*/
|
|
372
|
+
function resolveOptionNames(args, toKebab) {
|
|
373
|
+
const names = /* @__PURE__ */ new Set();
|
|
374
|
+
for (const [name, schema] of Object.entries(args)) if (schema.type !== "positional") names.add(resolveDisplayName(name, schema, toKebab));
|
|
375
|
+
return names;
|
|
376
|
+
}
|
|
289
377
|
//#endregion
|
|
290
378
|
//#region src/localization.ts
|
|
291
379
|
/**
|
|
@@ -313,15 +401,21 @@ function localizable(ctx, cmd, translate) {
|
|
|
313
401
|
if (key.startsWith(ERROR_PREFIX_AND_KEY_SEPARATOR)) return resolveAndFormatResource(key, key, values);
|
|
314
402
|
const namaspacedArgKey = resolveKey(ARG_PREFIX_AND_KEY_SEPARATOR, ctx.name);
|
|
315
403
|
if (key.startsWith(namaspacedArgKey)) {
|
|
316
|
-
|
|
317
|
-
|
|
404
|
+
const argKey = key.slice(namaspacedArgKey.length);
|
|
405
|
+
/**
|
|
406
|
+
* NOTE(kazupon): an argument describes itself, whatever it is called. `no-` is stripped only
|
|
407
|
+
* to reach the option that a negated form negates, and only when there is no argument under
|
|
408
|
+
* the name as written: `no-emoji` may be an argument of its own, and it then has nothing to do
|
|
409
|
+
* with `emoji`.
|
|
410
|
+
*/
|
|
411
|
+
const ownSchema = ctx.args[argKey];
|
|
412
|
+
if (ownSchema) return ownSchema.description || "";
|
|
318
413
|
if (argKey.startsWith("no-")) {
|
|
319
|
-
|
|
320
|
-
|
|
414
|
+
const negatedKey = argKey.slice(3);
|
|
415
|
+
const schema = ctx.args[negatedKey];
|
|
416
|
+
if (schema && schema.type === "boolean" && schema.negatable) return `${en_US_default["NEGATABLE"]} ${makeShortLongOptionPair(schema, negatedKey, ctx.toKebab)}`;
|
|
321
417
|
}
|
|
322
|
-
|
|
323
|
-
if (!schema) return argKey;
|
|
324
|
-
return negatable && schema.type === "boolean" && schema.negatable ? `${en_US_default["NEGATABLE"]} ${makeShortLongOptionPair(schema, argKey, ctx.toKebab)}` : schema.description || "";
|
|
418
|
+
return argKey;
|
|
325
419
|
}
|
|
326
420
|
if (key === resolveKey("description", ctx.name)) return "";
|
|
327
421
|
else if (key === resolveKey("examples", ctx.name)) return await resolveExamples(ctx, cmd.examples);
|
|
@@ -351,4 +445,4 @@ function formatResource(resource, values = Object.create(null)) {
|
|
|
351
445
|
* @license MIT
|
|
352
446
|
*/
|
|
353
447
|
//#endregion
|
|
354
|
-
export { ARG_ERROR_RESOURCE_KEYS, ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_ERROR_RESOURCE_KEYS, COMMON_ARGS, en_US_default as DefaultResource, ERROR_PREFIX, ERROR_PREFIX_AND_KEY_SEPARATOR, PLUGIN_PREFIX, SUGGESTION_ERROR_RESOURCE_KEYS, create, deepFreeze, formatResource, getCommandSubCommands, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveKey, resolveLazyCommand };
|
|
448
|
+
export { ARG_ERROR_RESOURCE_KEYS, ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_ERROR_RESOURCE_KEYS, COMMON_ARGS, en_US_default as DefaultResource, ERROR_PREFIX, ERROR_PREFIX_AND_KEY_SEPARATOR, PLUGIN_PREFIX, SUGGESTION_ERROR_RESOURCE_KEYS, create, deepFreeze, formatResource, getCommandSubCommands, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveCommandArgs, resolveDisplayName, resolveExamples, resolveKey, resolveLazyCommand, resolveOptionNames };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gunshi/shared",
|
|
3
3
|
"description": "shared utils for gunshi",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0-beta.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"jsr-exports-lint": "^0.4.2",
|
|
56
56
|
"publint": "^0.3.20",
|
|
57
57
|
"tsdown": "0.21.0",
|
|
58
|
-
"@gunshi/resources": "0.
|
|
59
|
-
"gunshi": "0.
|
|
58
|
+
"@gunshi/resources": "1.0.0-beta.0",
|
|
59
|
+
"gunshi": "1.0.0-beta.0"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"build": "tsdown",
|