@guanghechen/commander 4.5.0 → 4.6.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.
@@ -93,6 +93,8 @@ interface ICommandArgumentConfig<T = unknown> {
93
93
  coerce?: (rawValue: string) => T;
94
94
  }
95
95
  interface ICommandBuiltinOptionConfig {
96
+ /** Enable built-in --version option (root only, requires configured version) */
97
+ version?: boolean;
96
98
  /** Enable built-in --color/--no-color option for help rendering (defaults respect NO_COLOR) */
97
99
  color?: boolean;
98
100
  /** Enable built-in --log-level option */
@@ -104,15 +106,9 @@ interface ICommandBuiltinOptionConfig {
104
106
  /** Enable built-in --log-colorful/--no-log-colorful option */
105
107
  logColorful?: boolean;
106
108
  }
107
- interface ICommandBuiltinCommandConfig {
108
- /** Enable built-in help subcommand */
109
- help?: boolean;
110
- }
111
109
  interface ICommandBuiltinConfig {
112
110
  /** Built-in options configuration */
113
111
  option?: boolean | ICommandBuiltinOptionConfig;
114
- /** Built-in command configuration */
115
- command?: boolean | ICommandBuiltinCommandConfig;
116
112
  }
117
113
  /** Command example configuration */
118
114
  interface ICommandExample {
@@ -123,6 +119,15 @@ interface ICommandExample {
123
119
  /** Example description */
124
120
  desc: string;
125
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
+ }
126
131
  /** Command configuration */
127
132
  interface ICommandConfig {
128
133
  /** Command name (only for root command) */
@@ -133,6 +138,8 @@ interface ICommandConfig {
133
138
  version?: string;
134
139
  /** Built-in features configuration */
135
140
  builtin?: boolean | ICommandBuiltinConfig;
141
+ /** Command-level preset defaults */
142
+ preset?: ICommandPresetConfig;
136
143
  /** Default reporter for this command */
137
144
  reporter?: IReporter;
138
145
  }
@@ -141,6 +148,8 @@ interface ICommand {
141
148
  readonly name: string | undefined;
142
149
  readonly description: string;
143
150
  readonly version: string | undefined;
151
+ readonly builtin: ICommandConfig['builtin'] | undefined;
152
+ readonly preset: ICommandPresetConfig | undefined;
144
153
  readonly parent: ICommand | undefined;
145
154
  readonly options: ICommandOptionConfig[];
146
155
  readonly arguments: ICommandArgumentConfig[];
@@ -151,12 +160,16 @@ interface ICommand {
151
160
  interface ICommandContext {
152
161
  /** Current command node */
153
162
  cmd: ICommand;
154
- /** Environment variables */
163
+ /** Command chain from root to leaf */
164
+ chain: ICommand[];
165
+ /** Effective environment variables */
155
166
  envs: Record<string, string | undefined>;
167
+ /** Built-in control hit status */
168
+ controls: ICommandControls;
169
+ /** Input source snapshots */
170
+ sources: ICommandInputSources;
156
171
  /** Reporter instance */
157
172
  reporter: IReporter;
158
- /** Original argv */
159
- argv: string[];
160
173
  }
161
174
  /** Action callback parameters */
162
175
  interface ICommandActionParams {
@@ -185,11 +198,29 @@ type ICommandParsedOpts = Record<string, unknown>;
185
198
  /** Parsed arguments record */
186
199
  type ICommandParsedArgs = Record<string, unknown>;
187
200
  /** Route stage result */
188
- interface ICommandRouteResult {
201
+ interface ICommandRouteResult<TCommand = ICommand> {
189
202
  /** Command chain from root to leaf */
190
- chain: ICommand[];
203
+ chain: TCommand[];
191
204
  /** Remaining argv after routing */
192
205
  remaining: string[];
206
+ /** Routed command tokens from user argv (name/alias) */
207
+ cmds: string[];
208
+ }
209
+ /** Control-scan stage result */
210
+ interface ICommandControlScanResult {
211
+ /** Built-in control hit status */
212
+ controls: ICommandControls;
213
+ /** Remaining argv after stripping control tokens */
214
+ remaining: string[];
215
+ /** Optional target token from `help <child>` syntax */
216
+ helpTarget?: string;
217
+ }
218
+ /** Preset stage result */
219
+ interface ICommandPresetResult {
220
+ /** Effective tail argv after preset merge */
221
+ tailArgv: string[];
222
+ /** Effective envs after preset merge */
223
+ envs: Record<string, string | undefined>;
193
224
  }
194
225
  /** Tokenize stage result */
195
226
  interface ICommandTokenizeResult {
@@ -223,6 +254,69 @@ interface ICommandParseResult {
223
254
  /** Raw argument strings */
224
255
  rawArgs: string[];
225
256
  }
257
+ /** Input source snapshots for debugging/tracing */
258
+ interface ICommandInputSources {
259
+ preset: {
260
+ argv: string[];
261
+ envs: Record<string, string>;
262
+ };
263
+ user: {
264
+ /** Routed command tokens (name/alias as entered by user) */
265
+ cmds: string[];
266
+ /** Clean user tail argv after removing command chain/control/preset directives */
267
+ argv: string[];
268
+ /** Raw env snapshot from run/parse params */
269
+ envs: Record<string, string | undefined>;
270
+ };
271
+ }
272
+ /** Built-in run controls */
273
+ interface ICommandControls {
274
+ help: boolean;
275
+ version: boolean;
276
+ }
277
+ /** Built-in option resolution result (internal) */
278
+ interface ICommandBuiltinOptionResolved {
279
+ version: boolean;
280
+ color: boolean;
281
+ logLevel: boolean;
282
+ silent: boolean;
283
+ logDate: boolean;
284
+ logColorful: boolean;
285
+ }
286
+ /** Built-in config resolution result (internal) */
287
+ interface ICommandBuiltinResolved {
288
+ option: ICommandBuiltinOptionResolved;
289
+ }
290
+ /** Subcommand registry entry (internal) */
291
+ interface ISubcommandEntry<TCommand = ICommand> {
292
+ name: string;
293
+ aliases: string[];
294
+ command: TCommand;
295
+ }
296
+ /** Help option line (internal) */
297
+ interface IHelpOptionLine {
298
+ sig: string;
299
+ desc: string;
300
+ }
301
+ /** Help command line (internal) */
302
+ interface IHelpCommandLine {
303
+ name: string;
304
+ desc: string;
305
+ }
306
+ /** Help example line (internal) */
307
+ interface IHelpExampleLine {
308
+ title: string;
309
+ usage: string;
310
+ desc: string;
311
+ }
312
+ /** Structured help data for rendering (internal) */
313
+ interface IHelpData {
314
+ desc: string;
315
+ usage: string;
316
+ options: IHelpOptionLine[];
317
+ commands: IHelpCommandLine[];
318
+ examples: IHelpExampleLine[];
319
+ }
226
320
  /** Error kinds for command parsing */
227
321
  type ICommanderErrorKind = 'InvalidOptionFormat' | 'InvalidNegativeOption' | 'NegativeOptionWithValue' | 'NegativeOptionType' | 'UnknownOption' | 'UnknownSubcommand' | 'UnexpectedArgument' | 'MissingValue' | 'InvalidType' | 'UnsupportedShortSyntax' | 'OptionConflict' | 'MissingRequired' | 'InvalidChoice' | 'InvalidBooleanValue' | 'MissingRequiredArgument' | 'TooManyArguments' | 'ConfigurationError';
228
322
  /** Commander error with structured information */
@@ -275,13 +369,13 @@ interface ICompletionCommandConfig {
275
369
  /** Program name for completion scripts (defaults to root.name) */
276
370
  programName?: string;
277
371
  /** Default completion file paths for each shell */
278
- paths: ICompletionPaths;
372
+ paths?: Partial<ICompletionPaths>;
279
373
  }
280
374
 
281
375
  /**
282
376
  * Command class - CLI command builder with fluent API
283
377
  *
284
- * Execution flow: route → tokenize → resolve → parse → run
378
+ * Execution flow: route → control-scan → run-control(run) → preset → tokenize → resolve → parse → run
285
379
  *
286
380
  * @module @guanghechen/commander
287
381
  */
@@ -292,6 +386,8 @@ declare class Command implements ICommand {
292
386
  get name(): string | undefined;
293
387
  get description(): string;
294
388
  get version(): string | undefined;
389
+ get builtin(): ICommandConfig['builtin'] | undefined;
390
+ get preset(): ICommandPresetConfig | undefined;
295
391
  get parent(): Command | undefined;
296
392
  get options(): ICommandOptionConfig[];
297
393
  get arguments(): ICommandArgumentConfig[];
@@ -303,7 +399,7 @@ declare class Command implements ICommand {
303
399
  example(title: string, usage: string, desc: string): this;
304
400
  subcommand(name: string, cmd: Command): this;
305
401
  run(params: ICommandRunParams): Promise<void>;
306
- parse(params: ICommandRunParams): ICommandParseResult;
402
+ parse(params: ICommandRunParams): Promise<ICommandParseResult>;
307
403
  formatHelp(): string;
308
404
  getCompletionMeta(): ICompletionMeta;
309
405
  }
@@ -316,12 +412,22 @@ declare class Command implements ICommand {
316
412
  declare class Coerce {
317
413
  private constructor();
318
414
  private static create;
319
- static number(name: string, errorMessage?: string): (rawValue: string) => number;
415
+ static choice<TValue extends string>(name: string, values: ReadonlyArray<TValue>, errorMessage?: string): (rawValue: string) => TValue;
416
+ static domain(name: string, errorMessage?: string): (rawValue: string) => string;
417
+ static host(name: string, errorMessage?: string): (rawValue: string) => string;
320
418
  static integer(name: string, errorMessage?: string): (rawValue: string) => number;
419
+ static ip(name: string, errorMessage?: string): (rawValue: string) => string;
420
+ static number(name: string, errorMessage?: string): (rawValue: string) => number;
421
+ static port(name: string, errorMessage?: string): (rawValue: string) => number;
321
422
  static positiveInteger(name: string, errorMessage?: string): (rawValue: string) => number;
322
423
  static positiveNumber(name: string, errorMessage?: string): (rawValue: string) => number;
323
424
  }
324
425
 
426
+ declare function isIpv4(rawValue: string): boolean;
427
+ declare function isIpv6(rawValue: string): boolean;
428
+ declare function isIp(rawValue: string): boolean;
429
+ declare function isDomain(rawValue: string): boolean;
430
+
325
431
  /**
326
432
  * Shell completion generators
327
433
  *
@@ -349,7 +455,7 @@ declare class Coerce {
349
455
  * ```
350
456
  */
351
457
  declare class CompletionCommand extends Command {
352
- constructor(root: Command, config: ICompletionCommandConfig);
458
+ constructor(root: Command, config?: ICompletionCommandConfig);
353
459
  }
354
460
  declare class BashCompletion {
355
461
  #private;
@@ -450,5 +556,5 @@ declare const logColorfulOption: ICommandOptionConfig<boolean>;
450
556
  */
451
557
  declare const silentOption: ICommandOptionConfig<boolean>;
452
558
 
453
- export { BashCompletion, Coerce, Command, CommanderError, CompletionCommand, FishCompletion, PwshCompletion, logColorfulOption, logDateOption, logLevelOption, silentOption };
454
- export type { ICommand, ICommandAction, ICommandActionParams, ICommandArgumentConfig, ICommandArgumentKind, ICommandArgumentType, ICommandBuiltinCommandConfig, ICommandBuiltinConfig, ICommandBuiltinOptionConfig, ICommandConfig, ICommandContext, ICommandExample, ICommandOptionArgs, ICommandOptionConfig, ICommandOptionType, ICommandParseResult, ICommandParsedArgs, ICommandParsedOpts, ICommandResolveResult, ICommandRouteResult, ICommandRunParams, ICommandShiftResult, ICommandToken, ICommandTokenType, ICommandTokenizeResult, ICommanderErrorKind, ICompletionCommandConfig, ICompletionMeta, ICompletionOptionMeta, ICompletionPaths, ICompletionShellType };
559
+ export { BashCompletion, Coerce, Command, CommanderError, CompletionCommand, FishCompletion, PwshCompletion, isDomain, isIp, isIpv4, isIpv6, logColorfulOption, logDateOption, logLevelOption, silentOption };
560
+ 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, 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.5.0",
3
+ "version": "4.6.0",
4
4
  "description": "A minimal, type-safe command-line interface builder with fluent API",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -41,6 +41,7 @@
41
41
  "README.md"
42
42
  ],
43
43
  "dependencies": {
44
+ "@guanghechen/env": "^2.0.2",
44
45
  "@guanghechen/reporter": "^3.3.0"
45
46
  },
46
47
  "scripts": {