@gunshi/bone 0.26.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +67 -0
- package/lib/index.d.ts +699 -0
- package/lib/index.js +847 -0
- package/package.json +69 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,699 @@
|
|
|
1
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.20.1/node_modules/args-tokens/lib/parser-FiQIAw-2.d.ts
|
|
2
|
+
//#region src/parser.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Entry point of argument parser.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* forked from `nodejs/node` (`pkgjs/parseargs`)
|
|
9
|
+
* repository url: https://github.com/nodejs/node (https://github.com/pkgjs/parseargs)
|
|
10
|
+
* code url: https://github.com/nodejs/node/blob/main/lib/internal/util/parse_args/parse_args.js
|
|
11
|
+
*
|
|
12
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
13
|
+
* @license MIT
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Argument token Kind.
|
|
17
|
+
* - `option`: option token, support short option (e.g. `-x`) and long option (e.g. `--foo`)
|
|
18
|
+
* - `option-terminator`: option terminator (`--`) token, see guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
|
|
19
|
+
* - `positional`: positional token
|
|
20
|
+
*/
|
|
21
|
+
type ArgTokenKind = "option" | "option-terminator" | "positional";
|
|
22
|
+
/**
|
|
23
|
+
* Argument token.
|
|
24
|
+
*/
|
|
25
|
+
interface ArgToken {
|
|
26
|
+
/**
|
|
27
|
+
* Argument token kind.
|
|
28
|
+
*/
|
|
29
|
+
kind: ArgTokenKind;
|
|
30
|
+
/**
|
|
31
|
+
* Argument token index, e.g `--foo bar` => `--foo` index is 0, `bar` index is 1.
|
|
32
|
+
*/
|
|
33
|
+
index: number;
|
|
34
|
+
/**
|
|
35
|
+
* Option name, e.g. `--foo` => `foo`, `-x` => `x`.
|
|
36
|
+
*/
|
|
37
|
+
name?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Raw option name, e.g. `--foo` => `--foo`, `-x` => `-x`.
|
|
40
|
+
*/
|
|
41
|
+
rawName?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Option value, e.g. `--foo=bar` => `bar`, `-x=bar` => `bar`.
|
|
44
|
+
* If the `allowCompatible` option is `true`, short option value will be same as Node.js `parseArgs` behavior.
|
|
45
|
+
*/
|
|
46
|
+
value?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true`.
|
|
49
|
+
*/
|
|
50
|
+
inlineValue?: boolean;
|
|
51
|
+
} //#endregion
|
|
52
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.20.1/node_modules/args-tokens/lib/resolver-U72Jg6Ll.d.ts
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Parser Options.
|
|
56
|
+
*/
|
|
57
|
+
//#region src/resolver.d.ts
|
|
58
|
+
/**
|
|
59
|
+
* An argument schema
|
|
60
|
+
* This schema is similar to the schema of the `node:utils`.
|
|
61
|
+
* difference is that:
|
|
62
|
+
* - `required` property and `description` property are added
|
|
63
|
+
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum', 'positional', 'custom' too.
|
|
64
|
+
* - `default` property type, not support multiple types
|
|
65
|
+
*/
|
|
66
|
+
interface ArgSchema {
|
|
67
|
+
/**
|
|
68
|
+
* Type of argument.
|
|
69
|
+
*/
|
|
70
|
+
type: "string" | "boolean" | "number" | "enum" | "positional" | "custom";
|
|
71
|
+
/**
|
|
72
|
+
* A single character alias for the argument.
|
|
73
|
+
*/
|
|
74
|
+
short?: string;
|
|
75
|
+
/**
|
|
76
|
+
* A description of the argument.
|
|
77
|
+
*/
|
|
78
|
+
description?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Whether the argument is required or not.
|
|
81
|
+
*/
|
|
82
|
+
required?: true;
|
|
83
|
+
/**
|
|
84
|
+
* Whether the argument allow multiple values or not.
|
|
85
|
+
*/
|
|
86
|
+
multiple?: true;
|
|
87
|
+
/**
|
|
88
|
+
* Whether the negatable option for `boolean` type
|
|
89
|
+
*/
|
|
90
|
+
negatable?: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
|
|
93
|
+
*/
|
|
94
|
+
choices?: string[] | readonly string[];
|
|
95
|
+
/**
|
|
96
|
+
* The default value of the argument.
|
|
97
|
+
* if the type is 'enum', the default value must be one of the allowed values.
|
|
98
|
+
*/
|
|
99
|
+
default?: string | boolean | number;
|
|
100
|
+
/**
|
|
101
|
+
* Whether to convert the argument name to kebab-case.
|
|
102
|
+
*/
|
|
103
|
+
toKebab?: true;
|
|
104
|
+
/**
|
|
105
|
+
* A function to parse the value of the argument. if the type is 'custom', this function is required.
|
|
106
|
+
* If argument value will be invalid, this function have to throw an error.
|
|
107
|
+
* @param value
|
|
108
|
+
* @returns Parsed value
|
|
109
|
+
* @throws An Error, If the value is invalid. Error type should be `Error` or extends it
|
|
110
|
+
*/
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
112
|
+
parse?: (value: string) => any;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* An object that contains {@link ArgSchema | argument schema}.
|
|
116
|
+
*/
|
|
117
|
+
interface Args {
|
|
118
|
+
[option: string]: ArgSchema;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* An object that contains the values of the arguments.
|
|
122
|
+
*/
|
|
123
|
+
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
124
|
+
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
125
|
+
};
|
|
126
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
127
|
+
type IsFunction<T> = T extends ((...args: any[]) => any) ? true : false;
|
|
128
|
+
/**
|
|
129
|
+
* @internal
|
|
130
|
+
*/
|
|
131
|
+
type ExtractOptionValue<A extends ArgSchema> = A["type"] extends "string" ? ResolveOptionValue<A, string> : A["type"] extends "boolean" ? ResolveOptionValue<A, boolean> : A["type"] extends "number" ? ResolveOptionValue<A, number> : A["type"] extends "positional" ? ResolveOptionValue<A, string> : A["type"] extends "enum" ? A["choices"] extends string[] | readonly string[] ? ResolveOptionValue<A, A["choices"][number]> : never : A["type"] extends "custom" ? IsFunction<A["parse"]> extends true ? ResolveOptionValue<A, ReturnType<NonNullable<A["parse"]>>> : never : ResolveOptionValue<A, string | boolean | number>;
|
|
132
|
+
type ResolveOptionValue<A extends ArgSchema, T> = A["multiple"] extends true ? T[] : T;
|
|
133
|
+
/**
|
|
134
|
+
* @internal
|
|
135
|
+
*/
|
|
136
|
+
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: V[Arg] } & FilterArgs<A, V, "default"> & FilterArgs<A, V, "required"> & FilterPositionalArgs<A, V> extends infer P ? { [K in keyof P]: P[K] } : never;
|
|
137
|
+
/**
|
|
138
|
+
* @internal
|
|
139
|
+
*/
|
|
140
|
+
type FilterArgs<A extends Args, V extends Record<keyof A, unknown>, K extends keyof ArgSchema> = { [Arg in keyof A as A[Arg][K] extends {} ? Arg : never]: V[Arg] };
|
|
141
|
+
/**
|
|
142
|
+
* @internal
|
|
143
|
+
*/
|
|
144
|
+
type FilterPositionalArgs<A extends Args, V extends Record<keyof A, unknown>> = { [Arg in keyof A as A[Arg]["type"] extends "positional" ? Arg : never]: V[Arg] };
|
|
145
|
+
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region ../gunshi/src/plugin/context.d.ts
|
|
148
|
+
/**
|
|
149
|
+
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
150
|
+
*/
|
|
151
|
+
/**
|
|
152
|
+
* Type helper to create GunshiParams from extracted args and extensions
|
|
153
|
+
* @internal
|
|
154
|
+
*/
|
|
155
|
+
type ExtractedParams<G extends GunshiParamsConstraint, L extends Record<string, unknown> = {}> = {
|
|
156
|
+
args: ExtractArgs<G>;
|
|
157
|
+
extensions: ExtractExtensions<G> & L;
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* Gunshi plugin context interface.
|
|
161
|
+
*/
|
|
162
|
+
interface PluginContext<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
163
|
+
/**
|
|
164
|
+
* Get the global options
|
|
165
|
+
* @returns A map of global options.
|
|
166
|
+
*/
|
|
167
|
+
readonly globalOptions: Map<string, ArgSchema>;
|
|
168
|
+
/**
|
|
169
|
+
* Add a global option.
|
|
170
|
+
* @param name An option name
|
|
171
|
+
* @param schema An {@link ArgSchema} for the option
|
|
172
|
+
*/
|
|
173
|
+
addGlobalOption(name: string, schema: ArgSchema): void;
|
|
174
|
+
/**
|
|
175
|
+
* Decorate the header renderer.
|
|
176
|
+
* @param decorator - A decorator function that wraps the base header renderer.
|
|
177
|
+
*/
|
|
178
|
+
decorateHeaderRenderer<L extends Record<string, unknown> = DefaultGunshiParams['extensions']>(decorator: (baseRenderer: (ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Promise<string>, ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Promise<string>): void;
|
|
179
|
+
/**
|
|
180
|
+
* Decorate the usage renderer.
|
|
181
|
+
* @param decorator - A decorator function that wraps the base usage renderer.
|
|
182
|
+
*/
|
|
183
|
+
decorateUsageRenderer<L extends Record<string, unknown> = DefaultGunshiParams['extensions']>(decorator: (baseRenderer: (ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Promise<string>, ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Promise<string>): void;
|
|
184
|
+
/**
|
|
185
|
+
* Decorate the validation errors renderer.
|
|
186
|
+
* @param decorator - A decorator function that wraps the base validation errors renderer.
|
|
187
|
+
*/
|
|
188
|
+
decorateValidationErrorsRenderer<L extends Record<string, unknown> = DefaultGunshiParams['extensions']>(decorator: (baseRenderer: (ctx: Readonly<CommandContext<ExtractedParams<G, L>>>, error: AggregateError) => Promise<string>, ctx: Readonly<CommandContext<ExtractedParams<G, L>>>, error: AggregateError) => Promise<string>): void;
|
|
189
|
+
/**
|
|
190
|
+
* Decorate the command execution.
|
|
191
|
+
* Decorators are applied in reverse order (last registered is executed first).
|
|
192
|
+
* @param decorator - A decorator function that wraps the command runner
|
|
193
|
+
*/
|
|
194
|
+
decorateCommand<L extends Record<string, unknown> = DefaultGunshiParams['extensions']>(decorator: (baseRunner: (ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Awaitable<void | string>) => (ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Awaitable<void | string>): void;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
//#endregion
|
|
198
|
+
//#region ../gunshi/src/plugin/core.d.ts
|
|
199
|
+
/**
|
|
200
|
+
* Factory function for creating a plugin context.
|
|
201
|
+
* @param decorators - A {@link Decorators} instance.
|
|
202
|
+
* @returns A new {@link PluginContext} instance.
|
|
203
|
+
*/
|
|
204
|
+
/**
|
|
205
|
+
* Plugin dependency definition
|
|
206
|
+
*/
|
|
207
|
+
interface PluginDependency {
|
|
208
|
+
/**
|
|
209
|
+
* Dependency plugin id
|
|
210
|
+
*/
|
|
211
|
+
id: string;
|
|
212
|
+
/**
|
|
213
|
+
* Optional dependency flag.
|
|
214
|
+
* If true, the plugin will not throw an error if the dependency is not found.
|
|
215
|
+
*/
|
|
216
|
+
optional?: boolean;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Plugin function type
|
|
220
|
+
*/
|
|
221
|
+
type PluginFunction<G extends GunshiParams = DefaultGunshiParams> = (ctx: Readonly<PluginContext<G>>) => Awaitable<void>;
|
|
222
|
+
/**
|
|
223
|
+
* Plugin extension for CommandContext
|
|
224
|
+
*/
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Gunshi plugin, which is a function that receives a PluginContext.
|
|
228
|
+
* @param ctx - A {@link PluginContext}.
|
|
229
|
+
* @returns An {@link Awaitable} that resolves when the plugin is loaded.
|
|
230
|
+
*/
|
|
231
|
+
type Plugin<E extends GunshiParams['extensions'] = DefaultGunshiParams['extensions']> = PluginFunction & {
|
|
232
|
+
id: string;
|
|
233
|
+
name?: string;
|
|
234
|
+
dependencies?: (PluginDependency | string)[];
|
|
235
|
+
extension?: CommandContextExtension<E>;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
//#endregion
|
|
239
|
+
//#region ../gunshi/src/types.d.ts
|
|
240
|
+
/**
|
|
241
|
+
* Plugin return type with extension
|
|
242
|
+
* @internal
|
|
243
|
+
*/
|
|
244
|
+
type Awaitable<T> = T | Promise<T>;
|
|
245
|
+
/**
|
|
246
|
+
* Extend command context type. This type is used to extend the command context with additional properties at {@link CommandContext.extensions}.
|
|
247
|
+
*/
|
|
248
|
+
type ExtendContext = Record<string, unknown>;
|
|
249
|
+
/**
|
|
250
|
+
* Gunshi unified parameter type.
|
|
251
|
+
* This type combines both argument definitions and command context extensions.
|
|
252
|
+
*/
|
|
253
|
+
interface GunshiParams<P extends {
|
|
254
|
+
args?: Args;
|
|
255
|
+
extensions?: ExtendContext;
|
|
256
|
+
} = {
|
|
257
|
+
args: Args;
|
|
258
|
+
extensions: {};
|
|
259
|
+
}> {
|
|
260
|
+
/**
|
|
261
|
+
* Command argument definitions
|
|
262
|
+
*/
|
|
263
|
+
args: P extends {
|
|
264
|
+
args: infer A extends Args;
|
|
265
|
+
} ? A : Args;
|
|
266
|
+
/**
|
|
267
|
+
* Command context extensions
|
|
268
|
+
*/
|
|
269
|
+
extensions: P extends {
|
|
270
|
+
extensions: infer E extends ExtendContext;
|
|
271
|
+
} ? E : {};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Default Gunshi parameters
|
|
275
|
+
*/
|
|
276
|
+
type DefaultGunshiParams = GunshiParams;
|
|
277
|
+
/**
|
|
278
|
+
* Generic constraint for command-related types.
|
|
279
|
+
* This type constraint allows both GunshiParams and objects with extensions.
|
|
280
|
+
*/
|
|
281
|
+
type GunshiParamsConstraint = GunshiParams<any> | {
|
|
282
|
+
extensions: ExtendContext;
|
|
283
|
+
};
|
|
284
|
+
/**
|
|
285
|
+
* Type helper to extract args from G
|
|
286
|
+
* @internal
|
|
287
|
+
*/
|
|
288
|
+
type ExtractArgs<G> = G extends GunshiParams<any> ? G['args'] : Args;
|
|
289
|
+
/**
|
|
290
|
+
* Type helper to extract extensions from G
|
|
291
|
+
* @internal
|
|
292
|
+
*/
|
|
293
|
+
type ExtractExtensions<G> = G extends GunshiParams<any> ? G['extensions'] : G extends {
|
|
294
|
+
extensions: infer E;
|
|
295
|
+
} ? E : {};
|
|
296
|
+
/**
|
|
297
|
+
* Type helper to normalize G to GunshiParams
|
|
298
|
+
* @internal
|
|
299
|
+
*/
|
|
300
|
+
type NormalizeToGunshiParams<G> = G extends GunshiParams<any> ? G : G extends {
|
|
301
|
+
extensions: ExtendContext;
|
|
302
|
+
} ? GunshiParams<{
|
|
303
|
+
args: Args;
|
|
304
|
+
extensions: G['extensions'];
|
|
305
|
+
}> : DefaultGunshiParams;
|
|
306
|
+
/**
|
|
307
|
+
* Command environment.
|
|
308
|
+
*/
|
|
309
|
+
interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
310
|
+
/**
|
|
311
|
+
* Current working directory.
|
|
312
|
+
* @see {@link CliOptions.cwd}
|
|
313
|
+
*/
|
|
314
|
+
cwd: string | undefined;
|
|
315
|
+
/**
|
|
316
|
+
* Command name.
|
|
317
|
+
* @see {@link CliOptions.name}
|
|
318
|
+
*/
|
|
319
|
+
name: string | undefined;
|
|
320
|
+
/**
|
|
321
|
+
* Command description.
|
|
322
|
+
* @see {@link CliOptions.description}
|
|
323
|
+
*
|
|
324
|
+
*/
|
|
325
|
+
description: string | undefined;
|
|
326
|
+
/**
|
|
327
|
+
* Command version.
|
|
328
|
+
* @see {@link CliOptions.version}
|
|
329
|
+
*/
|
|
330
|
+
version: string | undefined;
|
|
331
|
+
/**
|
|
332
|
+
* Left margin of the command output.
|
|
333
|
+
* @default 2
|
|
334
|
+
* @see {@link CliOptions.leftMargin}
|
|
335
|
+
*/
|
|
336
|
+
leftMargin: number;
|
|
337
|
+
/**
|
|
338
|
+
* Middle margin of the command output.
|
|
339
|
+
* @default 10
|
|
340
|
+
* @see {@link CliOptions.middleMargin}
|
|
341
|
+
*/
|
|
342
|
+
middleMargin: number;
|
|
343
|
+
/**
|
|
344
|
+
* Whether to display the usage option type.
|
|
345
|
+
* @default false
|
|
346
|
+
* @see {@link CliOptions.usageOptionType}
|
|
347
|
+
*/
|
|
348
|
+
usageOptionType: boolean;
|
|
349
|
+
/**
|
|
350
|
+
* Whether to display the option value.
|
|
351
|
+
* @default true
|
|
352
|
+
* @see {@link CliOptions.usageOptionValue}
|
|
353
|
+
*/
|
|
354
|
+
usageOptionValue: boolean;
|
|
355
|
+
/**
|
|
356
|
+
* Whether to display the command usage.
|
|
357
|
+
* @default false
|
|
358
|
+
* @see {@link CliOptions.usageSilent}
|
|
359
|
+
*/
|
|
360
|
+
usageSilent: boolean;
|
|
361
|
+
/**
|
|
362
|
+
* Sub commands.
|
|
363
|
+
* @see {@link CliOptions.subCommands}
|
|
364
|
+
*/
|
|
365
|
+
subCommands: Map<string, Command<any> | LazyCommand<any>> | undefined;
|
|
366
|
+
/**
|
|
367
|
+
* Render function the command usage.
|
|
368
|
+
*/
|
|
369
|
+
renderUsage: ((ctx: Readonly<CommandContext<G>>) => Promise<string>) | null | undefined;
|
|
370
|
+
/**
|
|
371
|
+
* Render function the header section in the command usage.
|
|
372
|
+
*/
|
|
373
|
+
renderHeader: ((ctx: Readonly<CommandContext<G>>) => Promise<string>) | null | undefined;
|
|
374
|
+
/**
|
|
375
|
+
* Render function the validation errors.
|
|
376
|
+
*/
|
|
377
|
+
renderValidationErrors: ((ctx: Readonly<CommandContext<G>>, error: AggregateError) => Promise<string>) | null | undefined;
|
|
378
|
+
/**
|
|
379
|
+
* Hook that runs before any command execution
|
|
380
|
+
* @see {@link CliOptions.onBeforeCommand}
|
|
381
|
+
*/
|
|
382
|
+
onBeforeCommand: ((ctx: Readonly<CommandContext<G>>) => Awaitable<void>) | undefined;
|
|
383
|
+
/**
|
|
384
|
+
* Hook that runs after successful command execution
|
|
385
|
+
* @see {@link CliOptions.onAfterCommand}
|
|
386
|
+
*/
|
|
387
|
+
onAfterCommand: ((ctx: Readonly<CommandContext<G>>, result: string | void) => Awaitable<void>) | undefined;
|
|
388
|
+
/**
|
|
389
|
+
* Hook that runs when a command throws an error
|
|
390
|
+
* @see {@link CliOptions.onErrorCommand}
|
|
391
|
+
*/
|
|
392
|
+
onErrorCommand: ((ctx: Readonly<CommandContext<G>>, error: Error) => Awaitable<void>) | undefined;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* CLI options of `cli` function.
|
|
396
|
+
*/
|
|
397
|
+
interface CliOptions<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
398
|
+
/**
|
|
399
|
+
* Current working directory.
|
|
400
|
+
*/
|
|
401
|
+
cwd?: string;
|
|
402
|
+
/**
|
|
403
|
+
* Command program name.
|
|
404
|
+
*/
|
|
405
|
+
name?: string;
|
|
406
|
+
/**
|
|
407
|
+
* Command program description.
|
|
408
|
+
*
|
|
409
|
+
*/
|
|
410
|
+
description?: string;
|
|
411
|
+
/**
|
|
412
|
+
* Command program version.
|
|
413
|
+
*/
|
|
414
|
+
version?: string;
|
|
415
|
+
/**
|
|
416
|
+
* Sub commands.
|
|
417
|
+
*/
|
|
418
|
+
subCommands?: Map<string, Command<any> | LazyCommand<any>>;
|
|
419
|
+
/**
|
|
420
|
+
* Left margin of the command output.
|
|
421
|
+
*/
|
|
422
|
+
leftMargin?: number;
|
|
423
|
+
/**
|
|
424
|
+
* Middle margin of the command output.
|
|
425
|
+
*/
|
|
426
|
+
middleMargin?: number;
|
|
427
|
+
/**
|
|
428
|
+
* Whether to display the usage optional argument type.
|
|
429
|
+
*/
|
|
430
|
+
usageOptionType?: boolean;
|
|
431
|
+
/**
|
|
432
|
+
* Whether to display the optional argument value.
|
|
433
|
+
*/
|
|
434
|
+
usageOptionValue?: boolean;
|
|
435
|
+
/**
|
|
436
|
+
* Whether to display the command usage.
|
|
437
|
+
*/
|
|
438
|
+
usageSilent?: boolean;
|
|
439
|
+
/**
|
|
440
|
+
* Render function the command usage.
|
|
441
|
+
*/
|
|
442
|
+
renderUsage?: ((ctx: Readonly<CommandContext<G>>) => Promise<string>) | null;
|
|
443
|
+
/**
|
|
444
|
+
* Render function the header section in the command usage.
|
|
445
|
+
*/
|
|
446
|
+
renderHeader?: ((ctx: Readonly<CommandContext<G>>) => Promise<string>) | null;
|
|
447
|
+
/**
|
|
448
|
+
* Render function the validation errors.
|
|
449
|
+
*/
|
|
450
|
+
renderValidationErrors?: ((ctx: Readonly<CommandContext<G>>, error: AggregateError) => Promise<string>) | null;
|
|
451
|
+
/**
|
|
452
|
+
* User plugins.
|
|
453
|
+
*/
|
|
454
|
+
plugins?: Plugin[];
|
|
455
|
+
/**
|
|
456
|
+
* Hook that runs before any command execution
|
|
457
|
+
* @param ctx - The command context
|
|
458
|
+
*/
|
|
459
|
+
onBeforeCommand?: (ctx: Readonly<CommandContext<G>>) => Awaitable<void>;
|
|
460
|
+
/**
|
|
461
|
+
* Hook that runs after successful command execution
|
|
462
|
+
* @param ctx - The command context
|
|
463
|
+
* @param result - The command execution result
|
|
464
|
+
*/
|
|
465
|
+
onAfterCommand?: (ctx: Readonly<CommandContext<G>>, result: string | void) => Awaitable<void>;
|
|
466
|
+
/**
|
|
467
|
+
* Hook that runs when a command throws an error
|
|
468
|
+
* @param ctx - The command context
|
|
469
|
+
* @param error - The error thrown during execution
|
|
470
|
+
*/
|
|
471
|
+
onErrorCommand?: (ctx: Readonly<CommandContext<G>>, error: Error) => Awaitable<void>;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Command call mode.
|
|
475
|
+
*/
|
|
476
|
+
type CommandCallMode = 'entry' | 'subCommand' | 'unexpected';
|
|
477
|
+
/**
|
|
478
|
+
* Command context.
|
|
479
|
+
* Command context is the context of the command execution.
|
|
480
|
+
*/
|
|
481
|
+
interface CommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
482
|
+
/**
|
|
483
|
+
* Command name, that is the command that is executed.
|
|
484
|
+
* The command name is same {@link CommandEnvironment.name}.
|
|
485
|
+
*/
|
|
486
|
+
name: string | undefined;
|
|
487
|
+
/**
|
|
488
|
+
* Command description, that is the description of the command that is executed.
|
|
489
|
+
* The command description is same {@link CommandEnvironment.description}.
|
|
490
|
+
*/
|
|
491
|
+
description: string | undefined;
|
|
492
|
+
/**
|
|
493
|
+
* Command environment, that is the environment of the command that is executed.
|
|
494
|
+
* The command environment is same {@link CommandEnvironment}.
|
|
495
|
+
*/
|
|
496
|
+
env: Readonly<CommandEnvironment<G>>;
|
|
497
|
+
/**
|
|
498
|
+
* Command arguments, that is the arguments of the command that is executed.
|
|
499
|
+
* The command arguments is same {@link Command.args}.
|
|
500
|
+
*/
|
|
501
|
+
args: ExtractArgs<G>;
|
|
502
|
+
/**
|
|
503
|
+
* Command values, that is the values of the command that is executed.
|
|
504
|
+
* Resolve values with `resolveArgs` from command arguments and {@link Command.args}.
|
|
505
|
+
*/
|
|
506
|
+
values: ArgValues<ExtractArgs<G>>;
|
|
507
|
+
/**
|
|
508
|
+
* Command positionals arguments, that is the positionals of the command that is executed.
|
|
509
|
+
* Resolve positionals with `resolveArgs` from command arguments.
|
|
510
|
+
*/
|
|
511
|
+
positionals: string[];
|
|
512
|
+
/**
|
|
513
|
+
* Command rest arguments, that is the remaining argument not resolved by the optional command option delimiter `--`.
|
|
514
|
+
*/
|
|
515
|
+
rest: string[];
|
|
516
|
+
/**
|
|
517
|
+
* Original command line arguments.
|
|
518
|
+
* This argument is passed from `cli` function.
|
|
519
|
+
*/
|
|
520
|
+
_: string[];
|
|
521
|
+
/**
|
|
522
|
+
* Argument tokens, that is parsed by `parseArgs` function.
|
|
523
|
+
*/
|
|
524
|
+
tokens: ArgToken[];
|
|
525
|
+
/**
|
|
526
|
+
* Whether the currently executing command has been executed with the sub-command name omitted.
|
|
527
|
+
*/
|
|
528
|
+
omitted: boolean;
|
|
529
|
+
/**
|
|
530
|
+
* Command call mode.
|
|
531
|
+
* The command call mode is `entry` when the command is executed as an entry command, and `subCommand` when the command is executed as a sub-command.
|
|
532
|
+
*/
|
|
533
|
+
callMode: CommandCallMode;
|
|
534
|
+
/**
|
|
535
|
+
* Whether to convert the camel-case style argument name to kebab-case.
|
|
536
|
+
* This context value is set from {@link Command.toKebab} option.
|
|
537
|
+
*/
|
|
538
|
+
toKebab?: boolean;
|
|
539
|
+
/**
|
|
540
|
+
* Output a message.
|
|
541
|
+
* If {@link CommandEnvironment.usageSilent} is true, the message is not output.
|
|
542
|
+
* @param message an output message, @see {@link console.log}
|
|
543
|
+
* @param optionalParams an optional parameters, @see {@link console.log}
|
|
544
|
+
* @internal
|
|
545
|
+
*/
|
|
546
|
+
log: (message?: any, ...optionalParams: any[]) => void;
|
|
547
|
+
/**
|
|
548
|
+
* Command context extensions.
|
|
549
|
+
*/
|
|
550
|
+
extensions: keyof ExtractExtensions<G> extends never ? undefined : ExtractExtensions<G>;
|
|
551
|
+
/**
|
|
552
|
+
* Validation error from argument parsing.
|
|
553
|
+
* This will be set if argument validation fails during CLI execution.
|
|
554
|
+
*/
|
|
555
|
+
validationError?: AggregateError;
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* CommandContextCore type (base type without extensions)
|
|
559
|
+
*/
|
|
560
|
+
type CommandContextCore<G extends GunshiParamsConstraint = DefaultGunshiParams> = Readonly<CommandContext<G>>;
|
|
561
|
+
/**
|
|
562
|
+
* Command context extension
|
|
563
|
+
*/
|
|
564
|
+
interface CommandContextExtension<E extends GunshiParams['extensions'] = DefaultGunshiParams['extensions']> {
|
|
565
|
+
readonly key: symbol;
|
|
566
|
+
readonly factory: (ctx: CommandContextCore, cmd: Command) => Awaitable<E>;
|
|
567
|
+
readonly onFactory?: (ctx: Readonly<CommandContext>, cmd: Readonly<Command>) => Awaitable<void>;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Command interface.
|
|
571
|
+
*/
|
|
572
|
+
interface Command<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
573
|
+
/**
|
|
574
|
+
* Command name.
|
|
575
|
+
* It's used to find command line arguments to execute from sub commands, and it's recommended to specify.
|
|
576
|
+
*/
|
|
577
|
+
name?: string;
|
|
578
|
+
/**
|
|
579
|
+
* Command description.
|
|
580
|
+
* It's used to describe the command in usage and it's recommended to specify.
|
|
581
|
+
*/
|
|
582
|
+
description?: string;
|
|
583
|
+
/**
|
|
584
|
+
* Command arguments.
|
|
585
|
+
* Each argument can include a description property to describe the argument in usage.
|
|
586
|
+
*/
|
|
587
|
+
args?: ExtractArgs<G>;
|
|
588
|
+
/**
|
|
589
|
+
* Command examples.
|
|
590
|
+
* examples of how to use the command.
|
|
591
|
+
*/
|
|
592
|
+
examples?: string | CommandExamplesFetcher<G>;
|
|
593
|
+
/**
|
|
594
|
+
* Command runner. it's the command to be executed
|
|
595
|
+
*/
|
|
596
|
+
run?: CommandRunner<G>;
|
|
597
|
+
/**
|
|
598
|
+
* Whether to convert the camel-case style argument name to kebab-case.
|
|
599
|
+
* If you will set to `true`, All {@link Command.args} names will be converted to kebab-case.
|
|
600
|
+
*/
|
|
601
|
+
toKebab?: boolean;
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Lazy command interface.
|
|
605
|
+
* Lazy command that's not loaded until it is executed.
|
|
606
|
+
*/
|
|
607
|
+
type LazyCommand<G extends GunshiParamsConstraint = DefaultGunshiParams> = {
|
|
608
|
+
/**
|
|
609
|
+
* Command load function
|
|
610
|
+
*/
|
|
611
|
+
(): Awaitable<Command<G> | CommandRunner<G>>;
|
|
612
|
+
/**
|
|
613
|
+
* Command name
|
|
614
|
+
*/
|
|
615
|
+
commandName?: string;
|
|
616
|
+
} & Omit<Command<G>, 'run' | 'name'>;
|
|
617
|
+
/**
|
|
618
|
+
* Define a command type.
|
|
619
|
+
*/
|
|
620
|
+
type Commandable<G extends GunshiParamsConstraint = DefaultGunshiParams> = Command<G> | LazyCommand<G>;
|
|
621
|
+
/**
|
|
622
|
+
* Command examples fetcher.
|
|
623
|
+
* @param ctx A {@link CommandContext | command context}
|
|
624
|
+
* @returns A fetched command examples.
|
|
625
|
+
*/
|
|
626
|
+
type CommandExamplesFetcher<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ctx: Readonly<CommandContext<G>>) => Awaitable<string>;
|
|
627
|
+
/**
|
|
628
|
+
* Command runner.
|
|
629
|
+
* @param ctx A {@link CommandContext | command context}
|
|
630
|
+
* @returns void or string (for CLI output)
|
|
631
|
+
*/
|
|
632
|
+
type CommandRunner<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ctx: Readonly<CommandContext<G>>) => Awaitable<void | string>;
|
|
633
|
+
/**
|
|
634
|
+
* Command loader.
|
|
635
|
+
* A function that returns a command or command runner.
|
|
636
|
+
* This is used to lazily load commands.
|
|
637
|
+
* @returns A command or command runner
|
|
638
|
+
*/
|
|
639
|
+
type CommandLoader<G extends GunshiParamsConstraint = DefaultGunshiParams> = () => Awaitable<Command<G> | CommandRunner<G>>;
|
|
640
|
+
/**
|
|
641
|
+
* Command decorator.
|
|
642
|
+
* A function that wraps a command runner to add or modify its behavior.
|
|
643
|
+
* @param baseRunner The base command runner to decorate
|
|
644
|
+
* @returns The decorated command runner
|
|
645
|
+
*/
|
|
646
|
+
type CommandDecorator<G extends GunshiParamsConstraint = DefaultGunshiParams> = (baseRunner: (ctx: Readonly<CommandContext<G>>) => Awaitable<void | string>) => (ctx: Readonly<CommandContext<G>>) => Awaitable<void | string>;
|
|
647
|
+
/**
|
|
648
|
+
* Renderer decorator type.
|
|
649
|
+
* A function that wraps a base renderer to add or modify its behavior.
|
|
650
|
+
* @param baseRenderer The base renderer function to decorate
|
|
651
|
+
* @param ctx The command context
|
|
652
|
+
* @returns The decorated result
|
|
653
|
+
*/
|
|
654
|
+
type RendererDecorator<T, G extends GunshiParamsConstraint = DefaultGunshiParams> = (baseRenderer: (ctx: Readonly<CommandContext<G>>) => Promise<T>, ctx: Readonly<CommandContext<G>>) => Promise<T>;
|
|
655
|
+
/**
|
|
656
|
+
* Validation errors renderer decorator type.
|
|
657
|
+
* A function that wraps a validation errors renderer to add or modify its behavior.
|
|
658
|
+
* @param baseRenderer The base validation errors renderer function to decorate
|
|
659
|
+
* @param ctx The command context
|
|
660
|
+
* @param error The aggregate error containing validation errors
|
|
661
|
+
* @returns The decorated result
|
|
662
|
+
*/
|
|
663
|
+
type ValidationErrorsDecorator<G extends GunshiParamsConstraint = DefaultGunshiParams> = (baseRenderer: (ctx: Readonly<CommandContext<G>>, error: AggregateError) => Promise<string>, ctx: Readonly<CommandContext<G>>, error: AggregateError) => Promise<string>;
|
|
664
|
+
|
|
665
|
+
//#endregion
|
|
666
|
+
//#region ../gunshi/src/cli/bone.d.ts
|
|
667
|
+
/**
|
|
668
|
+
* Run the command.
|
|
669
|
+
* @param args Command line arguments
|
|
670
|
+
* @param entry A {@link Command | entry command}, an {@link CommandRunner | inline command runner}, or a {@link LazyCommand | lazily-loaded command}
|
|
671
|
+
* @param options A {@link CliOptions | CLI options}
|
|
672
|
+
* @returns A rendered usage or undefined. if you will use {@link CliOptions.usageSilent} option, it will return rendered usage string.
|
|
673
|
+
*/
|
|
674
|
+
declare function cli<A extends Args = Args, G extends GunshiParams = {
|
|
675
|
+
args: A;
|
|
676
|
+
extensions: {};
|
|
677
|
+
}>(argv: string[], entry: Command<G> | CommandRunner<G> | LazyCommand<G>, options?: CliOptions<G>): Promise<string | undefined>;
|
|
678
|
+
/**
|
|
679
|
+
* Run the command.
|
|
680
|
+
* @param args Command line arguments
|
|
681
|
+
* @param entry A {@link Command | entry command}, an {@link CommandRunner | inline command runner}, or a {@link LazyCommand | lazily-loaded command}
|
|
682
|
+
* @param options A {@link CliOptions | CLI options}
|
|
683
|
+
* @returns A rendered usage or undefined. if you will use {@link CliOptions.usageSilent} option, it will return rendered usage string.
|
|
684
|
+
*/
|
|
685
|
+
declare function cli<E extends ExtendContext = ExtendContext, G extends GunshiParams = {
|
|
686
|
+
args: Args;
|
|
687
|
+
extensions: E;
|
|
688
|
+
}>(argv: string[], entry: Command<G> | CommandRunner<G> | LazyCommand<G>, options?: CliOptions<G>): Promise<string | undefined>;
|
|
689
|
+
/**
|
|
690
|
+
* Run the command.
|
|
691
|
+
* @param args Command line arguments
|
|
692
|
+
* @param entry A {@link Command | entry command}, an {@link CommandRunner | inline command runner}, or a {@link LazyCommand | lazily-loaded command}
|
|
693
|
+
* @param options A {@link CliOptions | CLI options}
|
|
694
|
+
* @returns A rendered usage or undefined. if you will use {@link CliOptions.usageSilent} option, it will return rendered usage string.
|
|
695
|
+
*/
|
|
696
|
+
declare function cli<G extends GunshiParams = DefaultGunshiParams>(argv: string[], entry: Command<G> | CommandRunner<G> | LazyCommand<G>, options?: CliOptions<G>): Promise<string | undefined>;
|
|
697
|
+
|
|
698
|
+
//#endregion
|
|
699
|
+
export { ArgSchema, ArgToken, ArgValues, Args, Awaitable, CliOptions, Command, CommandCallMode, CommandContext, CommandContextCore, CommandContextExtension, CommandDecorator, CommandEnvironment, CommandExamplesFetcher, CommandLoader, CommandRunner, Commandable, DefaultGunshiParams, ExtendContext, ExtractArgs, ExtractExtensions, GunshiParams, GunshiParamsConstraint, LazyCommand, NormalizeToGunshiParams, RendererDecorator, ValidationErrorsDecorator, cli };
|