gunshi 0.16.0 → 0.17.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,6 +1,6 @@
1
1
  import { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, create, resolveLazyCommand } from "./utils-BYPzZy9X.js";
2
2
  import { createCommandContext } from "./context-BROXRnNP.js";
3
- import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BNorS8VG.js";
3
+ import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-pNEj3FtQ.js";
4
4
  import { parseArgs, resolveArgs } from "args-tokens";
5
5
 
6
6
  //#region src/cli.ts
@@ -18,10 +18,7 @@ async function cli(args, entry, opts = {}) {
18
18
  const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions);
19
19
  if (!command) throw new Error(`Command not found: ${name || ""}`);
20
20
  const options = resolveArgOptions(command.options);
21
- const { values, positionals, rest, error } = resolveArgs(options, tokens, {
22
- optionGrouping: true,
23
- allowNegative: true
24
- });
21
+ const { values, positionals, rest, error } = resolveArgs(options, tokens, { optionGrouping: true });
25
22
  const omitted = !subCommand;
26
23
  const ctx = await createCommandContext({
27
24
  options,
package/lib/context.d.ts CHANGED
@@ -1,54 +1,63 @@
1
- import { Command, CommandContext, CommandOptions } from "./types.d-aDzUZTqM.js";
1
+ import { Command, CommandContext, CommandOptions } from "./types.d-DomXJWKH.js";
2
2
  import { ArgOptions, ArgToken, ArgValues } from "args-tokens";
3
3
 
4
4
  //#region src/context.d.ts
5
5
  /**
6
6
  * Parameters of {@link createCommandContext}
7
7
  */
8
+
8
9
  interface CommandContextParams<Options extends ArgOptions, Values> {
9
- /**
10
- * An options of target command
11
- */
12
- options: Options;
13
- /**
14
- * A values of target command
15
- */
16
- values: Values;
17
- /**
18
- * A positionals arguments, which passed to the target command
19
- */
20
- positionals: string[];
21
- /**
22
- * A rest arguments, which passed to the target command
23
- */
24
- rest: string[];
25
- /**
26
- * Original command line arguments
27
- */
28
- args: string[];
29
- /**
30
- * Argument tokens that are parsed by the `parseArgs` function
31
- */
32
- tokens: ArgToken[];
33
- /**
34
- * Whether the command is omitted
35
- */
36
- omitted: boolean;
37
- /**
38
- * A target {@link Command | command}
39
- */
40
- command: Command<Options>;
41
- /**
42
- * A command options, which is spicialized from `cli` function
43
- */
44
- commandOptions: CommandOptions<Options>;
45
- }
46
- /**
47
- * Create a {@link CommandContext | command context}
48
- * @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
49
- * @returns A {@link CommandContext | command context}, which is readonly
50
- */
51
- declare function createCommandContext<Options extends ArgOptions = ArgOptions, Values extends ArgValues<Options> = ArgValues<Options>>({ options, values, positionals, rest, args, tokens, command, commandOptions, omitted }: CommandContextParams<Options, Values>): Promise<Readonly<CommandContext<Options, Values>>>;
10
+ /**
11
+ * An options of target command
12
+ */
13
+ options: Options;
14
+ /**
15
+ * A values of target command
16
+ */
17
+ values: Values;
18
+ /**
19
+ * A positionals arguments, which passed to the target command
20
+ */
21
+ positionals: string[];
22
+ /**
23
+ * A rest arguments, which passed to the target command
24
+ */
25
+ rest: string[];
26
+ /**
27
+ * Original command line arguments
28
+ */
29
+ args: string[];
30
+ /**
31
+ * Argument tokens that are parsed by the `parseArgs` function
32
+ */
33
+ tokens: ArgToken[];
34
+ /**
35
+ * Whether the command is omitted
36
+ */
37
+ omitted: boolean;
38
+ /**
39
+ * A target {@link Command | command}
40
+ */
41
+ command: Command<Options>;
42
+ /**
43
+ * A command options, which is spicialized from `cli` function
44
+ */
45
+ commandOptions: CommandOptions<Options>;
46
+ } /**
47
+ * Create a {@link CommandContext | command context}
48
+ * @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
49
+ * @returns A {@link CommandContext | command context}, which is readonly
50
+ */
52
51
 
53
- //#endregion
52
+ declare function createCommandContext<Options extends ArgOptions = ArgOptions, Values extends ArgValues<Options> = ArgValues<Options>>({
53
+ options,
54
+ values,
55
+ positionals,
56
+ rest,
57
+ args,
58
+ tokens,
59
+ command,
60
+ commandOptions,
61
+ omitted
62
+ }: CommandContextParams<Options, Values>): Promise<Readonly<CommandContext<Options, Values>>>; //#endregion
54
63
  export { createCommandContext };
@@ -1,4 +1,4 @@
1
- import { Command } from "./types.d-aDzUZTqM.js";
1
+ import { Command } from "./types.d-DomXJWKH.js";
2
2
  import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues as ArgValues$1 } from "args-tokens";
