gunshi 0.14.4 → 0.15.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.
@@ -1,4 +1,4 @@
1
- import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, renderHeader, renderUsage, renderValidationErrors, resolveLazyCommand, resolveOptionKey } from "./renderer-B2JVfepJ.js";
1
+ import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE$1 as DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, renderHeader, renderUsage, renderValidationErrors, resolveLazyCommand, resolveOptionKey } from "./renderer-BeuJazdk.js";
2
2
  import { parseArgs, resolveArgs } from "args-tokens";
3
3
 
4
4
  //#region src/locales/en-US.json
@@ -9,6 +9,7 @@ var USAGE = "USAGE";
9
9
  var OPTIONS = "OPTIONS";
10
10
  var EXAMPLES = "EXAMPLES";
11
11
  var FORMORE = "For more info, run any command with the `--help` flag:";
12
+ var NEGATABLE = "Negatable of";
12
13
  var help = "Display this help message";
13
14
  var version = "Display this version";
14
15
  var en_US_default = {
@@ -19,6 +20,7 @@ var en_US_default = {
19
20
  OPTIONS,
20
21
  EXAMPLES,
21
22
  FORMORE,
23
+ NEGATABLE,
22
24
  help,
23
25
  version
24
26
  };
@@ -60,7 +62,7 @@ var DefaultTranslation = class {
60
62
  //#endregion
61
63
  //#region src/context.ts
62
64
  const BUILT_IN_PREFIX_CODE = BUILT_IN_PREFIX.codePointAt(0);
63
- async function createCommandContext({ options, values, positionals, args, tokens, command, commandOptions, omitted = false }) {
65
+ async function createCommandContext({ options, values, positionals, rest, args, tokens, command, commandOptions, omitted = false }) {
64
66
  /**
65
67
  * normailize the options schema and values, to avoid prototype pollution
66
68
  */
@@ -121,6 +123,7 @@ async function createCommandContext({ options, values, positionals, args, tokens
121
123
  options: _options,
122
124
  values,
123
125
  positionals,
126
+ rest,
124
127
  _: args,
125
128
  tokens,
126
129
  log: commandOptions.usageSilent ? NOOP : log,
@@ -175,12 +178,16 @@ async function cli(args, entry, opts = {}) {
175
178
  const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions);
176
179
  if (!command) throw new Error(`Command not found: ${name || ""}`);
177
180
  const options = resolveArgOptions(command.options);
178
- const { values, positionals, error } = resolveArgs(options, tokens);
181
+ const { values, positionals, rest, error } = resolveArgs(options, tokens, {
182
+ optionGrouping: true,
183
+ allowNegative: true
184
+ });
179
185
  const omitted = !subCommand;
180
186
  const ctx = await createCommandContext({
181
187
  options,
182
188
  values,
183
189
  positionals,
190
+ rest,
184
191
  args,
185
192
  tokens,
186
193
  omitted,
@@ -0,0 +1,13 @@
1
+ import { Command } from "./types.d-zAq1WIpD.js";
2
+ import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues as ArgValues$1 } from "args-tokens";
3
+
4
+ //#region src/definition.d.ts
5
+ /**
6
+ * Define a {@link Command | command} with type inference
7
+ * @param definition A {@link Command | command} definition
8
+ * @returns A {@link Command | command} definition with type inference
9
+ */
10
+ declare function define<Options extends ArgOptions = ArgOptions>(definition: Command<Options>): Command<Options>;
11
+
12
+ //#endregion
13
+ export { ArgOptionSchema, ArgOptions$1 as ArgOptions, ArgValues$1 as ArgValues, define as define$1 };
@@ -1,25 +1,4 @@
1
- import { ArgOptions } from 'args-tokens';
2
- export { ArgOptionSchema, ArgOptions, ArgValues } from 'args-tokens';
3
- import { C as Command } from './types.d-Ce_-U-wp.js';
1
+ import "./types.d-zAq1WIpD.js";
2
+ import { ArgOptionSchema, ArgOptions, ArgValues, define$1 as define } from "./definition.d-DD_GraLw.js";
4
3
 
5
- /**
6
- * The entry for command deifinition.
7
- * @example
8
- * ```js
9
- * import { define } from 'gunshi/definition'
10
- * ```
11
- * @module
12
- */
13
- /**
14
- * @author kazuya kawaguchi (a.k.a. kazupon)
15
- * @license MIT
16
- */
17
-
18
- /**
19
- * Define a {@link Command | command} with type inference
20
- * @param definition A {@link Command | command} definition
21
- * @returns A {@link Command | command} definition with type inference
22
- */
23
- declare function define<Options extends ArgOptions = ArgOptions>(definition: Command<Options>): Command<Options>;
24
-
25
- export { define };
4
+ export { ArgOptionSchema, ArgOptions, ArgValues, define };
package/lib/definition.js CHANGED
@@ -1,3 +1,3 @@
1
- import { define } from "./definition-by5EpPkZ.js";
1
+ import { define } from "./definition-BAm6f1St.js";
2
2
 
3
3
  export { define };
@@ -1,13 +1,15 @@
1
- import { ArgOptions } from 'args-tokens';
2
- import { C as Command, a as CommandOptions } from './types.d-Ce_-U-wp.js';
1
+ import { Command, CommandOptions } from "./types.d-zAq1WIpD.js";
2
+ import { ArgOptions } from "args-tokens";
3
3
 
4
+ //#region src/generator.d.ts
4
5
  /**
5
- * Generate the command usage.
6
- * @param command - usage generate command, if you want to generate the usage of the default command where there are target commands and sub-commands, specify `null`.
7
- * @param entry - A {@link Command | entry command}
8
- * @param opts - A {@link CommandOptions | command options}
9
- * @returns A rendered usage.
10
- */
6
+ * Generate the command usage.
7
+ * @param command - usage generate command, if you want to generate the usage of the default command where there are target commands and sub-commands, specify `null`.
8
+ * @param entry - A {@link Command | entry command}
9
+ * @param opts - A {@link CommandOptions | command options}
10
+ * @returns A rendered usage.
11
+ */
11
12
  declare function generate<Options extends ArgOptions = ArgOptions>(command: string | null, entry: Command<Options>, opts?: CommandOptions<Options>): Promise<string>;
12
13
 
13
- export { generate };
14
+ //#endregion
15
+ export { generate };
package/lib/generator.js CHANGED
@@ -1,5 +1,5 @@
1
- import { create } from "./renderer-B2JVfepJ.js";
2
- import { cli } from "./cli-D4I22PHa.js";
1
+ import { create } from "./renderer-BeuJazdk.js";
2
+ import { cli } from "./cli--6qYDY8U.js";
3
3
 
4
4
  //#region src/generator.ts
5
5
  async function generate(command, entry, opts = {}) {
package/lib/index.d.ts CHANGED
@@ -1,25 +1,27 @@
1
- import { ArgOptions } from 'args-tokens';
2
- export { ArgOptionSchema, ArgOptions, ArgValues, parseArgs, resolveArgs } from 'args-tokens';
3
- import { C as Command, b as CommandRunner, a as CommandOptions, T as TranslationAdapter, c as TranslationAdapterFactoryOptions } from './types.d-Ce_-U-wp.js';
4
- export { f as CommandBuiltinKeys, d as CommandBuiltinOptionsKeys, e as CommandBuiltinResourceKeys, i as CommandContext, h as CommandEnvironment, g as CommandOptionKeys, j as CommandResource, k as CommandResourceFetcher, m as Commandable, D as DEFAULT_LOCALE, G as GenerateNamespacedKey, L as LazyCommand, R as RemovedIndex, l as TranslationAdapterFactory } from './types.d-Ce_-U-wp.js';
5
- export { define } from './definition.js';
1
+ import { Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions } from "./types.d-zAq1WIpD.js";
2
+ import { define$1 as define } from "./definition.d-DD_GraLw.js";
3
+ import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues, parseArgs, resolveArgs } from "args-tokens";
6
4
 
5
+ //#region src/cli.d.ts
7
6
  /**
8
- * Run the command.
9
- * @param args Command line arguments
10
- * @param entry A {@link Command | entry command} or an {@link CommandRunner | inline command runner}
11
- * @param opts A {@link CommandOptions | command options}
12
- * @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
13
- */
14
- declare function cli<Options extends ArgOptions = ArgOptions>(args: string[], entry: Command<Options> | CommandRunner<Options>, opts?: CommandOptions<Options>): Promise<string | undefined>;
7
+ * Run the command.
8
+ * @param args Command line arguments
9
+ * @param entry A {@link Command | entry command} or an {@link CommandRunner | inline command runner}
10
+ * @param opts A {@link CommandOptions | command options}
11
+ * @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
12
+ */
13
+ declare function cli<Options extends ArgOptions$1 = ArgOptions$1>(args: string[], entry: Command<Options> | CommandRunner<Options>, opts?: CommandOptions<Options>): Promise<string | undefined>;
15
14
 
15
+ //#endregion
16
+ //#region src/translation.d.ts
16
17
  declare class DefaultTranslation implements TranslationAdapter {
17
- #private;
18
- constructor(options: TranslationAdapterFactoryOptions);
19
- getResource(locale: string): Record<string, string> | undefined;
20
- setResource(locale: string, resource: Record<string, string>): void;
21
- getMessage(locale: string, key: string): string | undefined;
22
- translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;
18
+ #private;
19
+ constructor(options: TranslationAdapterFactoryOptions);
20
+ getResource(locale: string): Record<string, string> | undefined;
21
+ setResource(locale: string, resource: Record<string, string>): void;
22
+ getMessage(locale: string, key: string): string | undefined;
23
+ translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;
23
24
  }
24
25
 
25
- export { Command, CommandOptions, CommandRunner, DefaultTranslation, TranslationAdapter, TranslationAdapterFactoryOptions, cli };
26
+ //#endregion
27
+ export { ArgOptionSchema, ArgOptions, ArgValues, Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, DefaultTranslation, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions, cli, define, parseArgs, resolveArgs };
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { define } from "./definition-by5EpPkZ.js";
2
- import { DEFAULT_LOCALE } from "./renderer-B2JVfepJ.js";
3
- import { DefaultTranslation, cli } from "./cli-D4I22PHa.js";
1
+ import { define } from "./definition-BAm6f1St.js";
2
+ import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./renderer-BeuJazdk.js";
3
+ import { DefaultTranslation, cli } from "./cli--6qYDY8U.js";
4
4
  import { parseArgs, resolveArgs } from "args-tokens";
5
5
 
6
6
  export { DEFAULT_LOCALE, DefaultTranslation, cli, define, parseArgs, resolveArgs };
@@ -6,6 +6,7 @@
6
6
  "OPTIONS": "OPTIONS",
7
7
  "EXAMPLES": "EXAMPLES",
8
8
  "FORMORE": "For more info, run any command with the `--help` flag:",
9
+ "NEGATABLE": "Negatable of",
9
10
  "help": "Display this help message",
10
11
  "version": "Display this version"
11
12
  }
@@ -6,6 +6,7 @@
6
6
  "OPTIONS": "オプション",
7
7
  "EXAMPLES": "例",
8
8
  "FORMORE": "詳細は、コマンドと`--help`フラグを実行してください:",
9
+ "NEGATABLE": "否定可能な",
9
10
  "help": "このヘルプメッセージを表示",
10
11
  "version": "このバージョンを表示"
11
12
  }
@@ -224,6 +224,11 @@ function hasAllDefaultOptions(ctx) {
224
224
  function generateOptionsSymbols(ctx) {
225
225
  return hasOptions(ctx) ? hasAllDefaultOptions(ctx) ? `[${ctx.translate(resolveBuiltInKey("OPTIONS"))}]` : `<${ctx.translate(resolveBuiltInKey("OPTIONS"))}>` : "";
226
226
  }
227
+ function makeShortLongOptionPair(schema, name) {
228
+ let key = `--${name}`;
229
+ if (schema.short) key = `-${schema.short}, ${key}`;
230
+ return key;
231
+ }
227
232
  /**
228
233
  * Get options pairs for usage
229
234
  * @param ctx A {@link CommandContext | command context}
@@ -231,13 +236,17 @@ function generateOptionsSymbols(ctx) {
231
236
  */
232
237
  function getOptionsPairs(ctx) {
233
238
  return Object.entries(ctx.options).reduce((acc, [name, value]) => {
234
- let key = `--${name}`;
235
- if (value.short) key = `-${value.short}, ${key}`;
239
+ let key = makeShortLongOptionPair(value, name);
236
240
  if (value.type !== "boolean") key = value.default ? `${key} [${name}]` : `${key} <${name}>`;
237
241
  acc[name] = key;
242
+ if (value.type === "boolean" && !(name === "help" || name === "version")) acc[`no-${name}`] = `--no-${name}`;
238
243
  return acc;
239
244
  }, create());
240
245
  }
246
+ const resolveNegatableKey = (key) => key.split("no-")[1];
247
+ function resolveNegatableType(key, ctx) {
248
+ return ctx.options[key.startsWith("no-") ? resolveNegatableKey(key) : key].type;
249
+ }
241
250
  /**
242
251
  * Generate options usage
243
252
  * @param ctx A {@link CommandContext | command context}
@@ -246,10 +255,16 @@ function getOptionsPairs(ctx) {
246
255
  */
247
256
  async function generateOptionsUsage(ctx, optionsPairs) {
248
257
  const optionsMaxLength = Math.max(...Object.entries(optionsPairs).map(([_, value]) => value.length));
249
- const optionSchemaMaxLength = ctx.env.usageOptionType ? Math.max(...Object.entries(optionsPairs).map(([key, _]) => ctx.options[key].type.length)) : 0;
258
+ const optionSchemaMaxLength = ctx.env.usageOptionType ? Math.max(...Object.entries(optionsPairs).map(([key, _]) => resolveNegatableType(key, ctx).length)) : 0;
250
259
  const usages = await Promise.all(Object.entries(optionsPairs).map(([key, value]) => {
251
- const rawDesc = ctx.translate(resolveOptionKey(key));
252
- const optionsSchema = ctx.env.usageOptionType ? `[${ctx.options[key].type}] ` : "";
260
+ let rawDesc = ctx.translate(resolveOptionKey(key));
261
+ if (!rawDesc && key.startsWith("no-")) {
262
+ const name = resolveNegatableKey(key);
263
+ const schema = ctx.options[name];
264
+ const optionKey = makeShortLongOptionPair(schema, name);
265
+ rawDesc = `${ctx.translate(resolveBuiltInKey("NEGATABLE"))} ${optionKey}`;
266
+ }
267
+ const optionsSchema = ctx.env.usageOptionType ? `[${resolveNegatableType(key, ctx)}] ` : "";
253
268
  const desc = `${optionsSchema ? optionsSchema.padEnd(optionSchemaMaxLength + 3) : ""}${rawDesc}`;
254
269
  const option = `${value.padEnd(optionsMaxLength + ctx.env.middleMargin)}${desc}`;
255
270
  return `${option.padStart(ctx.env.leftMargin + option.length)}`;
@@ -266,4 +281,4 @@ function renderValidationErrors(_ctx, error) {
266
281
  }
267
282
 
268
283
  //#endregion
269
- export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, renderHeader, renderUsage, renderValidationErrors, resolveLazyCommand, resolveOptionKey };
284
+ export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE as DEFAULT_LOCALE$1, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, renderHeader, renderUsage, renderValidationErrors, resolveLazyCommand, resolveOptionKey };
package/lib/renderer.d.ts CHANGED
@@ -1,36 +1,32 @@
1
- import { ArgOptions } from 'args-tokens';
2
- import { i as CommandContext } from './types.d-Ce_-U-wp.js';
1
+ import { CommandContext } from "./types.d-zAq1WIpD.js";
2
+ import { ArgOptions } from "args-tokens";
3
3
 
4
+ //#region src/renderer/header.d.ts
4
5
  /**
5
- * @author kazuya kawaguchi (a.k.a. kazupon)
6
- * @license MIT
7
- */
8
-
9
- /**
10
- * Render the header.
11
- * @param ctx A {@link CommandContext | command context}
12
- * @returns A rendered header.
13
- */
6
+ * Render the header.
7
+ * @param ctx A {@link CommandContext | command context}
8
+ * @returns A rendered header.
9
+ */
14
10
  declare function renderHeader<Options extends ArgOptions = ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
15
11
 
12
+ //#endregion
13
+ //#region src/renderer/usage.d.ts
16
14
  /**
17
- * Render the usage.
18
- * @param ctx A {@link CommandContext | command context}
19
- * @returns A rendered usage.
20
- */
15
+ * Render the usage.
16
+ * @param ctx A {@link CommandContext | command context}
17
+ * @returns A rendered usage.
18
+ */
21
19
  declare function renderUsage<Options extends ArgOptions = ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
22
20
 
21
+ //#endregion
22
+ //#region src/renderer/validation.d.ts
23
23
  /**
24
- * @author kazuya kawaguchi (a.k.a. kazupon)
25
- * @license MIT
26
- */
27
-
28
- /**
29
- * Render the validation errors.
30
- * @param ctx A {@link CommandContext | command context}
31
- * @param error An {@link AggregateError} of option in `args-token` validation
32
- * @returns A rendered validation error.
33
- */
24
+ * Render the validation errors.
25
+ * @param ctx A {@link CommandContext | command context}
26
+ * @param error An {@link AggregateError} of option in `args-token` validation
27
+ * @returns A rendered validation error.
28
+ */
34
29
  declare function renderValidationErrors<Options extends ArgOptions = ArgOptions>(_ctx: CommandContext<Options>, error: AggregateError): Promise<string>;
35
30
 
36
- export { renderHeader, renderUsage, renderValidationErrors };
31
+ //#endregion
32
+ export { renderHeader, renderUsage, renderValidationErrors };
package/lib/renderer.js CHANGED
@@ -1,3 +1,3 @@
1
- import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-B2JVfepJ.js";
1
+ import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BeuJazdk.js";
2
2
 
3
3
  export { renderHeader, renderUsage, renderValidationErrors };
@@ -0,0 +1,403 @@
1
+ import { ArgOptions, ArgToken, ArgValues } from "args-tokens";
2
+
3
+ //#region rolldown:runtime
4
+
5
+
6
+
7
+ //#endregion
8
+ //#region src/constants.d.ts
9
+
10
+ declare namespace constants_d_exports {
11
+ export { BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, NOOP, OPTION_PREFIX, }
12
+ }
13
+ /**
14
+ * The default locale string, which format is BCP 47 language tag.
15
+ */
16
+ declare const DEFAULT_LOCALE = "en-US";
17
+ declare const BUILT_IN_PREFIX = "_";
18
+ declare const OPTION_PREFIX = "Option";
19
+ declare const BUILT_IN_KEY_SEPARATOR = ":";
20
+ declare const NOOP: () => void;
21
+ type CommonOptionType = {
22
+ readonly help: {
23
+ readonly type: 'boolean';
24
+ readonly short: 'h';
25
+ readonly description: string;
26
+ };
27
+ readonly version: {
28
+ readonly type: 'boolean';
29
+ readonly short: 'v';
30
+ readonly description: string;
31
+ };
32
+ };
33
+ declare const COMMON_OPTIONS: CommonOptionType;
34
+ declare const COMMAND_OPTIONS_DEFAULT: CommandOptions<ArgOptions>;
35
+ declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE"];
36
+
37
+ //#endregion
38
+ //#region src/types.d.ts
39
+ type Awaitable<T> = T | Promise<T>;
40
+ type RemoveIndexSignature<T> = {
41
+ [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
42
+ };
43
+ /**
44
+ * Remove index signature from object or record type.
45
+ */
46
+ type RemovedIndex<T> = RemoveIndexSignature<{
47
+ [K in keyof T]: T[K];
48
+ }>;
49
+ type KeyOfArgOptions<Options extends ArgOptions> = keyof Options | {
50
+ [K in keyof Options]: Options[K]['type'] extends 'boolean' ? `no-${Extract<K, string>}` : never;
51
+ }[keyof Options];
52
+ /**
53
+ * Generate a namespaced key.
54
+ */
55
+ type GenerateNamespacedKey<Key extends string, Prefixed extends string = typeof BUILT_IN_PREFIX> = `${Prefixed}${typeof BUILT_IN_KEY_SEPARATOR}${Key}`;
56
+ /**
57
+ * Command i18n built-in options keys.
58
+ */
59
+ type CommandBuiltinOptionsKeys = keyof (typeof constants_d_exports)['COMMON_OPTIONS'];
60
+ /**
61
+ * Command i18n built-in resource keys.
62
+ */
63
+ type CommandBuiltinResourceKeys = (typeof constants_d_exports)['COMMAND_BUILTIN_RESOURCE_KEYS'][number];
64
+ /**
65
+ * Command i18n built-in keys.
66
+ * The command i18n built-in keys are used to {@link CommandContext.translate | translate} function.
67
+ */
68
+ type CommandBuiltinKeys = GenerateNamespacedKey<CommandBuiltinOptionsKeys> | GenerateNamespacedKey<CommandBuiltinResourceKeys> | 'description' | 'examples';
69
+ /**
70
+ * Command i18n option keys.
71
+ * The command i18n option keys are used to {@link CommandContext.translate | translate} function.
72
+ */
73
+ type CommandOptionKeys<Options extends ArgOptions> = GenerateNamespacedKey<KeyOfArgOptions<RemovedIndex<Options>>, typeof OPTION_PREFIX>;
74
+ /**
75
+ * Command environment.
76
+ */
77
+ interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
78
+ /**
79
+ * Current working directory.
80
+ * @see {@link CommandOptions.cwd}
81
+ */
82
+ cwd: string | undefined;
83
+ /**
84
+ * Command name.
85
+ * @see {@link CommandOptions.name}
86
+ */
87
+ name: string | undefined;
88
+ /**
89
+ * Command description.
90
+ * @see {@link CommandOptions.description}
91
+ *
92
+ */
93
+ description: string | undefined;
94
+ /**
95
+ * Command version.
96
+ * @see {@link CommandOptions.version}
97
+ */
98
+ version: string | undefined;
99
+ /**
100
+ * Left margin of the command output.
101
+ * @default 2
102
+ * @see {@link CommandOptions.leftMargin}
103
+ */
104
+ leftMargin: number;
105
+ /**
106
+ * Middle margin of the command output.
107
+ * @default 10
108
+ * @see {@link CommandOptions.middleMargin}
109
+ */
110
+ middleMargin: number;
111
+ /**
112
+ * Whether to display the usage option type.
113
+ * @default false
114
+ * @see {@link CommandOptions.usageOptionType}
115
+ */
116
+ usageOptionType: boolean;
117
+ /**
118
+ * Whether to display the command usage.
119
+ * @default false
120
+ * @see {@link}
121
+ */
122
+ usageSilent: boolean;
123
+ /**
124
+ * Sub commands.
125
+ * @see {@link CommandOptions.subCommands}
126
+ */
127
+ subCommands: Map<string, Command<any> | LazyCommand<any>> | undefined;
128
+ /**
129
+ * Render function the command usage.
130
+ */
131
+ renderUsage: ((ctx: CommandContext<Options>) => Promise<string>) | null | undefined;
132
+ /**
133
+ * Render function the header section in the command usage.
134
+ */
135
+ renderHeader: ((ctx: CommandContext<Options>) => Promise<string>) | null | undefined;
136
+ /**
137
+ * Render function the validation errors.
138
+ */
139
+ renderValidationErrors: ((ctx: CommandContext<Options>, error: AggregateError) => Promise<string>) | null | undefined;
140
+ }
141
+ /**
142
+ * Command options.
143
+ */
144
+ interface CommandOptions<Options extends ArgOptions = ArgOptions> {
145
+ /**
146
+ * Current working directory.
147
+ */
148
+ cwd?: string;
149
+ /**
150
+ * Command program name.
151
+ */
152
+ name?: string;
153
+ /**
154
+ * Command program description.
155
+ *
156
+ */
157
+ description?: string;
158
+ /**
159
+ * Command program version.
160
+ */
161
+ version?: string;
162
+ /**
163
+ * Command program locale.
164
+ */
165
+ locale?: string | Intl.Locale;
166
+ /**
167
+ * Sub commands.
168
+ */
169
+ subCommands?: Map<string, Command<any> | LazyCommand<any>>;
170
+ /**
171
+ * Left margin of the command output.
172
+ */
173
+ leftMargin?: number;
174
+ /**
175
+ * Middle margin of the command output.
176
+ */
177
+ middleMargin?: number;
178
+ /**
179
+ * Whether to display the usage option type.
180
+ */
181
+ usageOptionType?: boolean;
182
+ /**
183
+ * Whether to display the command usage.
184
+ */
185
+ usageSilent?: boolean;
186
+ /**
187
+ * Render function the command usage.
188
+ */
189
+ renderUsage?: ((ctx: Readonly<CommandContext<Options>>) => Promise<string>) | null;
190
+ /**
191
+ * Render function the header section in the command usage.
192
+ */
193
+ renderHeader?: ((ctx: Readonly<CommandContext<Options>>) => Promise<string>) | null;
194
+ /**
195
+ * Render function the validation errors.
196
+ */
197
+ renderValidationErrors?: ((ctx: Readonly<CommandContext<Options>>, error: AggregateError) => Promise<string>) | null;
198
+ /**
199
+ * Translation adapter factory.
200
+ */
201
+ translationAdapterFactory?: TranslationAdapterFactory;
202
+ }
203
+ /**
204
+ * Command context.
205
+ * Command context is the context of the command execution.
206
+ */
207
+ interface CommandContext<Options extends ArgOptions = ArgOptions, Values = ArgValues<Options>> {
208
+ /**
209
+ * Command name, that is the command that is executed.
210
+ * The command name is same {@link CommandEnvironment.name}.
211
+ */
212
+ name: string | undefined;
213
+ /**
214
+ * Command description, that is the description of the command that is executed.
215
+ * The command description is same {@link CommandEnvironment.description}.
216
+ */
217
+ description: string | undefined;
218
+ /**
219
+ * Command locale, that is the locale of the command that is executed.
220
+ */
221
+ locale: Intl.Locale;
222
+ /**
223
+ * Command environment, that is the environment of the command that is executed.
224
+ * The command environment is same {@link CommandEnvironment}.
225
+ */
226
+ env: Readonly<CommandEnvironment<Options>>;
227
+ /**
228
+ * Command options, that is the options of the command that is executed.
229
+ * The command options is same {@link Command.options}.
230
+ */
231
+ options: Options;
232
+ /**
233
+ * Command values, that is the values of the command that is executed.
234
+ * Resolve values with `resolveArgs` from command arguments and {@link Command.options}.
235
+ */
236
+ values: Values;
237
+ /**
238
+ * Command positionals arguments, that is the positionals of the command that is executed.
239
+ * Resolve positionals with `resolveArgs` from command arguments.
240
+ */
241
+ positionals: string[];
242
+ /**
243
+ * Command rest arguments, that is the remaining argument not resolved by the optional command option delimiter `--`.
244
+ */
245
+ rest: string[];
246
+ /**
247
+ * Original command line arguments.
248
+ * This argument is passed from `cli` function.
249
+ */
250
+ _: string[];
251
+ /**
252
+ * Argument tokens, that is parsed by `parseArgs` function.
253
+ */
254
+ tokens: ArgToken[];
255
+ /**
256
+ * Whether the currently executing command has been executed with the sub-command name omitted.
257
+ */
258
+ omitted: boolean;
259
+ /**
260
+ * Output a message.
261
+ * If {@link CommandEnvironment.usageSilent} is true, the message is not output.
262
+ * @param message an output message, @see {@link console.log}
263
+ * @param optionalParams an optional parameters, @see {@link console.log}
264
+ */
265
+ log: (message?: any, ...optionalParams: any[]) => void;
266
+ /**
267
+ * Load sub-commands.
268
+ * The loaded commands are cached and returned when called again.
269
+ * @returns loaded commands.
270
+ */
271
+ loadCommands: () => Promise<Command<Options>[]>;
272
+ /**
273
+ * Translate function.
274
+ * @param key the key to be translated
275
+ * @param values the values to be formatted
276
+ * @returns A translated string.
277
+ */
278
+ translate: <T extends string = CommandBuiltinKeys, O = CommandOptionKeys<Options>, Key = CommandBuiltinKeys | O | T>(key: Key, values?: Record<string, unknown>) => string;
279
+ }
280
+ /**
281
+ * Command interface.
282
+ */
283
+ interface Command<Options extends ArgOptions = ArgOptions> {
284
+ /**
285
+ * Command name.
286
+ * It's used to find command line arguments to execute from sub commands, and it's recommended to specify.
287
+ */
288
+ name?: string;
289
+ /**
290
+ * Command description.
291
+ * It's used to describe the command in usage and it's recommended to specify.
292
+ */
293
+ description?: string;
294
+ /**
295
+ * Command options.
296
+ * Each option can include a description property to describe the option in usage.
297
+ */
298
+ options?: Options;
299
+ /**
300
+ * Command examples.
301
+ * examples of how to use the command.
302
+ */
303
+ examples?: string;
304
+ /**
305
+ * Command runner. it's the command to be executed
306
+ */
307
+ run: CommandRunner<Options>;
308
+ /**
309
+ * Command resource fetcher.
310
+ */
311
+ resource?: CommandResourceFetcher<Options>;
312
+ }
313
+ /**
314
+ * Command resource.
315
+ */
316
+ type CommandResource<Options extends ArgOptions = ArgOptions> = {
317
+ /**
318
+ * Command description.
319
+ */
320
+ description: string;
321
+ /**
322
+ * Examples usage.
323
+ */
324
+ examples: string;
325
+ } & {
326
+ [Option in GenerateNamespacedKey<KeyOfArgOptions<RemovedIndex<Options>>, typeof OPTION_PREFIX>]: string;
327
+ } & {
328
+ [key: string]: string;
329
+ };
330
+ /**
331
+ * Command resource fetcher.
332
+ * @param ctx A {@link CommandContext | command context}
333
+ * @returns A fetched {@link CommandResource | command resource}.
334
+ */
335
+ type CommandResourceFetcher<Options extends ArgOptions = ArgOptions, Values = ArgValues<Options>> = (ctx: Readonly<CommandContext<Options, Values>>) => Promise<CommandResource<Options>>;
336
+ /**
337
+ * Translation adapter factory.
338
+ */
339
+ type TranslationAdapterFactory = (options: TranslationAdapterFactoryOptions) => TranslationAdapter;
340
+ /**
341
+ * Translation adapter factory options.
342
+ */
343
+ interface TranslationAdapterFactoryOptions {
344
+ /**
345
+ * A locale.
346
+ */
347
+ locale: string;
348
+ /**
349
+ * A fallback locale.
350
+ */
351
+ fallbackLocale: string;
352
+ }
353
+ /**
354
+ * Translation adapter.
355
+ * This adapter is used to custom message formatter like {@link https://github.com/intlify/vue-i18n/blob/master/spec/syntax.ebnf | Intlify message format}, {@link https://github.com/tc39/proposal-intl-messageformat | `Intl.MessageFormat` (MF2)}, and etc.
356
+ * This adapter will support localization with your preferred message format.
357
+ */
358
+ interface TranslationAdapter<MessageResource = string> {
359
+ /**
360
+ * Get a resource of locale.
361
+ * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
362
+ * @returns A resource of locale. if resource not found, return `undefined`.
363
+ */
364
+ getResource(locale: string): Record<string, string> | undefined;
365
+ /**
366
+ * Set a resource of locale.
367
+ * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
368
+ * @param resource A resource of locale
369
+ */
370
+ setResource(locale: string, resource: Record<string, string>): void;
371
+ /**
372
+ * Get a message of locale.
373
+ * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
374
+ * @param key A key of message resource
375
+ * @returns A message of locale. if message not found, return `undefined`.
376
+ */
377
+ getMessage(locale: string, key: string): MessageResource | undefined;
378
+ /**
379
+ * Translate a message.
380
+ * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
381
+ * @param key A key of message resource
382
+ * @param values A values to be resolved in the message
383
+ * @returns A translated message, if message is not translated, return `undefined`.
384
+ */
385
+ translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;
386
+ }
387
+ /**
388
+ * Command runner.
389
+ * @param ctx A {@link CommandContext | command context}
390
+ */
391
+ type CommandRunner<Options extends ArgOptions = ArgOptions> = (ctx: Readonly<CommandContext<Options>>) => Awaitable<void>;
392
+ /**
393
+ * Lazy command interface.
394
+ * Lazy command that's not loaded until it is executed.
395
+ */
396
+ type LazyCommand<Options extends ArgOptions = ArgOptions> = () => Awaitable<Command<Options>>;
397
+ /**
398
+ * Define a command type.
399
+ */
400
+ type Commandable<Options extends ArgOptions> = Command<Options> | LazyCommand<Options>;
401
+
402
+ //#endregion
403
+ export { Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gunshi",
3
3
  "description": "Modern javascript command-line library",
4
- "version": "0.14.4",
4
+ "version": "0.15.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -71,7 +71,7 @@
71
71
  }
72
72
  },
73
73
  "dependencies": {
74
- "args-tokens": "^0.14.0"
74
+ "args-tokens": "^0.15.1"
75
75
  },
76
76
  "devDependencies": {
77
77
  "@eslint/markdown": "^6.3.0",
@@ -100,10 +100,11 @@
100
100
  "knip": "^5.50.2",
101
101
  "lint-staged": "^15.5.0",
102
102
  "messageformat": "4.0.0-10",
103
- "pkg-pr-new": "^0.0.42",
103
+ "pkg-pr-new": "^0.0.43",
104
104
  "prettier": "^3.5.3",
105
105
  "publint": "^0.3.11",
106
- "tsdown": "0.6.10",
106
+ "tsdown": "^0.9.1",
107
+ "tsdown-jsr-exports-lint": "^0.1.4",
107
108
  "typedoc": "^0.28.2",
108
109
  "typedoc-plugin-markdown": "^4.6.2",
109
110
  "typedoc-vitepress-theme": "^1.1.2",
@@ -152,7 +153,7 @@
152
153
  "lint:knip": "knip",
153
154
  "lint:prettier": "prettier . --check",
154
155
  "release": "bumpp --commit \"release: v%s\" --all --push --tag",
155
- "test": "vitest run",
156
+ "test": "vitest --typecheck run",
156
157
  "typecheck": "pnpm run --stream --color \"/^typecheck:/\"",
157
158
  "typecheck:deno": "deno check --all ./src",
158
159
  "typecheck:tsc": "tsc --noEmit"
@@ -1,417 +0,0 @@
1
- import { ArgOptions, ArgValues, ArgToken } from 'args-tokens';
2
-
3
- /**
4
- * @author kazuya kawaguchi (a.k.a. kazupon)
5
- * @license MIT
6
- */
7
-
8
- /**
9
- * The default locale string, which format is BCP 47 language tag.
10
- */
11
- declare const DEFAULT_LOCALE = "en-US";
12
- declare const BUILT_IN_PREFIX = "_";
13
- declare const OPTION_PREFIX = "Option";
14
- declare const BUILT_IN_KEY_SEPARATOR = ":";
15
- declare const NOOP: () => void;
16
- type CommonOptionType = {
17
- readonly help: {
18
- readonly type: "boolean"
19
- readonly short: "h"
20
- readonly description: string
21
- }
22
- readonly version: {
23
- readonly type: "boolean"
24
- readonly short: "v"
25
- readonly description: string
26
- }
27
- };
28
- declare const COMMON_OPTIONS: CommonOptionType;
29
- declare const COMMAND_OPTIONS_DEFAULT: CommandOptions<ArgOptions>;
30
- declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "OPTIONS", "EXAMPLES", "FORMORE"];
31
-
32
- declare const __constants_ts_BUILT_IN_KEY_SEPARATOR: typeof BUILT_IN_KEY_SEPARATOR;
33
- declare const __constants_ts_BUILT_IN_PREFIX: typeof BUILT_IN_PREFIX;
34
- declare const __constants_ts_COMMAND_BUILTIN_RESOURCE_KEYS: typeof COMMAND_BUILTIN_RESOURCE_KEYS;
35
- declare const __constants_ts_COMMAND_OPTIONS_DEFAULT: typeof COMMAND_OPTIONS_DEFAULT;
36
- declare const __constants_ts_COMMON_OPTIONS: typeof COMMON_OPTIONS;
37
- declare const __constants_ts_DEFAULT_LOCALE: typeof DEFAULT_LOCALE;
38
- declare const __constants_ts_NOOP: typeof NOOP;
39
- declare const __constants_ts_OPTION_PREFIX: typeof OPTION_PREFIX;
40
- declare namespace __constants_ts {
41
- export {
42
- __constants_ts_BUILT_IN_KEY_SEPARATOR as BUILT_IN_KEY_SEPARATOR,
43
- __constants_ts_BUILT_IN_PREFIX as BUILT_IN_PREFIX,
44
- __constants_ts_COMMAND_BUILTIN_RESOURCE_KEYS as COMMAND_BUILTIN_RESOURCE_KEYS,
45
- __constants_ts_COMMAND_OPTIONS_DEFAULT as COMMAND_OPTIONS_DEFAULT,
46
- __constants_ts_COMMON_OPTIONS as COMMON_OPTIONS,
47
- __constants_ts_DEFAULT_LOCALE as DEFAULT_LOCALE,
48
- __constants_ts_NOOP as NOOP,
49
- __constants_ts_OPTION_PREFIX as OPTION_PREFIX,
50
- };
51
- }
52
-
53
- type Awaitable<T> = T | Promise<T>;
54
- type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : K] : T[K] };
55
- /**
56
- * Remove index signature from object or record type.
57
- */
58
- type RemovedIndex<T> = RemoveIndexSignature<{ [K in keyof T] : T[K] }>;
59
- /**
60
- * Generate a namespaced key.
61
- */
62
- type GenerateNamespacedKey<
63
- Key extends string,
64
- Prefixed extends string = typeof BUILT_IN_PREFIX
65
- > = `${Prefixed}${typeof BUILT_IN_KEY_SEPARATOR}${Key}`;
66
- /**
67
- * Command i18n built-in options keys.
68
- */
69
- type CommandBuiltinOptionsKeys = keyof (typeof __constants_ts)["COMMON_OPTIONS"];
70
- /**
71
- * Command i18n built-in resource keys.
72
- */
73
- type CommandBuiltinResourceKeys = (typeof __constants_ts)["COMMAND_BUILTIN_RESOURCE_KEYS"][number];
74
- /**
75
- * Command i18n built-in keys.
76
- * The command i18n built-in keys are used to {@link CommandContext.translate | translate} function.
77
- */
78
- type CommandBuiltinKeys = GenerateNamespacedKey<CommandBuiltinOptionsKeys> | GenerateNamespacedKey<CommandBuiltinResourceKeys> | "description" | "examples";
79
- /**
80
- * Command i18n option keys.
81
- * The command i18n option keys are used to {@link CommandContext.translate | translate} function.
82
- */
83
- type CommandOptionKeys<Options extends ArgOptions> = GenerateNamespacedKey<keyof RemovedIndex<Options>, typeof OPTION_PREFIX>;
84
- /**
85
- * Command environment.
86
- */
87
- interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
88
- /**
89
- * Current working directory.
90
- * @see {@link CommandOptions.cwd}
91
- */
92
- cwd: string | undefined;
93
- /**
94
- * Command name.
95
- * @see {@link CommandOptions.name}
96
- */
97
- name: string | undefined;
98
- /**
99
- * Command description.
100
- * @see {@link CommandOptions.description}
101
- *
102
- */
103
- description: string | undefined;
104
- /**
105
- * Command version.
106
- * @see {@link CommandOptions.version}
107
- */
108
- version: string | undefined;
109
- /**
110
- * Left margin of the command output.
111
- * @default 2
112
- * @see {@link CommandOptions.leftMargin}
113
- */
114
- leftMargin: number;
115
- /**
116
- * Middle margin of the command output.
117
- * @default 10
118
- * @see {@link CommandOptions.middleMargin}
119
- */
120
- middleMargin: number;
121
- /**
122
- * Whether to display the usage option type.
123
- * @default false
124
- * @see {@link CommandOptions.usageOptionType}
125
- */
126
- usageOptionType: boolean;
127
- /**
128
- * Whether to display the command usage.
129
- * @default false
130
- * @see {@link}
131
- */
132
- usageSilent: boolean;
133
- /**
134
- * Sub commands.
135
- * @see {@link CommandOptions.subCommands}
136
- */
137
- subCommands: Map<string, Command<any> | LazyCommand<any>> | undefined;
138
- /**
139
- * Render function the command usage.
140
- */
141
- renderUsage: ((ctx: CommandContext<Options>) => Promise<string>) | null | undefined;
142
- /**
143
- * Render function the header section in the command usage.
144
- */
145
- renderHeader: ((ctx: CommandContext<Options>) => Promise<string>) | null | undefined;
146
- /**
147
- * Render function the validation errors.
148
- */
149
- renderValidationErrors: ((ctx: CommandContext<Options>, error: AggregateError) => Promise<string>) | null | undefined;
150
- }
151
- /**
152
- * Command options.
153
- */
154
- interface CommandOptions<Options extends ArgOptions = ArgOptions> {
155
- /**
156
- * Current working directory.
157
- */
158
- cwd?: string;
159
- /**
160
- * Command program name.
161
- */
162
- name?: string;
163
- /**
164
- * Command program description.
165
- *
166
- */
167
- description?: string;
168
- /**
169
- * Command program version.
170
- */
171
- version?: string;
172
- /**
173
- * Command program locale.
174
- */
175
- locale?: string | Intl.Locale;
176
- /**
177
- * Sub commands.
178
- */
179
- subCommands?: Map<string, Command<any> | LazyCommand<any>>;
180
- /**
181
- * Left margin of the command output.
182
- */
183
- leftMargin?: number;
184
- /**
185
- * Middle margin of the command output.
186
- */
187
- middleMargin?: number;
188
- /**
189
- * Whether to display the usage option type.
190
- */
191
- usageOptionType?: boolean;
192
- /**
193
- * Whether to display the command usage.
194
- */
195
- usageSilent?: boolean;
196
- /**
197
- * Render function the command usage.
198
- */
199
- renderUsage?: ((ctx: Readonly<CommandContext<Options>>) => Promise<string>) | null;
200
- /**
201
- * Render function the header section in the command usage.
202
- */
203
- renderHeader?: ((ctx: Readonly<CommandContext<Options>>) => Promise<string>) | null;
204
- /**
205
- * Render function the validation errors.
206
- */
207
- renderValidationErrors?: ((ctx: Readonly<CommandContext<Options>>, error: AggregateError) => Promise<string>) | null;
208
- /**
209
- * Translation adapter factory.
210
- */
211
- translationAdapterFactory?: TranslationAdapterFactory;
212
- }
213
- /**
214
- * Command context.
215
- * Command context is the context of the command execution.
216
- */
217
- interface CommandContext<
218
- Options extends ArgOptions = ArgOptions,
219
- Values = ArgValues<Options>
220
- > {
221
- /**
222
- * Command name, that is the command that is executed.
223
- * The command name is same {@link CommandEnvironment.name}.
224
- */
225
- name: string | undefined;
226
- /**
227
- * Command description, that is the description of the command that is executed.
228
- * The command description is same {@link CommandEnvironment.description}.
229
- */
230
- description: string | undefined;
231
- /**
232
- * Command locale, that is the locale of the command that is executed.
233
- */
234
- locale: Intl.Locale;
235
- /**
236
- * Command environment, that is the environment of the command that is executed.
237
- * The command environment is same {@link CommandEnvironment}.
238
- */
239
- env: Readonly<CommandEnvironment<Options>>;
240
- /**
241
- * Command options, that is the options of the command that is executed.
242
- * The command options is same {@link Command.options}.
243
- */
244
- options: Options;
245
- /**
246
- * Command values, that is the values of the command that is executed.
247
- * Resolve values with `resolveArgs` from command arguments and {@link Command.options}.
248
- */
249
- values: Values;
250
- /**
251
- * Command positionals arguments, that is the positionals of the command that is executed.
252
- * Resolve positionals with `resolveArgs` from command arguments.
253
- */
254
- positionals: string[];
255
- /**
256
- * Original command line arguments.
257
- * This argument is passed from `cli` function.
258
- */
259
- _: string[];
260
- /**
261
- * Argument tokens, that is parsed by `parseArgs` function.
262
- */
263
- tokens: ArgToken[];
264
- /**
265
- * Whether the currently executing command has been executed with the sub-command name omitted.
266
- */
267
- omitted: boolean;
268
- /**
269
- * Output a message.
270
- * If {@link CommandEnvironment.usageSilent} is true, the message is not output.
271
- * @param message an output message, @see {@link console.log}
272
- * @param optionalParams an optional parameters, @see {@link console.log}
273
- */
274
- log: (message?: any, ...optionalParams: any[]) => void;
275
- /**
276
- * Load sub-commands.
277
- * The loaded commands are cached and returned when called again.
278
- * @returns loaded commands.
279
- */
280
- loadCommands: () => Promise<Command<Options>[]>;
281
- /**
282
- * Translate function.
283
- * @param key the key to be translated
284
- * @param values the values to be formatted
285
- * @returns A translated string.
286
- */
287
- translate: <
288
- T extends string = CommandBuiltinKeys,
289
- O = CommandOptionKeys<Options>,
290
- Key = CommandBuiltinKeys | O | T
291
- >(key: Key, values?: Record<string, unknown>) => string;
292
- }
293
- /**
294
- * Command interface.
295
- */
296
- interface Command<Options extends ArgOptions = ArgOptions> {
297
- /**
298
- * Command name.
299
- * It's used to find command line arguments to execute from sub commands, and it's recommended to specify.
300
- */
301
- name?: string;
302
- /**
303
- * Command description.
304
- * It's used to describe the command in usage and it's recommended to specify.
305
- */
306
- description?: string;
307
- /**
308
- * Command options.
309
- * Each option can include a description property to describe the option in usage.
310
- */
311
- options?: Options;
312
- /**
313
- * Command examples.
314
- * examples of how to use the command.
315
- */
316
- examples?: string;
317
- /**
318
- * Command runner. it's the command to be executed
319
- */
320
- run: CommandRunner<Options>;
321
- /**
322
- * Command resource fetcher.
323
- */
324
- resource?: CommandResourceFetcher<Options>;
325
- }
326
- /**
327
- * Command resource.
328
- */
329
- type CommandResource<Options extends ArgOptions = ArgOptions> = {
330
- /**
331
- * Command description.
332
- */
333
- description: string
334
- /**
335
- * Examples usage.
336
- */
337
- examples: string
338
- } & { [Option in GenerateNamespacedKey<keyof RemovedIndex<Options>, typeof OPTION_PREFIX>] : string } & {
339
- [key: string]: string
340
- };
341
- /**
342
- * Command resource fetcher.
343
- * @param ctx A {@link CommandContext | command context}
344
- * @returns A fetched {@link CommandResource | command resource}.
345
- */
346
- type CommandResourceFetcher<
347
- Options extends ArgOptions = ArgOptions,
348
- Values = ArgValues<Options>
349
- > = (ctx: Readonly<CommandContext<Options, Values>>) => Promise<CommandResource<Options>>;
350
- /**
351
- * Translation adapter factory.
352
- */
353
- type TranslationAdapterFactory = (options: TranslationAdapterFactoryOptions) => TranslationAdapter;
354
- /**
355
- * Translation adapter factory options.
356
- */
357
- interface TranslationAdapterFactoryOptions {
358
- /**
359
- * A locale.
360
- */
361
- locale: string;
362
- /**
363
- * A fallback locale.
364
- */
365
- fallbackLocale: string;
366
- }
367
- /**
368
- * Translation adapter.
369
- * This adapter is used to custom message formatter like {@link https://github.com/intlify/vue-i18n/blob/master/spec/syntax.ebnf | Intlify message format}, {@link https://github.com/tc39/proposal-intl-messageformat | `Intl.MessageFormat` (MF2)}, and etc.
370
- * This adapter will support localization with your preferred message format.
371
- */
372
- interface TranslationAdapter<MessageResource = string> {
373
- /**
374
- * Get a resource of locale.
375
- * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
376
- * @returns A resource of locale. if resource not found, return `undefined`.
377
- */
378
- getResource(locale: string): Record<string, string> | undefined;
379
- /**
380
- * Set a resource of locale.
381
- * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
382
- * @param resource A resource of locale
383
- */
384
- setResource(locale: string, resource: Record<string, string>): void;
385
- /**
386
- * Get a message of locale.
387
- * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
388
- * @param key A key of message resource
389
- * @returns A message of locale. if message not found, return `undefined`.
390
- */
391
- getMessage(locale: string, key: string): MessageResource | undefined;
392
- /**
393
- * Translate a message.
394
- * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
395
- * @param key A key of message resource
396
- * @param values A values to be resolved in the message
397
- * @returns A translated message, if message is not translated, return `undefined`.
398
- */
399
- translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;
400
- }
401
- /**
402
- * Command runner.
403
- * @param ctx A {@link CommandContext | command context}
404
- */
405
- type CommandRunner<Options extends ArgOptions = ArgOptions> = (ctx: Readonly<CommandContext<Options>>) => Awaitable<void>;
406
- /**
407
- * Lazy command interface.
408
- * Lazy command that's not loaded until it is executed.
409
- */
410
- type LazyCommand<Options extends ArgOptions = ArgOptions> = () => Awaitable<Command<Options>>;
411
- /**
412
- * Define a command type.
413
- */
414
- type Commandable<Options extends ArgOptions> = Command<Options> | LazyCommand<Options>;
415
-
416
- export { DEFAULT_LOCALE as D };
417
- export type { Command as C, GenerateNamespacedKey as G, LazyCommand as L, RemovedIndex as R, TranslationAdapter as T, CommandOptions as a, CommandRunner as b, TranslationAdapterFactoryOptions as c, CommandBuiltinOptionsKeys as d, CommandBuiltinResourceKeys as e, CommandBuiltinKeys as f, CommandOptionKeys as g, CommandEnvironment as h, CommandContext as i, CommandResource as j, CommandResourceFetcher as k, TranslationAdapterFactory as l, Commandable as m };