@guanghechen/commander 4.6.0 → 4.7.1
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/CHANGELOG.md +14 -0
- package/README.md +13 -15
- package/lib/cjs/browser.cjs +1779 -0
- package/lib/cjs/index.cjs +176 -1028
- package/lib/cjs/node.cjs +2165 -0
- package/lib/esm/browser.mjs +1764 -0
- package/lib/esm/index.mjs +175 -1021
- package/lib/esm/node.mjs +2145 -0
- package/lib/types/browser.d.ts +551 -0
- package/lib/types/index.d.ts +15 -191
- package/lib/types/node.d.ts +604 -0
- package/package.json +13 -10
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
import { IReporter } from '@guanghechen/reporter';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type definitions for @guanghechen/commander
|
|
5
|
+
*
|
|
6
|
+
* @module @guanghechen/commander
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Token type: long option, short option, or positional */
|
|
10
|
+
type ICommandTokenType = 'long' | 'short' | 'none';
|
|
11
|
+
/**
|
|
12
|
+
* Command token after preprocessing.
|
|
13
|
+
*
|
|
14
|
+
* - original: raw input for error messages (e.g., --LOG-LEVEL=info, -v)
|
|
15
|
+
* - resolved: normalized form (e.g., --logLevel=info, -v)
|
|
16
|
+
* - name: option name for matching (e.g., logLevel, v, '')
|
|
17
|
+
* - type: token type (long/short/none)
|
|
18
|
+
*/
|
|
19
|
+
interface ICommandToken {
|
|
20
|
+
/** Raw input, used for error display */
|
|
21
|
+
original: string;
|
|
22
|
+
/** Normalized form, used for parsing */
|
|
23
|
+
resolved: string;
|
|
24
|
+
/** Option name for matching: camelCase for long, single char for short, '' for positional */
|
|
25
|
+
name: string;
|
|
26
|
+
/** Token type */
|
|
27
|
+
type: ICommandTokenType;
|
|
28
|
+
}
|
|
29
|
+
/** Option value type */
|
|
30
|
+
type ICommandOptionType = 'boolean' | 'number' | 'string';
|
|
31
|
+
/** Option argument mode */
|
|
32
|
+
type ICommandOptionArgs = 'none' | 'required' | 'variadic';
|
|
33
|
+
/**
|
|
34
|
+
* Option configuration.
|
|
35
|
+
*
|
|
36
|
+
* `type` and `args` must be specified together. Valid combinations:
|
|
37
|
+
* - boolean + none → boolean
|
|
38
|
+
* - string + required → string
|
|
39
|
+
* - number + required → number
|
|
40
|
+
* - string + variadic → string[]
|
|
41
|
+
* - number + variadic → number[]
|
|
42
|
+
*
|
|
43
|
+
* @template T - The type of the option value
|
|
44
|
+
*/
|
|
45
|
+
interface ICommandOptionConfig<T = unknown> {
|
|
46
|
+
/** Long option name (camelCase, required) */
|
|
47
|
+
long: string;
|
|
48
|
+
/** Short option (single character) */
|
|
49
|
+
short?: string;
|
|
50
|
+
/** Value type (required) */
|
|
51
|
+
type: ICommandOptionType;
|
|
52
|
+
/** Argument mode (required) */
|
|
53
|
+
args: ICommandOptionArgs;
|
|
54
|
+
/** Description for help text */
|
|
55
|
+
desc: string;
|
|
56
|
+
/** Whether this option is required (mutually exclusive with default) */
|
|
57
|
+
required?: boolean;
|
|
58
|
+
/** Default value when not provided */
|
|
59
|
+
default?: T;
|
|
60
|
+
/** Allowed values for validation and completion */
|
|
61
|
+
choices?: T extends Array<infer U> ? U[] : T[];
|
|
62
|
+
/** Single value transformation (called for each value, before choices validation) */
|
|
63
|
+
coerce?: (rawValue: string) => T extends Array<infer U> ? U : T;
|
|
64
|
+
/** Callback after parsing, applies value to context */
|
|
65
|
+
apply?: (value: T, ctx: ICommandContext) => void;
|
|
66
|
+
}
|
|
67
|
+
/** Argument kind */
|
|
68
|
+
type ICommandArgumentKind = 'required' | 'optional' | 'variadic';
|
|
69
|
+
/** Argument value type */
|
|
70
|
+
type ICommandArgumentType = 'string' | 'number';
|
|
71
|
+
/**
|
|
72
|
+
* Positional argument configuration.
|
|
73
|
+
*
|
|
74
|
+
* Constraints:
|
|
75
|
+
* - required arguments must come before optional
|
|
76
|
+
* - variadic can only appear once, and must be last
|
|
77
|
+
* - required cannot have default
|
|
78
|
+
*
|
|
79
|
+
* @template T - The type of the argument value
|
|
80
|
+
*/
|
|
81
|
+
interface ICommandArgumentConfig<T = unknown> {
|
|
82
|
+
/** Argument name */
|
|
83
|
+
name: string;
|
|
84
|
+
/** Argument description */
|
|
85
|
+
desc: string;
|
|
86
|
+
/** Argument kind: required / optional / variadic */
|
|
87
|
+
kind: ICommandArgumentKind;
|
|
88
|
+
/** Value type, defaults to 'string' */
|
|
89
|
+
type?: ICommandArgumentType;
|
|
90
|
+
/** Default value when not provided (only for optional arguments) */
|
|
91
|
+
default?: T;
|
|
92
|
+
/** Custom value transformation (takes precedence over type conversion) */
|
|
93
|
+
coerce?: (rawValue: string) => T;
|
|
94
|
+
}
|
|
95
|
+
interface ICommandBuiltinOptionConfig {
|
|
96
|
+
/** Enable built-in --version option (requires configured version on target command) */
|
|
97
|
+
version?: boolean;
|
|
98
|
+
/** Enable built-in --color/--no-color option for help rendering (defaults respect NO_COLOR) */
|
|
99
|
+
color?: boolean;
|
|
100
|
+
/** Enable built-in --log-level option */
|
|
101
|
+
logLevel?: boolean;
|
|
102
|
+
/** Enable built-in --silent option */
|
|
103
|
+
silent?: boolean;
|
|
104
|
+
/** Enable built-in --log-date/--no-log-date option */
|
|
105
|
+
logDate?: boolean;
|
|
106
|
+
/** Enable built-in --log-colorful/--no-log-colorful option */
|
|
107
|
+
logColorful?: boolean;
|
|
108
|
+
}
|
|
109
|
+
interface ICommandBuiltinConfig {
|
|
110
|
+
/** Built-in options configuration */
|
|
111
|
+
option?: boolean | ICommandBuiltinOptionConfig;
|
|
112
|
+
}
|
|
113
|
+
/** Command example configuration */
|
|
114
|
+
interface ICommandExample {
|
|
115
|
+
/** Example title */
|
|
116
|
+
title: string;
|
|
117
|
+
/** Usage fragment relative to command path */
|
|
118
|
+
usage: string;
|
|
119
|
+
/** Example description */
|
|
120
|
+
desc: string;
|
|
121
|
+
}
|
|
122
|
+
/** Command preset defaults */
|
|
123
|
+
interface ICommandPresetConfig {
|
|
124
|
+
/** Preset root directory (absolute path) */
|
|
125
|
+
root?: string;
|
|
126
|
+
/** Default preset options file */
|
|
127
|
+
opt?: string;
|
|
128
|
+
/** Default preset envs file */
|
|
129
|
+
env?: string;
|
|
130
|
+
}
|
|
131
|
+
/** Runtime file stats abstraction */
|
|
132
|
+
interface ICommandRuntimeStats {
|
|
133
|
+
isDirectory(): boolean;
|
|
134
|
+
}
|
|
135
|
+
/** Runtime abstraction for environment-dependent operations */
|
|
136
|
+
interface ICommandRuntime {
|
|
137
|
+
/** Current working directory */
|
|
138
|
+
cwd(): string;
|
|
139
|
+
/** Path absolute check */
|
|
140
|
+
isAbsolute(filepath: string): boolean;
|
|
141
|
+
/** Resolve paths into an absolute path */
|
|
142
|
+
resolve(...paths: string[]): string;
|
|
143
|
+
/** Read UTF-8 text file */
|
|
144
|
+
readFile(filepath: string): Promise<string>;
|
|
145
|
+
/** Stat file system entry */
|
|
146
|
+
stat(filepath: string): Promise<ICommandRuntimeStats>;
|
|
147
|
+
}
|
|
148
|
+
/** Command configuration */
|
|
149
|
+
interface ICommandConfig {
|
|
150
|
+
/** Command name (only for root command) */
|
|
151
|
+
name?: string;
|
|
152
|
+
/** Command description */
|
|
153
|
+
desc: string;
|
|
154
|
+
/** Version (for built-in --version on this command) */
|
|
155
|
+
version?: string;
|
|
156
|
+
/** Built-in features configuration */
|
|
157
|
+
builtin?: boolean | ICommandBuiltinConfig;
|
|
158
|
+
/** Command-level preset defaults */
|
|
159
|
+
preset?: ICommandPresetConfig;
|
|
160
|
+
/** Default reporter for this command */
|
|
161
|
+
reporter?: IReporter;
|
|
162
|
+
/** Runtime adapter for environment-dependent operations */
|
|
163
|
+
runtime?: ICommandRuntime;
|
|
164
|
+
}
|
|
165
|
+
/** Command interface */
|
|
166
|
+
interface ICommand {
|
|
167
|
+
readonly name: string | undefined;
|
|
168
|
+
readonly description: string;
|
|
169
|
+
readonly version: string | undefined;
|
|
170
|
+
readonly builtin: ICommandConfig['builtin'] | undefined;
|
|
171
|
+
readonly preset: ICommandPresetConfig | undefined;
|
|
172
|
+
readonly parent: ICommand | undefined;
|
|
173
|
+
readonly options: ICommandOptionConfig[];
|
|
174
|
+
readonly arguments: ICommandArgumentConfig[];
|
|
175
|
+
readonly examples: ICommandExample[];
|
|
176
|
+
readonly subcommands: Map<string, ICommand>;
|
|
177
|
+
}
|
|
178
|
+
/** Execution context */
|
|
179
|
+
interface ICommandContext {
|
|
180
|
+
/** Current command node */
|
|
181
|
+
cmd: ICommand;
|
|
182
|
+
/** Command chain from root to leaf */
|
|
183
|
+
chain: ICommand[];
|
|
184
|
+
/** Effective environment variables */
|
|
185
|
+
envs: Record<string, string | undefined>;
|
|
186
|
+
/** Built-in control hit status */
|
|
187
|
+
controls: ICommandControls;
|
|
188
|
+
/** Input source snapshots */
|
|
189
|
+
sources: ICommandInputSources;
|
|
190
|
+
/** Reporter instance */
|
|
191
|
+
reporter: IReporter;
|
|
192
|
+
}
|
|
193
|
+
/** Action callback parameters */
|
|
194
|
+
interface ICommandActionParams {
|
|
195
|
+
/** Execution context */
|
|
196
|
+
ctx: ICommandContext;
|
|
197
|
+
/** Parsed options (keyed by long name) */
|
|
198
|
+
opts: ICommandParsedOpts;
|
|
199
|
+
/** Parsed positional arguments (keyed by argument name) */
|
|
200
|
+
args: ICommandParsedArgs;
|
|
201
|
+
/** Raw positional argument strings (before type conversion) */
|
|
202
|
+
rawArgs: string[];
|
|
203
|
+
}
|
|
204
|
+
/** Action handler function */
|
|
205
|
+
type ICommandAction = (params: ICommandActionParams) => void | Promise<void>;
|
|
206
|
+
/** run() / parse() method parameters */
|
|
207
|
+
interface ICommandRunParams {
|
|
208
|
+
/** Command line arguments (usually process.argv.slice(2)) */
|
|
209
|
+
argv: string[];
|
|
210
|
+
/** Environment variables (usually process.env) */
|
|
211
|
+
envs: Record<string, string | undefined>;
|
|
212
|
+
/** Optional reporter override */
|
|
213
|
+
reporter?: IReporter;
|
|
214
|
+
}
|
|
215
|
+
/** Parsed options record */
|
|
216
|
+
type ICommandParsedOpts = Record<string, unknown>;
|
|
217
|
+
/** Parsed arguments record */
|
|
218
|
+
type ICommandParsedArgs = Record<string, unknown>;
|
|
219
|
+
/** Route stage result */
|
|
220
|
+
interface ICommandRouteResult<TCommand = ICommand> {
|
|
221
|
+
/** Command chain from root to leaf */
|
|
222
|
+
chain: TCommand[];
|
|
223
|
+
/** Remaining argv after routing */
|
|
224
|
+
remaining: string[];
|
|
225
|
+
/** Routed command tokens from user argv (name/alias) */
|
|
226
|
+
cmds: string[];
|
|
227
|
+
}
|
|
228
|
+
/** Control-scan stage result */
|
|
229
|
+
interface ICommandControlScanResult {
|
|
230
|
+
/** Built-in control hit status */
|
|
231
|
+
controls: ICommandControls;
|
|
232
|
+
/** Remaining argv after stripping control tokens */
|
|
233
|
+
remaining: string[];
|
|
234
|
+
/** Optional target token from `help <child>` syntax */
|
|
235
|
+
helpTarget?: string;
|
|
236
|
+
}
|
|
237
|
+
/** Preset stage result */
|
|
238
|
+
interface ICommandPresetResult {
|
|
239
|
+
/** Effective tail argv after preset merge */
|
|
240
|
+
tailArgv: string[];
|
|
241
|
+
/** Effective envs after preset merge */
|
|
242
|
+
envs: Record<string, string | undefined>;
|
|
243
|
+
}
|
|
244
|
+
/** Tokenize stage result */
|
|
245
|
+
interface ICommandTokenizeResult {
|
|
246
|
+
/** Option tokens (before --) */
|
|
247
|
+
optionTokens: ICommandToken[];
|
|
248
|
+
/** Arguments after -- */
|
|
249
|
+
restArgs: string[];
|
|
250
|
+
}
|
|
251
|
+
/** Resolve stage result */
|
|
252
|
+
interface ICommandResolveResult {
|
|
253
|
+
/** Tokens consumed by each command */
|
|
254
|
+
consumedTokens: Map<ICommand, ICommandToken[]>;
|
|
255
|
+
/** Argument tokens (non-option tokens) */
|
|
256
|
+
argTokens: ICommandToken[];
|
|
257
|
+
}
|
|
258
|
+
/** shift() method result (internal) */
|
|
259
|
+
interface ICommandShiftResult {
|
|
260
|
+
/** Tokens consumed by this command */
|
|
261
|
+
consumed: ICommandToken[];
|
|
262
|
+
/** Remaining tokens to pass to parent */
|
|
263
|
+
remaining: ICommandToken[];
|
|
264
|
+
}
|
|
265
|
+
/** Parse stage result */
|
|
266
|
+
interface ICommandParseResult {
|
|
267
|
+
/** Execution context */
|
|
268
|
+
ctx: ICommandContext;
|
|
269
|
+
/** Parsed options */
|
|
270
|
+
opts: ICommandParsedOpts;
|
|
271
|
+
/** Parsed arguments */
|
|
272
|
+
args: ICommandParsedArgs;
|
|
273
|
+
/** Raw argument strings */
|
|
274
|
+
rawArgs: string[];
|
|
275
|
+
}
|
|
276
|
+
/** Input source snapshots for debugging/tracing */
|
|
277
|
+
interface ICommandInputSources {
|
|
278
|
+
preset: {
|
|
279
|
+
argv: string[];
|
|
280
|
+
envs: Record<string, string>;
|
|
281
|
+
};
|
|
282
|
+
user: {
|
|
283
|
+
/** Routed command tokens (name/alias as entered by user) */
|
|
284
|
+
cmds: string[];
|
|
285
|
+
/** Clean user tail argv after removing command chain/control/preset directives */
|
|
286
|
+
argv: string[];
|
|
287
|
+
/** Raw env snapshot from run/parse params */
|
|
288
|
+
envs: Record<string, string | undefined>;
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
/** Built-in run controls */
|
|
292
|
+
interface ICommandControls {
|
|
293
|
+
help: boolean;
|
|
294
|
+
version: boolean;
|
|
295
|
+
}
|
|
296
|
+
/** Built-in option resolution result (internal) */
|
|
297
|
+
interface ICommandBuiltinOptionResolved {
|
|
298
|
+
version: boolean;
|
|
299
|
+
color: boolean;
|
|
300
|
+
logLevel: boolean;
|
|
301
|
+
silent: boolean;
|
|
302
|
+
logDate: boolean;
|
|
303
|
+
logColorful: boolean;
|
|
304
|
+
}
|
|
305
|
+
/** Built-in config resolution result (internal) */
|
|
306
|
+
interface ICommandBuiltinResolved {
|
|
307
|
+
option: ICommandBuiltinOptionResolved;
|
|
308
|
+
}
|
|
309
|
+
/** Subcommand registry entry (internal) */
|
|
310
|
+
interface ISubcommandEntry<TCommand = ICommand> {
|
|
311
|
+
name: string;
|
|
312
|
+
aliases: string[];
|
|
313
|
+
command: TCommand;
|
|
314
|
+
}
|
|
315
|
+
/** Help option line (internal) */
|
|
316
|
+
interface IHelpOptionLine {
|
|
317
|
+
sig: string;
|
|
318
|
+
desc: string;
|
|
319
|
+
}
|
|
320
|
+
/** Help command line (internal) */
|
|
321
|
+
interface IHelpCommandLine {
|
|
322
|
+
name: string;
|
|
323
|
+
desc: string;
|
|
324
|
+
}
|
|
325
|
+
/** Help example line (internal) */
|
|
326
|
+
interface IHelpExampleLine {
|
|
327
|
+
title: string;
|
|
328
|
+
usage: string;
|
|
329
|
+
desc: string;
|
|
330
|
+
}
|
|
331
|
+
/** Structured help data for rendering (internal) */
|
|
332
|
+
interface IHelpData {
|
|
333
|
+
desc: string;
|
|
334
|
+
usage: string;
|
|
335
|
+
options: IHelpOptionLine[];
|
|
336
|
+
commands: IHelpCommandLine[];
|
|
337
|
+
examples: IHelpExampleLine[];
|
|
338
|
+
}
|
|
339
|
+
/** Error kinds for command parsing */
|
|
340
|
+
type ICommanderErrorKind = 'InvalidOptionFormat' | 'InvalidNegativeOption' | 'NegativeOptionWithValue' | 'NegativeOptionType' | 'UnknownOption' | 'UnknownSubcommand' | 'UnexpectedArgument' | 'MissingValue' | 'InvalidType' | 'UnsupportedShortSyntax' | 'OptionConflict' | 'MissingRequired' | 'InvalidChoice' | 'InvalidBooleanValue' | 'MissingRequiredArgument' | 'TooManyArguments' | 'ConfigurationError';
|
|
341
|
+
/** Commander error with structured information */
|
|
342
|
+
declare class CommanderError extends Error {
|
|
343
|
+
readonly kind: ICommanderErrorKind;
|
|
344
|
+
readonly commandPath: string;
|
|
345
|
+
constructor(kind: ICommanderErrorKind, message: string, commandPath: string);
|
|
346
|
+
/** Format error with help hint */
|
|
347
|
+
format(): string;
|
|
348
|
+
}
|
|
349
|
+
/** Shell type for completion */
|
|
350
|
+
type ICompletionShellType = 'bash' | 'fish' | 'pwsh';
|
|
351
|
+
/** Option metadata for completion */
|
|
352
|
+
interface ICompletionOptionMeta {
|
|
353
|
+
/** Long option name (camelCase) */
|
|
354
|
+
long: string;
|
|
355
|
+
/** Short option */
|
|
356
|
+
short?: string;
|
|
357
|
+
/** Description */
|
|
358
|
+
desc: string;
|
|
359
|
+
/** Whether option takes value (args !== 'none') */
|
|
360
|
+
takesValue: boolean;
|
|
361
|
+
/** Allowed values */
|
|
362
|
+
choices?: string[];
|
|
363
|
+
}
|
|
364
|
+
/** Command metadata for completion */
|
|
365
|
+
interface ICompletionMeta {
|
|
366
|
+
/** Command name */
|
|
367
|
+
name: string;
|
|
368
|
+
/** Description */
|
|
369
|
+
desc: string;
|
|
370
|
+
/** Command aliases */
|
|
371
|
+
aliases: string[];
|
|
372
|
+
/** Options */
|
|
373
|
+
options: ICompletionOptionMeta[];
|
|
374
|
+
/** Subcommands */
|
|
375
|
+
subcommands: ICompletionMeta[];
|
|
376
|
+
}
|
|
377
|
+
/** Shell completion paths configuration */
|
|
378
|
+
interface ICompletionPaths {
|
|
379
|
+
/** Bash completion file path */
|
|
380
|
+
bash: string;
|
|
381
|
+
/** Fish completion file path */
|
|
382
|
+
fish: string;
|
|
383
|
+
/** PowerShell completion file path */
|
|
384
|
+
pwsh: string;
|
|
385
|
+
}
|
|
386
|
+
/** CompletionCommand configuration */
|
|
387
|
+
interface ICompletionCommandConfig {
|
|
388
|
+
/** Program name for completion scripts (defaults to root.name) */
|
|
389
|
+
programName?: string;
|
|
390
|
+
/** Default completion file paths for each shell */
|
|
391
|
+
paths?: Partial<ICompletionPaths>;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Node runtime adapter
|
|
396
|
+
*
|
|
397
|
+
* @module @guanghechen/commander
|
|
398
|
+
*/
|
|
399
|
+
|
|
400
|
+
declare function createNodeCommandRuntime(): ICommandRuntime;
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Command class - CLI command builder with fluent API
|
|
404
|
+
*
|
|
405
|
+
* Execution flow: route → control-scan → run-control(run) → preset → tokenize → resolve → parse → run
|
|
406
|
+
*
|
|
407
|
+
* @module @guanghechen/commander
|
|
408
|
+
*/
|
|
409
|
+
|
|
410
|
+
declare class Command implements ICommand {
|
|
411
|
+
#private;
|
|
412
|
+
constructor(config: ICommandConfig);
|
|
413
|
+
get name(): string | undefined;
|
|
414
|
+
get description(): string;
|
|
415
|
+
get version(): string | undefined;
|
|
416
|
+
get builtin(): ICommandConfig['builtin'] | undefined;
|
|
417
|
+
get preset(): ICommandPresetConfig | undefined;
|
|
418
|
+
get parent(): Command | undefined;
|
|
419
|
+
get options(): ICommandOptionConfig[];
|
|
420
|
+
get arguments(): ICommandArgumentConfig[];
|
|
421
|
+
get examples(): ICommandExample[];
|
|
422
|
+
get subcommands(): Map<string, ICommand>;
|
|
423
|
+
option<T>(opt: ICommandOptionConfig<T>): this;
|
|
424
|
+
argument<T>(arg: ICommandArgumentConfig<T>): this;
|
|
425
|
+
action(fn: ICommandAction): this;
|
|
426
|
+
example(title: string, usage: string, desc: string): this;
|
|
427
|
+
subcommand(name: string, cmd: Command): this;
|
|
428
|
+
run(params: ICommandRunParams): Promise<void>;
|
|
429
|
+
parse(params: ICommandRunParams): Promise<ICommandParseResult>;
|
|
430
|
+
formatHelp(): string;
|
|
431
|
+
getCompletionMeta(): ICompletionMeta;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Pre-defined coerce factory methods for @guanghechen/commander.
|
|
436
|
+
*
|
|
437
|
+
* @module @guanghechen/commander/coerce
|
|
438
|
+
*/
|
|
439
|
+
declare class Coerce {
|
|
440
|
+
private constructor();
|
|
441
|
+
private static create;
|
|
442
|
+
static choice<TValue extends string>(name: string, values: ReadonlyArray<TValue>, errorMessage?: string): (rawValue: string) => TValue;
|
|
443
|
+
static domain(name: string, errorMessage?: string): (rawValue: string) => string;
|
|
444
|
+
static host(name: string, errorMessage?: string): (rawValue: string) => string;
|
|
445
|
+
static integer(name: string, errorMessage?: string): (rawValue: string) => number;
|
|
446
|
+
static ip(name: string, errorMessage?: string): (rawValue: string) => string;
|
|
447
|
+
static number(name: string, errorMessage?: string): (rawValue: string) => number;
|
|
448
|
+
static port(name: string, errorMessage?: string): (rawValue: string) => number;
|
|
449
|
+
static positiveInteger(name: string, errorMessage?: string): (rawValue: string) => number;
|
|
450
|
+
static positiveNumber(name: string, errorMessage?: string): (rawValue: string) => number;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
declare function isIpv4(rawValue: string): boolean;
|
|
454
|
+
declare function isIpv6(rawValue: string): boolean;
|
|
455
|
+
declare function isIp(rawValue: string): boolean;
|
|
456
|
+
declare function isDomain(rawValue: string): boolean;
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Pre-defined common options for @guanghechen/commander
|
|
460
|
+
*
|
|
461
|
+
* @module @guanghechen/commander/options
|
|
462
|
+
*/
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Pre-defined --log-level option for setting log verbosity.
|
|
466
|
+
*
|
|
467
|
+
* Supports: debug | info | hint | warn | error (case-insensitive)
|
|
468
|
+
*
|
|
469
|
+
* | Property | Value |
|
|
470
|
+
* | --------- | ---------------------------------- |
|
|
471
|
+
* | long | 'logLevel' |
|
|
472
|
+
* | type | 'string' |
|
|
473
|
+
* | args | 'required' |
|
|
474
|
+
* | default | 'info' |
|
|
475
|
+
* | choices | LOG_LEVELS |
|
|
476
|
+
* | coerce | resolveLogLevel (case-insensitive) |
|
|
477
|
+
* | apply | ctx.reporter.setLevel(value) |
|
|
478
|
+
*
|
|
479
|
+
* @example
|
|
480
|
+
* ```typescript
|
|
481
|
+
* import { logLevelOption } from '@guanghechen/commander'
|
|
482
|
+
*
|
|
483
|
+
* const cmd = new Command('app')
|
|
484
|
+
* .option(logLevelOption)
|
|
485
|
+
* .action(({ opts }) => {
|
|
486
|
+
* console.log(opts.logLevel) // 'debug' | 'info' | 'hint' | 'warn' | 'error'
|
|
487
|
+
* })
|
|
488
|
+
*
|
|
489
|
+
* // Override with spread syntax
|
|
490
|
+
* .option({ ...logLevelOption, default: 'warn' })
|
|
491
|
+
* ```
|
|
492
|
+
*/
|
|
493
|
+
declare const logLevelOption: ICommandOptionConfig<string>;
|
|
494
|
+
/**
|
|
495
|
+
* Pre-defined --log-date option for controlling timestamp output.
|
|
496
|
+
*
|
|
497
|
+
* | Property | Value |
|
|
498
|
+
* | --------- | --------- |
|
|
499
|
+
* | long | 'logDate' |
|
|
500
|
+
* | type | 'boolean' |
|
|
501
|
+
* | args | 'none' |
|
|
502
|
+
* | default | true |
|
|
503
|
+
*/
|
|
504
|
+
declare const logDateOption: ICommandOptionConfig<boolean>;
|
|
505
|
+
/**
|
|
506
|
+
* Pre-defined --log-colorful option for controlling colorful output.
|
|
507
|
+
*
|
|
508
|
+
* | Property | Value |
|
|
509
|
+
* | --------- | ------------- |
|
|
510
|
+
* | long | 'logColorful' |
|
|
511
|
+
* | type | 'boolean' |
|
|
512
|
+
* | args | 'none' |
|
|
513
|
+
* | default | true |
|
|
514
|
+
*/
|
|
515
|
+
declare const logColorfulOption: ICommandOptionConfig<boolean>;
|
|
516
|
+
/**
|
|
517
|
+
* Pre-defined --silent option for suppressing non-error output.
|
|
518
|
+
*
|
|
519
|
+
* | Property | Value |
|
|
520
|
+
* | --------- | --------- |
|
|
521
|
+
* | long | 'silent' |
|
|
522
|
+
* | type | 'boolean' |
|
|
523
|
+
* | args | 'none' |
|
|
524
|
+
* | default | false |
|
|
525
|
+
*
|
|
526
|
+
* @example
|
|
527
|
+
* ```typescript
|
|
528
|
+
* import { silentOption } from '@guanghechen/commander'
|
|
529
|
+
*
|
|
530
|
+
* const cmd = new Command('app')
|
|
531
|
+
* .option(silentOption)
|
|
532
|
+
* .action(({ opts }) => {
|
|
533
|
+
* if (!opts.silent) {
|
|
534
|
+
* console.log('Processing...')
|
|
535
|
+
* }
|
|
536
|
+
* })
|
|
537
|
+
* ```
|
|
538
|
+
*/
|
|
539
|
+
declare const silentOption: ICommandOptionConfig<boolean>;
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Browser runtime adapter
|
|
543
|
+
*
|
|
544
|
+
* @module @guanghechen/commander
|
|
545
|
+
*/
|
|
546
|
+
|
|
547
|
+
declare function createBrowserCommandRuntime(): ICommandRuntime;
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Runtime adapters
|
|
551
|
+
*
|
|
552
|
+
* @module @guanghechen/commander
|
|
553
|
+
*/
|
|
554
|
+
|
|
555
|
+
declare function getDefaultCommandRuntime(): ICommandRuntime;
|
|
556
|
+
declare function setDefaultCommandRuntime(runtime: ICommandRuntime): void;
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Shell completion generators
|
|
560
|
+
*
|
|
561
|
+
* @module @guanghechen/commander
|
|
562
|
+
*/
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Built-in completion command that generates shell completion scripts.
|
|
566
|
+
*
|
|
567
|
+
* @example
|
|
568
|
+
* ```typescript
|
|
569
|
+
* const root = new Command({ name: 'mycli', desc: 'My CLI' })
|
|
570
|
+
* root.subcommand('completion', new CompletionCommand(root, {
|
|
571
|
+
* paths: {
|
|
572
|
+
* bash: `~/.local/share/bash-completion/completions/mycli`,
|
|
573
|
+
* fish: `~/.config/fish/completions/mycli.fish`,
|
|
574
|
+
* pwsh: `~/.config/powershell/Microsoft.PowerShell_profile.ps1`,
|
|
575
|
+
* }
|
|
576
|
+
* }))
|
|
577
|
+
*
|
|
578
|
+
* // Usage:
|
|
579
|
+
* // mycli completion --bash > ~/.local/share/bash-completion/completions/mycli
|
|
580
|
+
* // mycli completion --fish --write (writes to default path)
|
|
581
|
+
* // mycli completion --fish --write /custom/path.fish
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
declare class CompletionCommand extends Command {
|
|
585
|
+
constructor(root: Command, config?: ICompletionCommandConfig);
|
|
586
|
+
}
|
|
587
|
+
declare class BashCompletion {
|
|
588
|
+
#private;
|
|
589
|
+
constructor(meta: ICompletionMeta, programName: string);
|
|
590
|
+
generate(): string;
|
|
591
|
+
}
|
|
592
|
+
declare class FishCompletion {
|
|
593
|
+
#private;
|
|
594
|
+
constructor(meta: ICompletionMeta, programName: string);
|
|
595
|
+
generate(): string;
|
|
596
|
+
}
|
|
597
|
+
declare class PwshCompletion {
|
|
598
|
+
#private;
|
|
599
|
+
constructor(meta: ICompletionMeta, programName: string);
|
|
600
|
+
generate(): string;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
export { BashCompletion, Coerce, Command, CommanderError, CompletionCommand, FishCompletion, PwshCompletion, createBrowserCommandRuntime, createNodeCommandRuntime, getDefaultCommandRuntime, isDomain, isIp, isIpv4, isIpv6, logColorfulOption, logDateOption, logLevelOption, setDefaultCommandRuntime, silentOption };
|
|
604
|
+
export type { ICommand, ICommandAction, ICommandActionParams, ICommandArgumentConfig, ICommandArgumentKind, ICommandArgumentType, ICommandBuiltinConfig, ICommandBuiltinOptionConfig, ICommandBuiltinOptionResolved, ICommandBuiltinResolved, ICommandConfig, ICommandContext, ICommandControlScanResult, ICommandControls, ICommandExample, ICommandInputSources, ICommandOptionArgs, ICommandOptionConfig, ICommandOptionType, ICommandParseResult, ICommandParsedArgs, ICommandParsedOpts, ICommandPresetConfig, ICommandPresetResult, ICommandResolveResult, ICommandRouteResult, ICommandRunParams, ICommandRuntime, ICommandRuntimeStats, ICommandShiftResult, ICommandToken, ICommandTokenType, ICommandTokenizeResult, ICommanderErrorKind, ICompletionCommandConfig, ICompletionMeta, ICompletionOptionMeta, ICompletionPaths, ICompletionShellType, IHelpCommandLine, IHelpData, IHelpExampleLine, IHelpOptionLine, ISubcommandEntry };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guanghechen/commander",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.7.1",
|
|
4
4
|
"description": "A minimal, type-safe command-line interface builder with fluent API",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "guanghechen",
|
|
@@ -20,17 +20,20 @@
|
|
|
20
20
|
],
|
|
21
21
|
"type": "module",
|
|
22
22
|
"exports": {
|
|
23
|
-
".":
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
23
|
+
".": null,
|
|
24
|
+
"./browser": {
|
|
25
|
+
"types": "./lib/types/browser.d.ts",
|
|
26
|
+
"source": "./src/runtime/browser/entry.ts",
|
|
27
|
+
"import": "./lib/esm/browser.mjs",
|
|
28
|
+
"require": "./lib/cjs/browser.cjs"
|
|
29
|
+
},
|
|
30
|
+
"./node": {
|
|
31
|
+
"types": "./lib/types/node.d.ts",
|
|
32
|
+
"source": "./src/runtime/node/index.ts",
|
|
33
|
+
"import": "./lib/esm/node.mjs",
|
|
34
|
+
"require": "./lib/cjs/node.cjs"
|
|
28
35
|
}
|
|
29
36
|
},
|
|
30
|
-
"source": "./src/index.ts",
|
|
31
|
-
"main": "./lib/cjs/index.cjs",
|
|
32
|
-
"module": "./lib/esm/index.mjs",
|
|
33
|
-
"types": "./lib/types/index.d.ts",
|
|
34
37
|
"license": "MIT",
|
|
35
38
|
"files": [
|
|
36
39
|
"lib/",
|