3
3
 
4
4
  //#region src/definition.d.ts
@@ -7,7 +7,6 @@ import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues as A
7
7
  * @param definition A {@link Command | command} definition
8
8
  * @returns A {@link Command | command} definition with type inference
9
9
  */
10
- declare function define<Options extends ArgOptions = ArgOptions>(definition: Command<Options>): Command<Options>;
11
10
 
12
- //#endregion
11
+ declare function define<Options extends ArgOptions = ArgOptions>(definition: Command<Options>): Command<Options>; //#endregion
13
12
  export { ArgOptionSchema, ArgOptions$1 as ArgOptions, ArgValues$1 as ArgValues, define as define$1 };
@@ -1,4 +1,3 @@
1
- import "./types.d-aDzUZTqM.js";
2
- import { ArgOptionSchema, ArgOptions, ArgValues, define$1 as define } from "./definition.d-DllW_uD3.js";
3
-
1
+ import "./types.d-DomXJWKH.js";
2
+ import { ArgOptionSchema, ArgOptions, ArgValues, define$1 as define } from "./definition.d-wYlroF3H.js";
4
3
  export { ArgOptionSchema, ArgOptions, ArgValues, define };
@@ -1,4 +1,4 @@
1
- import { Command, CommandOptions } from "./types.d-aDzUZTqM.js";
1
+ import { Command, CommandOptions } from "./types.d-DomXJWKH.js";
2
2
  import { ArgOptions } from "args-tokens";
3
3
 
4
4
  //#region src/generator.d.ts
@@ -9,6 +9,7 @@ import { ArgOptions } from "args-tokens";
9
9
  * @param opts - A {@link CommandOptions | command options}
10
10
  * @returns A rendered usage.
11
11
  */
12
+
12
13
  declare function generate<Options extends ArgOptions = ArgOptions>(command: string | null, entry: Command<Options>, opts?: CommandOptions<Options>): Promise<string>;
13
14
 
14
15
  //#endregion
package/lib/generator.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { create } from "./utils-BYPzZy9X.js";
2
2
  import "./context-BROXRnNP.js";
3
- import "./renderer-BNorS8VG.js";
4
- import { cli } from "./cli-B7eqtqBF.js";
3
+ import "./renderer-pNEj3FtQ.js";
4
+ import { cli } from "./cli-D2dWSSRj.js";
5
5
 
6
6
  //#region src/generator.ts
7
7
  /**
package/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
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-aDzUZTqM.js";
2
- import { define$1 as define } from "./definition.d-DllW_uD3.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-DomXJWKH.js";
2
+ import { define$1 as define } from "./definition.d-wYlroF3H.js";
3
3
  import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues, parseArgs, resolveArgs } from "args-tokens";
4
4
 
5
5
  //#region src/cli.d.ts
@@ -10,17 +10,18 @@ import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues, par
10
10
  * @param opts A {@link CommandOptions | command options}
11
11
  * @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
12
12
  */
13
+
13
14
  declare function cli<Options extends ArgOptions$1 = ArgOptions$1>(args: string[], entry: Command<Options> | CommandRunner<Options>, opts?: CommandOptions<Options>): Promise<string | undefined>;
14
15
 
15
16
  //#endregion
16
17
  //#region src/translation.d.ts
