@gunshi/shared 1.0.0-beta.0 → 1.0.0-beta.2
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 +46 -67
- package/lib/index.js +39 -33
- package/package.json +4 -4
package/lib/index.d.ts
CHANGED
|
@@ -55,9 +55,6 @@ interface ArgToken {
|
|
|
55
55
|
*/
|
|
56
56
|
inlineValue?: boolean | undefined;
|
|
57
57
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Parser Options.
|
|
60
|
-
*/
|
|
61
58
|
//#endregion
|
|
62
59
|
//#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/resolver.d.ts
|
|
63
60
|
//#region src/resolver.d.ts
|
|
@@ -638,7 +635,7 @@ interface Args {
|
|
|
638
635
|
*
|
|
639
636
|
* @typeParam T - {@link Args | Arguments} which is an object that defines the command line arguments.
|
|
640
637
|
*/
|
|
641
|
-
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]
|
|
638
|
+
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]>; }> : {
|
|
642
639
|
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
643
640
|
};
|
|
644
641
|
/**
|
|
@@ -670,7 +667,7 @@ type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T
|
|
|
670
667
|
*
|
|
671
668
|
* @internal
|
|
672
669
|
*/
|
|
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;
|
|
670
|
+
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: IsPresentArg<A[Arg]> extends true ? unknown : V[Arg]; } & { -readonly [Arg in keyof A as IsPresentArg<A[Arg]> extends true ? Arg : never]: V[Arg]; } extends (infer P) ? { [K in keyof P]: P[K]; } : never;
|
|
674
671
|
/**
|
|
675
672
|
* Whether an argument with the schema `S` has a value: with a default, with `required: true` or as
|
|
676
673
|
* a required positional argument. For a union of schemas, only when each schema of the union has
|
|
@@ -678,16 +675,6 @@ type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -r
|
|
|
678
675
|
* value, as {@link FilterArgs} does.
|
|
679
676
|
*/
|
|
680
677
|
type IsPresentArg<S extends ArgSchema> = S extends ArgSchema ? true extends (S['default'] extends {} ? true : S['required'] extends true ? true : false) ? true : IsRequiredPositionalArg<S> : never;
|
|
681
|
-
/**
|
|
682
|
-
* Filters the arguments based on their default values.
|
|
683
|
-
*
|
|
684
|
-
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
685
|
-
* @typeParam V - Resolvable argument values.
|
|
686
|
-
* @typeParam K - Key of the {@link ArgSchema | argument schema} to filter by.
|
|
687
|
-
*
|
|
688
|
-
* @internal
|
|
689
|
-
*/
|
|
690
|
-
|
|
691
678
|
type IsRequiredPositionalArg<A extends ArgSchema> = A['type'] extends 'positional' ? A['multiple'] extends true ? A['required'] extends true ? true : false : MayBeUnrequired<A> extends true ? A['default'] extends {} ? true : false : true : false;
|
|
692
679
|
/**
|
|
693
680
|
* Whether the `required` of `A` may be `false`. An optional `required` of type `boolean`, as in
|
|
@@ -698,10 +685,6 @@ type IsRequiredPositionalArg<A extends ArgSchema> = A['type'] extends 'positiona
|
|
|
698
685
|
type MayBeUnrequired<A extends ArgSchema> = false extends A['required'] ? [A] extends [{
|
|
699
686
|
required: unknown;
|
|
700
687
|
}] ? true : boolean | undefined extends A['required'] ? false : true : false;
|
|
701
|
-
/**
|
|
702
|
-
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
703
|
-
*/
|
|
704
|
-
|
|
705
688
|
/**
|
|
706
689
|
* Tracks which arguments were explicitly provided by the user.
|
|
707
690
|
*
|
|
@@ -714,7 +697,7 @@ type MayBeUnrequired<A extends ArgSchema> = false extends A['required'] ? [A] ex
|
|
|
714
697
|
*
|
|
715
698
|
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
716
699
|
*/
|
|
717
|
-
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean };
|
|
700
|
+
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean; };
|
|
718
701
|
//#endregion
|
|
719
702
|
//#region ../gunshi/src/types.d.ts
|
|
720
703
|
/**
|
|
@@ -1302,19 +1285,6 @@ declare function resolveLazyCommand<G extends GunshiParamsConstraint = DefaultGu
|
|
|
1302
1285
|
* @returns A new object with the specified prototype
|
|
1303
1286
|
*/
|
|
1304
1287
|
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;
|
|
1318
1288
|
/**
|
|
1319
1289
|
* Log a message to the console.
|
|
1320
1290
|
*
|
|
@@ -1366,23 +1336,24 @@ type CommonArgType = {
|
|
|
1366
1336
|
declare const COMMON_ARGS: CommonArgType;
|
|
1367
1337
|
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "ARGUMENTS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES"];
|
|
1368
1338
|
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"];
|
|
1339
|
+
declare const ARG_ERROR_HINT_RESOURCE_KEYS: readonly ["err:arg:missing-value:hint"];
|
|
1369
1340
|
declare const COMMAND_ERROR_RESOURCE_KEYS: readonly ["err:cmd:not-found", "err:cmd:ambiguous", "err:cmd:inconsistent-options", "err:cmd:lazy-schema-mismatch"];
|
|
1370
1341
|
declare const SUGGESTION_ERROR_RESOURCE_KEYS: readonly ["err:suggestion:did-you-mean"];
|
|
1371
1342
|
//#endregion
|
|
1372
1343
|
//#region src/types.d.ts
|
|
1373
|
-
type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K] };
|
|
1344
|
+
type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K]; };
|
|
1374
1345
|
/**
|
|
1375
|
-
* Make all properties in T deeply
|
|
1346
|
+
* Make all properties in T deeply writable (not readonly)
|
|
1376
1347
|
*/
|
|
1377
|
-
type DeepWriteable<T> = T extends ((...args: any) => any) ? T : T extends readonly (infer U)[] ? DeepWriteable<U>[] : T extends object ? { -readonly [P in keyof T]: DeepWriteable<T[P]
|
|
1348
|
+
type DeepWriteable<T> = T extends ((...args: any) => any) ? T : T extends readonly (infer U)[] ? DeepWriteable<U>[] : T extends object ? { -readonly [P in keyof T]: DeepWriteable<T[P]>; } : T;
|
|
1378
1349
|
/**
|
|
1379
1350
|
* Remove index signature from object or record type.
|
|
1380
1351
|
*/
|
|
1381
|
-
type RemovedIndex<T> = RemoveIndexSignature<{ [K in keyof T]: T[K] }>;
|
|
1352
|
+
type RemovedIndex<T> = RemoveIndexSignature<{ [K in keyof T]: T[K]; }>;
|
|
1382
1353
|
/**
|
|
1383
1354
|
* Resolve a key on {@link Args}.
|
|
1384
1355
|
*/
|
|
1385
|
-
type KeyOfArgs<A extends Args> = keyof A | { [K in keyof A]: A[K]['type'] extends 'boolean' ? A[K]['negatable'] extends true ? `no-${Extract<K, string>}` : never : never }[keyof A];
|
|
1356
|
+
type KeyOfArgs<A extends Args> = keyof A | { [K in keyof A]: A[K]['type'] extends 'boolean' ? A[K]['negatable'] extends true ? `no-${Extract<K, string>}` : never : never; }[keyof A];
|
|
1386
1357
|
/**
|
|
1387
1358
|
* Generate a namespaced key.
|
|
1388
1359
|
*/
|
|
@@ -1399,6 +1370,10 @@ type CommandBuiltinResourceKeys = (typeof COMMAND_BUILTIN_RESOURCE_KEYS)[number]
|
|
|
1399
1370
|
* Args validation error resource keys.
|
|
1400
1371
|
*/
|
|
1401
1372
|
type ArgErrorResourceKeys = (typeof ARG_ERROR_RESOURCE_KEYS)[number];
|
|
1373
|
+
/**
|
|
1374
|
+
* Resource keys of the hints that follow an args validation error.
|
|
1375
|
+
*/
|
|
1376
|
+
type ArgErrorHintResourceKeys = (typeof ARG_ERROR_HINT_RESOURCE_KEYS)[number];
|
|
1402
1377
|
/**
|
|
1403
1378
|
* Command validation error resource keys.
|
|
1404
1379
|
*/
|
|
@@ -1410,7 +1385,7 @@ type SuggestionErrorResourceKeys = (typeof SUGGESTION_ERROR_RESOURCE_KEYS)[numbe
|
|
|
1410
1385
|
/**
|
|
1411
1386
|
* Error resource keys.
|
|
1412
1387
|
*/
|
|
1413
|
-
type ErrorResourceKeys = ArgErrorResourceKeys | CommandErrorResourceKeys | SuggestionErrorResourceKeys;
|
|
1388
|
+
type ErrorResourceKeys = ArgErrorResourceKeys | ArgErrorHintResourceKeys | CommandErrorResourceKeys | SuggestionErrorResourceKeys;
|
|
1414
1389
|
/**
|
|
1415
1390
|
* Built-in resource keys.
|
|
1416
1391
|
*/
|
|
@@ -1433,21 +1408,17 @@ type CommandArgKeys<A extends Args, C = {}, K extends string = GenerateNamespace
|
|
|
1433
1408
|
/**
|
|
1434
1409
|
* Resolve translation keys for command context.
|
|
1435
1410
|
*/
|
|
1436
|
-
type ResolveTranslationKeys<A extends Args, C = {}
|
|
1437
|
-
// for
|
|
1438
|
-
|
|
1439
|
-
// for extended resources
|
|
1440
|
-
R extends string = keyof RemovedIndex<E>, T extends string = (C extends {
|
|
1411
|
+
type ResolveTranslationKeys<A extends Args, C = {} // for CommandContext
|
|
1412
|
+
, E extends Record<string, string> = {} // for extended resources
|
|
1413
|
+
, R extends string = keyof RemovedIndex<E>, T extends string = C extends {
|
|
1441
1414
|
name: infer N;
|
|
1442
|
-
} ? N extends string ? GenerateNamespacedKey<R, N> : R : R | CommandBuiltinKeys
|
|
1415
|
+
} ? N extends string ? GenerateNamespacedKey<R, N> : R : R | CommandBuiltinKeys, O = CommandArgKeys<A, C>> = ErrorResourceKeys | CommandBuiltinKeys | O | T;
|
|
1443
1416
|
/**
|
|
1444
1417
|
* Translation function interface
|
|
1445
1418
|
*/
|
|
1446
|
-
interface Translation<A extends Args, C = {}
|
|
1447
|
-
// for
|
|
1448
|
-
|
|
1449
|
-
// for extended resources
|
|
1450
|
-
K = ResolveTranslationKeys<A, C, E>> {
|
|
1419
|
+
interface Translation<A extends Args, C = {} // for CommandContext
|
|
1420
|
+
, E extends Record<string, string> = {} // for extended resources
|
|
1421
|
+
, K = ResolveTranslationKeys<A, C, E>> {
|
|
1451
1422
|
(key: K, values?: Record<string, unknown>): string;
|
|
1452
1423
|
}
|
|
1453
1424
|
//#endregion
|
|
@@ -1459,9 +1430,8 @@ K = ResolveTranslationKeys<A, C, E>> {
|
|
|
1459
1430
|
* @typeParam C - Additional context type for command localization.
|
|
1460
1431
|
* @typeParam E - Extended resource keys type.
|
|
1461
1432
|
*/
|
|
1462
|
-
interface Localization<A extends Args, C = {}
|
|
1463
|
-
|
|
1464
|
-
E extends Record<string, string> = {}> {
|
|
1433
|
+
interface Localization<A extends Args, C = {} // for CommandContext
|
|
1434
|
+
, E extends Record<string, string> = {}> {
|
|
1465
1435
|
<K = ResolveTranslationKeys<A, C, E>>(key: K, values?: Record<string, unknown>): Promise<string>;
|
|
1466
1436
|
}
|
|
1467
1437
|
/**
|
|
@@ -1478,11 +1448,9 @@ E extends Record<string, string> = {}> {
|
|
|
1478
1448
|
* @param translate - Translation function
|
|
1479
1449
|
* @returns Localizable function
|
|
1480
1450
|
*/
|
|
1481
|
-
declare function localizable<A extends Args, C = {}
|
|
1482
|
-
// for
|
|
1483
|
-
E
|
|
1484
|
-
// for extended resources
|
|
1485
|
-
K = ResolveTranslationKeys<A, C, E>>(ctx: CommandContext, cmd: Command, translate?: Translation<A, C, E, K>): Localization<A, C, E>;
|
|
1451
|
+
declare function localizable<A extends Args, C = {} // for CommandContext
|
|
1452
|
+
, E extends Record<string, string> = {} // for extended resources
|
|
1453
|
+
, K = ResolveTranslationKeys<A, C, E>>(ctx: CommandContext, cmd: Command, translate?: Translation<A, C, E, K>): Localization<A, C, E>;
|
|
1486
1454
|
/**
|
|
1487
1455
|
* Format a resource message by replacing placeholder values.
|
|
1488
1456
|
*
|
|
@@ -1492,7 +1460,7 @@ K = ResolveTranslationKeys<A, C, E>>(ctx: CommandContext, cmd: Command, translat
|
|
|
1492
1460
|
*/
|
|
1493
1461
|
declare function formatResource(resource: string | undefined, values?: Record<string, unknown>): string | undefined;
|
|
1494
1462
|
//#endregion
|
|
1495
|
-
//#region ../resources/locales/en-US.d.ts
|
|
1463
|
+
//#region ../resources/locales/en-US.json.d.ts
|
|
1496
1464
|
declare const _exports: {
|
|
1497
1465
|
COMMAND: string;
|
|
1498
1466
|
COMMANDS: string;
|
|
@@ -1515,6 +1483,7 @@ declare const _exports: {
|
|
|
1515
1483
|
"err:arg:unknown-option": string;
|
|
1516
1484
|
"err:arg:unexpected-value": string;
|
|
1517
1485
|
"err:arg:missing-value": string;
|
|
1486
|
+
"err:arg:missing-value:hint": string;
|
|
1518
1487
|
"err:arg:conflict": string;
|
|
1519
1488
|
"err:arg:invalid-default": string;
|
|
1520
1489
|
"err:cmd:not-found": string;
|
|
@@ -1560,7 +1529,7 @@ declare function resolveArgKey<A extends Args = DefaultGunshiParams['args'], K e
|
|
|
1560
1529
|
* @param name - The command name.
|
|
1561
1530
|
* @returns Prefixed non-built-in key.
|
|
1562
1531
|
*/
|
|
1563
|
-
declare function resolveKey<T extends Record<string, string> = {}, K extends string =
|
|
1532
|
+
declare function resolveKey<T extends Record<string, string> = {}, K extends string = keyof T extends string ? keyof T : string>(key: K, name?: string): string;
|
|
1564
1533
|
/**
|
|
1565
1534
|
* Resolve command examples.
|
|
1566
1535
|
*
|
|
@@ -1609,12 +1578,22 @@ declare function resolveDisplayName(name: string, schema: ArgSchema, toKebab?: b
|
|
|
1609
1578
|
* @returns The set of names.
|
|
1610
1579
|
*/
|
|
1611
1580
|
declare function resolveOptionNames(args: Args, toKebab?: boolean): Set<string>;
|
|
1612
|
-
//#endregion
|
|
1613
|
-
//#region src/index.d.ts
|
|
1614
1581
|
/**
|
|
1615
|
-
*
|
|
1616
|
-
*
|
|
1617
|
-
|
|
1618
|
-
|
|
1582
|
+
* Resolve the arguments of a command, the way gunshi parses and renders them.
|
|
1583
|
+
*
|
|
1584
|
+
* The global options belong to no command definition, so they are merged into the arguments of the
|
|
1585
|
+
* command. An argument of the command shadows the global option of the same name, and it shadows the
|
|
1586
|
+
* same way by short name: a global option gives up its short name to an argument of the command that
|
|
1587
|
+
* claims the same letter, and keeps its long name.
|
|
1588
|
+
*
|
|
1589
|
+
* NOTE(kazupon): a copy of the function that the core of gunshi resolves the arguments with. The
|
|
1590
|
+
* core does not export its own, to keep it out of the public API. `@gunshi/plugin-completion`
|
|
1591
|
+
* completes the arguments with this copy, and the tests pin it to the results of the core.
|
|
1592
|
+
*
|
|
1593
|
+
* @param globalOptions - The global options that plugins registered with `addGlobalOption`.
|
|
1594
|
+
* @param args - The {@linkcode Args | arguments} that the command declares.
|
|
1595
|
+
* @returns The merged arguments.
|
|
1596
|
+
*/
|
|
1597
|
+
declare function resolveCommandArgs<A extends Args = Args>(globalOptions?: ReadonlyMap<string, ArgSchema>, args?: A): A;
|
|
1619
1598
|
//#endregion
|
|
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 };
|
|
1599
|
+
export { ARG_ERROR_HINT_RESOURCE_KEYS, ARG_ERROR_RESOURCE_KEYS, ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, type ArgErrorHintResourceKeys, type ArgErrorResourceKeys, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, type BuiltinResource, type BuiltinResourceKeys, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_ERROR_RESOURCE_KEYS, COMMON_ARGS, type CommandArgKeys, type CommandBuiltinArgsKeys, type CommandBuiltinKeys, type CommandBuiltinResourceKeys, type CommandErrorResourceKeys, type DeepWriteable, _exports as DefaultResource, ERROR_PREFIX, ERROR_PREFIX_AND_KEY_SEPARATOR, type ErrorResourceKeys, type GenerateNamespacedKey, type KeyOfArgs, Localization, PLUGIN_PREFIX, type RemovedIndex, type ResolveTranslationKeys, SUGGESTION_ERROR_RESOURCE_KEYS, type SuggestionErrorResourceKeys, type Translation, create, deepFreeze, formatResource, getCommandSubCommands, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveCommandArgs, resolveDisplayName, resolveExamples, resolveKey, resolveLazyCommand, resolveOptionNames };
|
package/lib/index.js
CHANGED
|
@@ -101,37 +101,6 @@ function create(obj = null) {
|
|
|
101
101
|
return Object.create(obj);
|
|
102
102
|
}
|
|
103
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
|
-
/**
|
|
135
104
|
* Log a message to the console.
|
|
136
105
|
*
|
|
137
106
|
* @param args - Arguments to log
|
|
@@ -225,6 +194,7 @@ const ARG_ERROR_RESOURCE_KEYS = [
|
|
|
225
194
|
"err:arg:conflict",
|
|
226
195
|
"err:arg:invalid-default"
|
|
227
196
|
];
|
|
197
|
+
const ARG_ERROR_HINT_RESOURCE_KEYS = ["err:arg:missing-value:hint"];
|
|
228
198
|
const COMMAND_ERROR_RESOURCE_KEYS = [
|
|
229
199
|
"err:cmd:not-found",
|
|
230
200
|
"err:cmd:ambiguous",
|
|
@@ -249,13 +219,14 @@ var en_US_default = {
|
|
|
249
219
|
help: "Display this help message",
|
|
250
220
|
version: "Display this version",
|
|
251
221
|
"err:arg:required-option": "Optional argument {$displayName} is required",
|
|
252
|
-
"err:arg:required-positional": "Positional argument {$
|
|
222
|
+
"err:arg:required-positional": "Positional argument {$displayName} is required",
|
|
253
223
|
"err:arg:invalid-type": "Invalid value for {$displayName}: expected {$expected}",
|
|
254
224
|
"err:arg:invalid-choice": "Invalid value for {$displayName}: expected one of {$choices}",
|
|
255
225
|
"err:arg:custom-parse": "Invalid value for {$displayName}: {$reason}",
|
|
256
226
|
"err:arg:unknown-option": "Unknown option: {$rawName}",
|
|
257
227
|
"err:arg:unexpected-value": "Optional argument {$displayName} does not take a value",
|
|
258
228
|
"err:arg:missing-value": "Optional argument {$displayName} requires a value",
|
|
229
|
+
"err:arg:missing-value:hint": "(to pass '{$next}' as its value, write '{$suggestion}')",
|
|
259
230
|
"err:arg:conflict": "{$displayName} conflicts with {$conflictDisplayName}",
|
|
260
231
|
"err:arg:invalid-default": "Invalid default for {$displayName}: {$actual} is not one of {$choices}",
|
|
261
232
|
"err:cmd:not-found": "Command not found: {$commandName}",
|
|
@@ -374,6 +345,41 @@ function resolveOptionNames(args, toKebab) {
|
|
|
374
345
|
for (const [name, schema] of Object.entries(args)) if (schema.type !== "positional") names.add(resolveDisplayName(name, schema, toKebab));
|
|
375
346
|
return names;
|
|
376
347
|
}
|
|
348
|
+
/**
|
|
349
|
+
* Resolve the arguments of a command, the way gunshi parses and renders them.
|
|
350
|
+
*
|
|
351
|
+
* The global options belong to no command definition, so they are merged into the arguments of the
|
|
352
|
+
* command. An argument of the command shadows the global option of the same name, and it shadows the
|
|
353
|
+
* same way by short name: a global option gives up its short name to an argument of the command that
|
|
354
|
+
* claims the same letter, and keeps its long name.
|
|
355
|
+
*
|
|
356
|
+
* NOTE(kazupon): a copy of the function that the core of gunshi resolves the arguments with. The
|
|
357
|
+
* core does not export its own, to keep it out of the public API. `@gunshi/plugin-completion`
|
|
358
|
+
* completes the arguments with this copy, and the tests pin it to the results of the core.
|
|
359
|
+
*
|
|
360
|
+
* @param globalOptions - The global options that plugins registered with `addGlobalOption`.
|
|
361
|
+
* @param args - The {@linkcode Args | arguments} that the command declares.
|
|
362
|
+
* @returns The merged arguments.
|
|
363
|
+
*/
|
|
364
|
+
function resolveCommandArgs(globalOptions, args) {
|
|
365
|
+
return Object.assign(create(), resolveGlobalOptions(globalOptions, args), args);
|
|
366
|
+
}
|
|
367
|
+
function resolveGlobalOptions(globalOptions, args) {
|
|
368
|
+
if (!globalOptions) return;
|
|
369
|
+
const shortNames = /* @__PURE__ */ new Set();
|
|
370
|
+
for (const schema of Object.values(args || {})) if (schema.type !== "positional" && schema.short) shortNames.add(schema.short);
|
|
371
|
+
const resolved = create();
|
|
372
|
+
for (const [name, schema] of globalOptions)
|
|
373
|
+
/**
|
|
374
|
+
* NOTE(kazupon): a copy, because the schema is the one that the plugin registered, which every
|
|
375
|
+
* command of the CLI shares. Only this command gives up the short name.
|
|
376
|
+
*/
|
|
377
|
+
resolved[name] = schema.short && shortNames.has(schema.short) ? {
|
|
378
|
+
...schema,
|
|
379
|
+
short: void 0
|
|
380
|
+
} : schema;
|
|
381
|
+
return resolved;
|
|
382
|
+
}
|
|
377
383
|
//#endregion
|
|
378
384
|
//#region src/localization.ts
|
|
379
385
|
/**
|
|
@@ -445,4 +451,4 @@ function formatResource(resource, values = Object.create(null)) {
|
|
|
445
451
|
* @license MIT
|
|
446
452
|
*/
|
|
447
453
|
//#endregion
|
|
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 };
|
|
454
|
+
export { ARG_ERROR_HINT_RESOURCE_KEYS, 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": "1.0.0-beta.
|
|
4
|
+
"version": "1.0.0-beta.2",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -50,13 +50,13 @@
|
|
|
50
50
|
}
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
+
"@gunshi/resources": "1.0.0-beta.2",
|
|
53
54
|
"deno": "^2.7.14",
|
|
55
|
+
"gunshi": "1.0.0-beta.2",
|
|
54
56
|
"jsr": "^0.14.3",
|
|
55
57
|
"jsr-exports-lint": "^0.4.2",
|
|
56
58
|
"publint": "^0.3.20",
|
|
57
|
-
"tsdown": "0.
|
|
58
|
-
"@gunshi/resources": "1.0.0-beta.0",
|
|
59
|
-
"gunshi": "1.0.0-beta.0"
|
|
59
|
+
"tsdown": "0.22.14"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"build": "tsdown",
|