17
18
  declare class DefaultTranslation implements TranslationAdapter {
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;
19
+ #private;
20
+ constructor(options: TranslationAdapterFactoryOptions);
21
+ getResource(locale: string): Record<string, string> | undefined;
22
+ setResource(locale: string, resource: Record<string, string>): void;
23
+ getMessage(locale: string, key: string): string | undefined;
24
+ translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;
24
25
  }
25
26
 
26
27
  //#endregion
package/lib/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./utils-BYPzZy9X.js";
2
2
  import { DefaultTranslation } from "./context-BROXRnNP.js";
3
3
  import { define } from "./definition-VzcnM0si.js";
4
- import "./renderer-BNorS8VG.js";
5
- import { cli } from "./cli-B7eqtqBF.js";
4
+ import "./renderer-pNEj3FtQ.js";
5
+ import { cli } from "./cli-D2dWSSRj.js";
6
6
  import { parseArgs, resolveArgs } from "args-tokens";
7
7
 
8
8
  export { DEFAULT_LOCALE, DefaultTranslation, cli, define, parseArgs, resolveArgs };
@@ -181,7 +181,7 @@ function getOptionsPairs(ctx) {
181
181
  let key = makeShortLongOptionPair(value, name);
182
182
  if (value.type !== "boolean") key = value.default ? `${key} [${name}]` : `${key} <${name}>`;
183
183
  acc[name] = key;
184
- if (value.type === "boolean" && !(name === "help" || name === "version")) acc[`no-${name}`] = `--no-${name}`;
184
+ if (value.type === "boolean" && value.negatable && !(name === "help" || name === "version")) acc[`no-${name}`] = `--no-${name}`;
185
185
  return acc;
186
186
  }, create());
187
187
  }
package/lib/renderer.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CommandContext } from "./types.d-aDzUZTqM.js";
1
+ import { CommandContext } from "./types.d-DomXJWKH.js";
2
2
  import { ArgOptions } from "args-tokens";
3
3
 
4
4
  //#region src/renderer/header.d.ts
@@ -7,6 +7,7 @@ import { ArgOptions } from "args-tokens";
7
7
  * @param ctx A {@link CommandContext | command context}
8
8
  * @returns A rendered header.
9
9
  */
10
+
10
11
  declare function renderHeader<Options extends ArgOptions = ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
11
12
 
12
13
  //#endregion
package/lib/renderer.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import "./utils-BYPzZy9X.js";
2
- import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BNorS8VG.js";
2
+ import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-pNEj3FtQ.js";
3
3
 
4
4
  export { renderHeader, renderUsage, renderValidationErrors };
@@ -0,0 +1,395 @@
1
+ import { ArgOptions, ArgToken, ArgValues } from "args-tokens";
2
+
3
+ //#region rolldown:runtime
4
+ declare namespace constants_d_exports {
5
+ export { BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, NOOP, OPTION_PREFIX };
6
+ }
7
+ /**
8
+ * The default locale string, which format is BCP 47 language tag.
9
+ */
10
+ declare const DEFAULT_LOCALE = "en-US";
11
+ declare const BUILT_IN_PREFIX = "_";
12
+ declare const OPTION_PREFIX = "Option";
13
+ declare const BUILT_IN_KEY_SEPARATOR = ":";
14
+ declare const NOOP: () => void;
15
+ type CommonOptionType = {
16
+ readonly help: {
17
+ readonly type: 'boolean';
18
+ readonly short: 'h';
19
+ readonly description: string;
20
+ };
21
+ readonly version: {
22
+ readonly type: 'boolean';
23
+ readonly short: 'v';
24
+ readonly description: string;
25
+ };
26
+ };
27
+ declare const COMMON_OPTIONS: CommonOptionType;
28
+ declare const COMMAND_OPTIONS_DEFAULT: CommandOptions<ArgOptions>;
29
+ declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE"];
30
+
31
+ //#endregion
32
+ //#region src/types.d.ts
33
+ type Awaitable<T> = T | Promise<T>;
34
+ type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K] };
35
+
36
+ /**
37
+ * Remove index signature from object or record type.
38
+ */
39
+ type RemovedIndex<T> = RemoveIndexSignature<{ [K in keyof T]: T[K] }>;
40
+ type KeyOfArgOptions<Options extends ArgOptions> = keyof Options | { [K in keyof Options]: Options[K]['type'] extends 'boolean' ? Options[K]['negatable'] extends true ? `no-${Extract<K, string>}` : never : never }[keyof Options];
41
+
42
+ /**
43
+ * Generate a namespaced key.
44
+ */
45
+ type GenerateNamespacedKey<Key extends string, Prefixed extends string = typeof BUILT_IN_PREFIX> = `${Prefixed}${typeof BUILT_IN_KEY_SEPARATOR}${Key}`;
46
+
47
+ /**
48
+ * Command i18n built-in options keys.
49
+ */
50
+ type CommandBuiltinOptionsKeys = keyof (typeof constants_d_exports)['COMMON_OPTIONS'];
51
+
52
+ /**
53
+ * Command i18n built-in resource keys.
54
+ */
55
+ type CommandBuiltinResourceKeys = (typeof constants_d_exports)['COMMAND_BUILTIN_RESOURCE_KEYS'][number];
56
+
57
+ /**
58
+ * Command i18n built-in keys.
59
+ * The command i18n built-in keys are used to {@link CommandContext.translate | translate} function.
60
+ */
61
+ type CommandBuiltinKeys = GenerateNamespacedKey<CommandBuiltinOptionsKeys> | GenerateNamespacedKey<CommandBuiltinResourceKeys> | 'description' | 'examples';
62
+
63
+ /**
64
+ * Command i18n option keys.
65
+ * The command i18n option keys are used to {@link CommandContext.translate | translate} function.
66
+ */
67
+ type CommandOptionKeys<Options extends ArgOptions> = GenerateNamespacedKey<KeyOfArgOptions<RemovedIndex<Options>>, typeof OPTION_PREFIX>;
68
+
69
+ /**
70
+ * Command environment.
71
+ */
72
+ interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
73
+ /**
74
+ * Current working directory.
75
+ * @see {@link CommandOptions.cwd}
76
+ */
77
+ cwd: string | undefined;
78
+ /**
79
+ * Command name.
80
+ * @see {@link CommandOptions.name}
81
+ */
82
+ name: string | undefined;
83
+ /**
84
+ * Command description.
85
+ * @see {@link CommandOptions.description}
86
+ *
87
+ */
88
+ description: string | undefined;
89
+ /**
90
+ * Command version.
91
+ * @see {@link CommandOptions.version}
92
+ */
93
+ version: string | undefined;
94
+ /**
95
+ * Left margin of the command output.
96
+ * @default 2
97
+ * @see {@link CommandOptions.leftMargin}
98
+ */
99
+ leftMargin: number;
100
+ /**
101
+ * Middle margin of the command output.
102
+ * @default 10
103
+ * @see {@link CommandOptions.middleMargin}
104
+ */
105
+ middleMargin: number;
106
+ /**
107
+ * Whether to display the usage option type.
108
+ * @default false
109
+ * @see {@link CommandOptions.usageOptionType}
110
+ */
111
+ usageOptionType: boolean;
112
+ /**
113
+ * Whether to display the command usage.
114
+ * @default false
115
+ * @see {@link}
116
+ */
117
+ usageSilent: boolean;
118
+ /**
119
+ * Sub commands.
120
+ * @see {@link CommandOptions.subCommands}
121
+ */
122
+ subCommands: Map<string, Command<any> | LazyCommand<any>> | undefined;
123
+ /**
124
+ * Render function the command usage.
125
+ */
126
+ renderUsage: ((ctx: CommandContext<Options>) => Promise<string>) | null | undefined;
127
+ /**
128
+ * Render function the header section in the command usage.
129
+ */
130
+ renderHeader: ((ctx: CommandContext<Options>) => Promise<string>) | null | undefined;
131
+ /**
132
+ * Render function the validation errors.
133
+ */
134
+ renderValidationErrors: ((ctx: CommandContext<Options>, error: AggregateError) => Promise<string>) | null | undefined;
135
+ }
136
+
137
+ /**
138
+ * Command options.
139
+ */
140
+ interface CommandOptions<Options extends ArgOptions = ArgOptions> {
141
+ /**
142
+ * Current working directory.
143
+ */
144
+ cwd?: string;
145
+ /**
146
+ * Command program name.
147
+ */
148
+ name?: string;
149
+ /**
150
+ * Command program description.
151
+ *
152
+ */
153
+ description?: string;
154
+ /**
155
+ * Command program version.
156
+ */
157
+ version?: string;
158
+ /**
159
+ * Command program locale.
160
+ */
161
+ locale?: string | Intl.Locale;
162
+ /**
163
+ * Sub commands.
164
+ */
165
+ subCommands?: Map<string, Command<any> | LazyCommand<any>>;
166
+ /**
167
+ * Left margin of the command output.
168
+ */
169
+ leftMargin?: number;
170
+ /**
171
+ * Middle margin of the command output.
172
+ */
173
+ middleMargin?: number;
174
+ /**
175
+ * Whether to display the usage option type.
176
+ */
177
+ usageOptionType?: boolean;
178
+ /**
179
+ * Whether to display the command usage.
180
+ */
181
+ usageSilent?: boolean;
182
+ /**
183
+ * Render function the command usage.
184
+ */
185
+ renderUsage?: ((ctx: Readonly<CommandContext<Options>>) => Promise<string>) | null;
186
+ /**
187
+ * Render function the header section in the command usage.
188
+ */
189
+ renderHeader?: ((ctx: Readonly<CommandContext<Options>>) => Promise<string>) | null;
190
+ /**
191
+ * Render function the validation errors.
192
+ */
193
+ renderValidationErrors?: ((ctx: Readonly<CommandContext<Options>>, error: AggregateError) => Promise<string>) | null;
194
+ /**
195
+ * Translation adapter factory.
196
+ */
197
+ translationAdapterFactory?: TranslationAdapterFactory;
198
+ }
199
+ /**
200
+ * Command context.
201
+ * Command context is the context of the command execution.
202
+ */
203
+ interface CommandContext<Options extends ArgOptions = ArgOptions, Values = ArgValues<Options>> {
204
+ /**
205
+ * Command name, that is the command that is executed.
206
+ * The command name is same {@link CommandEnvironment.name}.
207
+ */
208
+ name: string | undefined;
209
+ /**
210
+ * Command description, that is the description of the command that is executed.
211
+ * The command description is same {@link CommandEnvironment.description}.
212
+ */
213
+ description: string | undefined;
214
+ /**
215
+ * Command locale, that is the locale of the command that is executed.
216
+ */
217
+ locale: Intl.Locale;
218
+ /**
219
+ * Command environment, that is the environment of the command that is executed.
220
+ * The command environment is same {@link CommandEnvironment}.
221
+ */
222
+ env: Readonly<CommandEnvironment<Options>>;
223
+ /**
224
+ * Command options, that is the options of the command that is executed.
225
+ * The command options is same {@link Command.options}.
226
+ */
227
+ options: Options;
228
+ /**
229
+ * Command values, that is the values of the command that is executed.
230
+ * Resolve values with `resolveArgs` from command arguments and {@link Command.options}.
231
+ */
232
+ values: Values;
233
+ /**
234
+ * Command positionals arguments, that is the positionals of the command that is executed.
235
+ * Resolve positionals with `resolveArgs` from command arguments.
236
+ */
237
+ positionals: string[];
238
+ /**
239
+ * Command rest arguments, that is the remaining argument not resolved by the optional command option delimiter `--`.
240
+ */
241
+ rest: string[];
242
+ /**
243
+ * Original command line arguments.
244
+ * This argument is passed from `cli` function.
245
+ */
246
+ _: string[];
247
+ /**
248
+ * Argument tokens, that is parsed by `parseArgs` function.
249
+ */
250
+ tokens: ArgToken[];
251
+ /**
252
+ * Whether the currently executing command has been executed with the sub-command name omitted.
253
+ */
254
+ omitted: boolean;
255
+ /**
256
+ * Output a message.
257
+ * If {@link CommandEnvironment.usageSilent} is true, the message is not output.
258
+ * @param message an output message, @see {@link console.log}
259
+ * @param optionalParams an optional parameters, @see {@link console.log}
260
+ */
261
+ log: (message?: any, ...optionalParams: any[]) => void;
262
+ /**
263
+ * Load sub-commands.
264
+ * The loaded commands are cached and returned when called again.
265
+ * @returns loaded commands.
266
+ */
267
+ loadCommands: () => Promise<Command<Options>[]>;
268
+ /**
269
+ * Translate function.
270
+ * @param key the key to be translated
271
+ * @param values the values to be formatted
272
+ * @returns A translated string.
273
+ */
274
+ translate: <T extends string = CommandBuiltinKeys, O = CommandOptionKeys<Options>, Key = CommandBuiltinKeys | O | T>(key: Key, values?: Record<string, unknown>) => string;
275
+ } /**
276
+ * Command interface.
277
+ */
278
+
279
+ interface Command<Options extends ArgOptions = ArgOptions> {
280
+ /**
281
+ * Command name.
282
+ * It's used to find command line arguments to execute from sub commands, and it's recommended to specify.
283
+ */
284
+ name?: string;
285
+ /**
286
+ * Command description.
287
+ * It's used to describe the command in usage and it's recommended to specify.
288
+ */
289
+ description?: string;
290
+ /**
291
+ * Command options.
292
+ * Each option can include a description property to describe the option in usage.
293
+ */
294
+ options?: Options;
295
+ /**
296
+ * Command examples.
297
+ * examples of how to use the command.
298
+ */
299
+ examples?: string;
300
+ /**
301
+ * Command runner. it's the command to be executed
302
+ */
303
+ run: CommandRunner<Options>;
304
+ /**
305
+ * Command resource fetcher.
306
+ */
307
+ resource?: CommandResourceFetcher<Options>;
308
+ } /**
309
+ * Command resource.
310
+ */
311
+
312
+ type CommandResource<Options extends ArgOptions = ArgOptions> = {
313
+ /**
314
+ * Command description.
315
+ */
316
+ description: string;
317
+ /**
318
+ * Examples usage.
319
+ */
320
+ examples: string;
321
+ } & { [Option in GenerateNamespacedKey<KeyOfArgOptions<RemovedIndex<Options>>, typeof OPTION_PREFIX>]: string } & {
322
+ [key: string]: string;
323
+ }; /**
324
+ * Command resource fetcher.
325
+ * @param ctx A {@link CommandContext | command context}
326
+ * @returns A fetched {@link CommandResource | command resource}.
327
+ */
328
+
329
+ type CommandResourceFetcher<Options extends ArgOptions = ArgOptions, Values = ArgValues<Options>> = (ctx: Readonly<CommandContext<Options, Values>>) => Promise<CommandResource<Options>>; /**
330
+ * Translation adapter factory.
331
+ */
332
+
333
+ type TranslationAdapterFactory = (options: TranslationAdapterFactoryOptions) => TranslationAdapter; /**
334
+ * Translation adapter factory options.
335
+ */
336
+
337
+ interface TranslationAdapterFactoryOptions {
338
+ /**
339
+ * A locale.
340
+ */
341
+ locale: string;
342
+ /**
343
+ * A fallback locale.
344
+ */
345
+ fallbackLocale: string;
346
+ } /**
347
+ * Translation adapter.
348
+ * 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.
349
+ * This adapter will support localization with your preferred message format.
350
+ */
351
+
352
+ interface TranslationAdapter<MessageResource = string> {
353
+ /**
354
+ * Get a resource of locale.
355
+ * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
356
+ * @returns A resource of locale. if resource not found, return `undefined`.
357
+ */
358
+ getResource(locale: string): Record<string, string> | undefined;
359
+ /**
360
+ * Set a resource of locale.
361
+ * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
362
+ * @param resource A resource of locale
363
+ */
364
+ setResource(locale: string, resource: Record<string, string>): void;
365
+ /**
366
+ * Get a message of locale.
367
+ * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
368
+ * @param key A key of message resource
369
+ * @returns A message of locale. if message not found, return `undefined`.
370
+ */
371
+ getMessage(locale: string, key: string): MessageResource | undefined;
372
+ /**
373
+ * Translate a message.
374
+ * @param locale A Locale at the time of command execution. That is Unicord locale ID (BCP 47)
375
+ * @param key A key of message resource
376
+ * @param values A values to be resolved in the message
377
+ * @returns A translated message, if message is not translated, return `undefined`.
378
+ */
379
+ translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;
380
+ } /**
381
+ * Command runner.
382
+ * @param ctx A {@link CommandContext | command context}
383
+ */
384
+
385
+ type CommandRunner<Options extends ArgOptions = ArgOptions> = (ctx: Readonly<CommandContext<Options>>) => Awaitable<void>; /**
386
+ * Lazy command interface.
387
+ * Lazy command that's not loaded until it is executed.
388
+ */
389
+
390
+ type LazyCommand<Options extends ArgOptions = ArgOptions> = () => Awaitable<Command<Options>>; /**
391
+ * Define a command type.
392
+ */
393
+
394
+ type Commandable<Options extends ArgOptions> = Command<Options> | LazyCommand<Options>; //#endregion
395
+ 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.16.0",
4
+ "version": "0.17.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -77,20 +77,20 @@
77
77
  }
78
78
  },
79
79
  "dependencies": {
80
- "args-tokens": "^0.15.1"
80
+ "args-tokens": "^0.16.0"
81
81
  },
82
82
  "devDependencies": {
83
- "@eslint/markdown": "^6.3.0",
83
+ "@eslint/markdown": "^6.4.0",
84
84
  "@intlify/core": "next",
85
85
  "@kazupon/eslint-config": "^0.29.0",
86
86
  "@kazupon/prettier-config": "^0.1.1",
87
- "@types/node": "^22.14.0",
88
- "@vitest/eslint-plugin": "^1.1.42",
87
+ "@types/node": "^22.15.3",
88
+ "@vitest/eslint-plugin": "^1.1.43",
89
89
  "bumpp": "^10.1.0",
90
- "deno": "^2.2.8",
91
- "eslint": "^9.24.0",
90
+ "deno": "^2.2.12",
91
+ "eslint": "^9.25.1",
92
92
  "eslint-config-prettier": "^10.1.2",
93
- "eslint-import-resolver-typescript": "^4.3.2",
93
+ "eslint-import-resolver-typescript": "^4.3.4",
94
94
  "eslint-plugin-import": "^2.31.0",
95
95
  "eslint-plugin-jsonc": "^2.20.0",
96
96
  "eslint-plugin-module-interop": "^0.3.1",
@@ -98,28 +98,28 @@
98
98
  "eslint-plugin-regexp": "^2.7.0",
99
99
  "eslint-plugin-unicorn": "^58.0.0",
100
100
  "eslint-plugin-unused-imports": "^4.1.4",
101
- "eslint-plugin-vue": "^10.0.0",
101
+ "eslint-plugin-vue": "^10.0.1",
102
102
  "eslint-plugin-vue-composable": "^1.0.0",
103
- "eslint-plugin-yml": "^1.17.0",
103
+ "eslint-plugin-yml": "^1.18.0",
104
104
  "gh-changelogen": "^0.2.8",
105
105
  "jsr": "^0.13.4",
106
106
  "jsr-exports-lint": "^0.2.0",
107
- "knip": "^5.50.2",
108
- "lint-staged": "^15.5.0",
107
+ "knip": "^5.50.5",
108
+ "lint-staged": "^15.5.1",
109
109
  "messageformat": "4.0.0-10",
110
110
  "pkg-pr-new": "^0.0.43",
111
111
  "prettier": "^3.5.3",
112
- "publint": "^0.3.11",
113
- "tsdown": "^0.9.6",
114
- "typedoc": "^0.28.2",
115
- "typedoc-plugin-markdown": "^4.6.2",
112
+ "publint": "^0.3.12",
113
+ "tsdown": "^0.10.0",
114
+ "typedoc": "^0.28.3",
115
+ "typedoc-plugin-markdown": "^4.6.3",
116
116
  "typedoc-vitepress-theme": "^1.1.2",
117
117
  "typescript": "^5.8.3",
118
- "typescript-eslint": "^8.29.1",
118
+ "typescript-eslint": "^8.31.1",
119
119
  "vitepress": "^1.6.3",
120
- "vitepress-plugin-group-icons": "^1.4.1",
121
- "vitepress-plugin-llms": "^1.0.0",
122
- "vitest": "^3.1.1",
120
+ "vitepress-plugin-group-icons": "^1.5.2",
121
+ "vitepress-plugin-llms": "^1.1.1",
122
+ "vitest": "^3.1.2",
123
123
  "vue": "^3.5.13"
124
124
  },
125
125
  "prettier": "@kazupon/prettier-config",
@@ -1,403 +0,0 @@
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 };