@guanghechen/commander 4.7.7 → 4.7.9

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,13 +1,32 @@
1
1
  import { IReporter } from '@guanghechen/reporter';
2
2
 
3
- /**
4
- * Type definitions for @guanghechen/commander
5
- *
6
- * @module @guanghechen/commander
7
- */
8
-
9
3
  /** Token type: long option, short option, or positional */
10
4
  type ICommandTokenType = 'long' | 'short' | 'none';
5
+ /** Token source kind */
6
+ type ICommandTokenSource = 'user' | 'preset';
7
+ /** Preset metadata carried by token/issue source tracing */
8
+ interface ICommandPresetIssueMeta {
9
+ file?: string;
10
+ profile?: string;
11
+ variant?: string;
12
+ optionKey?: string;
13
+ }
14
+ /** Preset source snapshot metadata carried by ctx.sources.preset.meta */
15
+ interface ICommandPresetSourceMeta {
16
+ applied: boolean;
17
+ file?: string;
18
+ profile?: string;
19
+ variant?: string;
20
+ resolvedEnvFile?: string;
21
+ }
22
+ /** Preset execution state in input source snapshot */
23
+ type ICommandPresetSourceState = 'skipped' | 'none' | 'applied';
24
+ /** Input segment before tokenize, preserving source attribution */
25
+ interface ICommandArgvSegment {
26
+ value: string;
27
+ source: ICommandTokenSource;
28
+ preset?: ICommandPresetIssueMeta;
29
+ }
11
30
  /**
12
31
  * Command token after preprocessing.
13
32
  *
@@ -25,46 +44,12 @@ interface ICommandToken {
25
44
  name: string;
26
45
  /** Token type */
27
46
  type: ICommandTokenType;
47
+ /** Source of this token */
48
+ source: ICommandTokenSource;
49
+ /** Preset metadata when source='preset' */
50
+ preset?: ICommandPresetIssueMeta;
28
51
  }
29
- /** Option value type */
30
- type ICommandOptionType = 'boolean' | 'number' | 'string';
31
- /** Option argument mode */
32
- type ICommandOptionArgs = 'none' | 'required' | 'optional' | '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
- * - string + optional → string | undefined
40
- * - number + required → number
41
- * - string + variadic → string[]
42
- * - number + variadic → number[]
43
- *
44
- * @template T - The type of the option value
45
- */
46
- interface ICommandOptionConfig<T = unknown> {
47
- /** Long option name (camelCase, required) */
48
- long: string;
49
- /** Short option (single character) */
50
- short?: string;
51
- /** Value type (required) */
52
- type: ICommandOptionType;
53
- /** Argument mode (required) */
54
- args: ICommandOptionArgs;
55
- /** Description for help text */
56
- desc: string;
57
- /** Whether this option is required (mutually exclusive with default) */
58
- required?: boolean;
59
- /** Default value when not provided */
60
- default?: T;
61
- /** Allowed values for validation and completion */
62
- choices?: T extends Array<infer U> ? U[] : T[];
63
- /** Single value transformation (called for each value, before choices validation) */
64
- coerce?: (rawValue: string) => T extends Array<infer U> ? U : T;
65
- /** Callback after parsing, applies value to context */
66
- apply?: (value: T, ctx: ICommandContext) => void;
67
- }
52
+
68
53
  /** Argument kind */
69
54
  type ICommandArgumentKind = 'required' | 'optional' | 'variadic' | 'some';
70
55
  /** Argument value type */
@@ -95,11 +80,14 @@ interface ICommandArgumentConfig<T = unknown> {
95
80
  /** Custom value transformation (takes precedence over type conversion) */
96
81
  coerce?: (rawValue: string) => T;
97
82
  }
83
+
98
84
  interface ICommandBuiltinOptionConfig {
99
85
  /** Enable built-in --version option (requires configured version on target command) */
100
86
  version?: boolean;
101
87
  /** Enable built-in --color/--no-color option for help rendering (defaults respect NO_COLOR) */
102
88
  color?: boolean;
89
+ /** Enable built-in --devmode option */
90
+ devmode?: boolean;
103
91
  /** Enable built-in --log-level option */
104
92
  logLevel?: boolean;
105
93
  /** Enable built-in --silent option */
@@ -214,6 +202,7 @@ interface ICommand {
214
202
  readonly examples: ICommandExample[];
215
203
  readonly subcommands: Map<string, ICommand>;
216
204
  }
205
+
217
206
  /** Execution context */
218
207
  interface ICommandContext {
219
208
  /** Current command node */
@@ -233,6 +222,8 @@ interface ICommandContext {
233
222
  interface ICommandActionParams {
234
223
  /** Execution context */
235
224
  ctx: ICommandContext;
225
+ /** Effective built-in options for current leaf command */
226
+ builtin: ICommandBuiltinParsedOptions;
236
227
  /** Parsed options (keyed by long name) */
237
228
  opts: ICommandParsedOpts;
238
229
  /** Parsed positional arguments (keyed by argument name) */
@@ -255,6 +246,92 @@ interface ICommandRunParams {
255
246
  type ICommandParsedOpts = Record<string, unknown>;
256
247
  /** Parsed arguments record */
257
248
  type ICommandParsedArgs = Record<string, unknown>;
249
+ /** Input source snapshots for debugging/tracing */
250
+ interface ICommandInputSources {
251
+ preset: {
252
+ state: ICommandPresetSourceState;
253
+ argv: string[];
254
+ envs: Record<string, string>;
255
+ meta?: ICommandPresetSourceMeta;
256
+ };
257
+ user: {
258
+ /** Routed command tokens (name/alias as entered by user) */
259
+ cmds: string[];
260
+ /** Clean user tail argv after removing command chain/control/preset directives */
261
+ argv: string[];
262
+ /** Raw env snapshot from run/parse params */
263
+ envs: Record<string, string | undefined>;
264
+ };
265
+ }
266
+ /** Built-in run controls */
267
+ interface ICommandControls {
268
+ help: boolean;
269
+ version: boolean;
270
+ }
271
+ /** Built-in option resolution result (internal) */
272
+ interface ICommandBuiltinOptionResolved {
273
+ version: boolean;
274
+ color: boolean;
275
+ devmode: boolean;
276
+ logLevel: boolean;
277
+ silent: boolean;
278
+ logDate: boolean;
279
+ logColorful: boolean;
280
+ }
281
+ /** Built-in config resolution result (internal) */
282
+ interface ICommandBuiltinResolved {
283
+ option: ICommandBuiltinOptionResolved;
284
+ }
285
+ /** Effective built-in options exposed to action/parse result */
286
+ interface ICommandBuiltinParsedOptions {
287
+ devmode: boolean;
288
+ color?: boolean;
289
+ logLevel?: string;
290
+ silent?: boolean;
291
+ logDate?: boolean;
292
+ logColorful?: boolean;
293
+ }
294
+
295
+ /** Option value type */
296
+ type ICommandOptionType = 'boolean' | 'number' | 'string';
297
+ /** Option argument mode */
298
+ type ICommandOptionArgs = 'none' | 'required' | 'optional' | 'variadic';
299
+ /**
300
+ * Option configuration.
301
+ *
302
+ * `type` and `args` must be specified together. Valid combinations:
303
+ * - boolean + none -> boolean
304
+ * - string + required -> string
305
+ * - string + optional -> string | undefined
306
+ * - number + required -> number
307
+ * - string + variadic -> string[]
308
+ * - number + variadic -> number[]
309
+ *
310
+ * @template T - The type of the option value
311
+ */
312
+ interface ICommandOptionConfig<T = unknown> {
313
+ /** Long option name (camelCase, required) */
314
+ long: string;
315
+ /** Short option (single character) */
316
+ short?: string;
317
+ /** Value type (required) */
318
+ type: ICommandOptionType;
319
+ /** Argument mode (required) */
320
+ args: ICommandOptionArgs;
321
+ /** Description for help text */
322
+ desc: string;
323
+ /** Whether this option is required (mutually exclusive with default) */
324
+ required?: boolean;
325
+ /** Default value when not provided */
326
+ default?: T;
327
+ /** Allowed values for validation and completion */
328
+ choices?: T extends Array<infer U> ? U[] : T[];
329
+ /** Single value transformation (called for each value, before choices validation) */
330
+ coerce?: (rawValue: string) => T extends Array<infer U> ? U : T;
331
+ /** Callback after parsing, applies value to context */
332
+ apply?: (value: T, ctx: ICommandContext) => void;
333
+ }
334
+
258
335
  /** Route stage result */
259
336
  interface ICommandRouteResult<TCommand = ICommand> {
260
337
  /** Command chain from root to leaf */
@@ -279,6 +356,8 @@ interface ICommandPresetResult {
279
356
  tailArgv: string[];
280
357
  /** Effective envs after preset merge */
281
358
  envs: Record<string, string | undefined>;
359
+ /** Source-attributed argv segments consumed by tokenize */
360
+ segments: ICommandArgvSegment[];
282
361
  }
283
362
  /** Tokenize stage result */
284
363
  interface ICommandTokenizeResult {
@@ -301,50 +380,6 @@ interface ICommandShiftResult {
301
380
  /** Remaining tokens to pass to parent */
302
381
  remaining: ICommandToken[];
303
382
  }
304
- /** Parse stage result */
305
- interface ICommandParseResult {
306
- /** Execution context */
307
- ctx: ICommandContext;
308
- /** Parsed options */
309
- opts: ICommandParsedOpts;
310
- /** Parsed arguments */
311
- args: ICommandParsedArgs;
312
- /** Raw argument strings */
313
- rawArgs: string[];
314
- }
315
- /** Input source snapshots for debugging/tracing */
316
- interface ICommandInputSources {
317
- preset: {
318
- argv: string[];
319
- envs: Record<string, string>;
320
- };
321
- user: {
322
- /** Routed command tokens (name/alias as entered by user) */
323
- cmds: string[];
324
- /** Clean user tail argv after removing command chain/control/preset directives */
325
- argv: string[];
326
- /** Raw env snapshot from run/parse params */
327
- envs: Record<string, string | undefined>;
328
- };
329
- }
330
- /** Built-in run controls */
331
- interface ICommandControls {
332
- help: boolean;
333
- version: boolean;
334
- }
335
- /** Built-in option resolution result (internal) */
336
- interface ICommandBuiltinOptionResolved {
337
- version: boolean;
338
- color: boolean;
339
- logLevel: boolean;
340
- silent: boolean;
341
- logDate: boolean;
342
- logColorful: boolean;
343
- }
344
- /** Built-in config resolution result (internal) */
345
- interface ICommandBuiltinResolved {
346
- option: ICommandBuiltinOptionResolved;
347
- }
348
383
  /** Subcommand registry entry (internal) */
349
384
  interface ISubcommandEntry<TCommand = ICommand> {
350
385
  name: string;
@@ -381,16 +416,77 @@ interface IHelpData {
381
416
  commands: IHelpCommandLine[];
382
417
  examples: IHelpExampleLine[];
383
418
  }
419
+ /** Parse stage result */
420
+ interface ICommandParseResult {
421
+ /** Execution context */
422
+ ctx: ICommandContext;
423
+ /** Effective built-in options for current leaf command */
424
+ builtin: ICommandBuiltinParsedOptions;
425
+ /** Parsed options */
426
+ opts: ICommandParsedOpts;
427
+ /** Parsed arguments */
428
+ args: ICommandParsedArgs;
429
+ /** Raw argument strings */
430
+ rawArgs: string[];
431
+ }
432
+
433
+ type ICommandStage = 'route' | 'control-scan' | 'control-run' | 'preset' | 'tokenize' | 'builtin-resolve' | 'resolve' | 'parse' | 'run';
434
+ type ICommandIssueKind = 'error' | 'hint';
435
+ interface ICommandIssueSourceAttribution {
436
+ primary?: ICommandTokenSource;
437
+ related?: ICommandTokenSource[];
438
+ }
439
+ type ICommandIssueScope = 'control' | 'preset' | 'option' | 'argument' | 'command' | 'runtime' | 'action';
440
+ type ICommandErrorIssueCode = 'invalid_option_format' | 'invalid_negative_option' | 'negative_option_with_value' | 'negative_option_type' | 'unknown_option' | 'unknown_subcommand' | 'unexpected_argument' | 'missing_value' | 'invalid_type' | 'unsupported_short_syntax' | 'option_conflict' | 'missing_required' | 'invalid_choice' | 'invalid_boolean_value' | 'missing_required_argument' | 'too_many_arguments' | 'configuration_error' | 'action_failed';
441
+ type ICommandHintIssueCode = 'preset_token_injected' | 'mixed_source_conflict' | 'did_you_mean_subcommand' | 'command_does_not_accept_positional_arguments';
442
+ type ICommandIssueCode = ICommandErrorIssueCode | ICommandHintIssueCode;
443
+ interface ICommandIssueReason {
444
+ code: ICommandIssueCode;
445
+ message: string;
446
+ details?: Record<string, unknown>;
447
+ }
448
+ interface ICommandIssueBase {
449
+ kind: ICommandIssueKind;
450
+ stage: ICommandStage;
451
+ originStage?: ICommandStage;
452
+ source?: ICommandIssueSourceAttribution;
453
+ scope: ICommandIssueScope;
454
+ preset?: ICommandPresetIssueMeta;
455
+ }
456
+ interface ICommandErrorIssue extends ICommandIssueBase {
457
+ kind: 'error';
458
+ reason: Omit<ICommandIssueReason, 'code'> & {
459
+ code: ICommandErrorIssueCode;
460
+ };
461
+ }
462
+ interface ICommandHintIssue extends ICommandIssueBase {
463
+ kind: 'hint';
464
+ reason: Omit<ICommandIssueReason, 'code'> & {
465
+ code: ICommandHintIssueCode;
466
+ };
467
+ }
468
+ type ICommandIssue = ICommandErrorIssue | ICommandHintIssue;
469
+ interface ICommandErrorMeta {
470
+ commandPath: string;
471
+ token?: string;
472
+ option?: string;
473
+ argument?: string;
474
+ issues: ICommandIssue[];
475
+ }
384
476
  /** Error kinds for command parsing */
385
- type ICommanderErrorKind = 'InvalidOptionFormat' | 'InvalidNegativeOption' | 'NegativeOptionWithValue' | 'NegativeOptionType' | 'UnknownOption' | 'UnknownSubcommand' | 'UnexpectedArgument' | 'MissingValue' | 'InvalidType' | 'UnsupportedShortSyntax' | 'OptionConflict' | 'MissingRequired' | 'InvalidChoice' | 'InvalidBooleanValue' | 'MissingRequiredArgument' | 'TooManyArguments' | 'ConfigurationError';
477
+ type ICommanderErrorKind = 'InvalidOptionFormat' | 'InvalidNegativeOption' | 'NegativeOptionWithValue' | 'NegativeOptionType' | 'UnknownOption' | 'UnknownSubcommand' | 'UnexpectedArgument' | 'MissingValue' | 'InvalidType' | 'UnsupportedShortSyntax' | 'OptionConflict' | 'MissingRequired' | 'InvalidChoice' | 'InvalidBooleanValue' | 'MissingRequiredArgument' | 'TooManyArguments' | 'ConfigurationError' | 'ActionFailed';
386
478
  /** Commander error with structured information */
387
479
  declare class CommanderError extends Error {
388
480
  readonly kind: ICommanderErrorKind;
389
481
  readonly commandPath: string;
390
- constructor(kind: ICommanderErrorKind, message: string, commandPath: string);
482
+ readonly meta: ICommandErrorMeta | undefined;
483
+ constructor(kind: ICommanderErrorKind, message: string, commandPath: string, meta?: ICommandErrorMeta);
484
+ withIssue(issue: ICommandIssue): CommanderError;
485
+ withIssues(issues: ICommandIssue[]): CommanderError;
391
486
  /** Format error with help hint */
392
487
  format(): string;
393
488
  }
489
+
394
490
  /** Shell type for completion */
395
491
  type ICompletionShellType = 'bash' | 'fish' | 'pwsh';
396
492
  /** Option metadata for completion */
@@ -462,7 +558,7 @@ declare function createNodeCommandRuntime(): ICommandRuntime;
462
558
  /**
463
559
  * Command class - CLI command builder with fluent API
464
560
  *
465
- * Execution flow: route → control-scan → run-control(run) → preset → tokenize → resolve → parse → run
561
+ * Execution flow: route → control-scan → control-run(run) → preset → tokenize → builtin-resolve → resolve → parse → run
466
562
  *
467
563
  * @module @guanghechen/commander
468
564
  */
@@ -521,36 +617,6 @@ declare function isDomain(rawValue: string): boolean;
521
617
  * @module @guanghechen/commander/options
522
618
  */
523
619
 
524
- /**
525
- * Pre-defined --log-level option for setting log verbosity.
526
- *
527
- * Supports: debug | info | hint | warn | error (case-insensitive)
528
- *
529
- * | Property | Value |
530
- * | --------- | ---------------------------------- |
531
- * | long | 'logLevel' |
532
- * | type | 'string' |
533
- * | args | 'required' |
534
- * | default | 'info' |
535
- * | choices | LOG_LEVELS |
536
- * | coerce | resolveLogLevel (case-insensitive) |
537
- * | apply | ctx.reporter.setLevel(value) |
538
- *
539
- * @example
540
- * ```typescript
541
- * import { logLevelOption } from '@guanghechen/commander'
542
- *
543
- * const cmd = new Command('app')
544
- * .option(logLevelOption)
545
- * .action(({ opts }) => {
546
- * console.log(opts.logLevel) // 'debug' | 'info' | 'hint' | 'warn' | 'error'
547
- * })
548
- *
549
- * // Override with spread syntax
550
- * .option({ ...logLevelOption, default: 'warn' })
551
- * ```
552
- */
553
- declare const logLevelOption: ICommandOptionConfig<string>;
554
620
  /**
555
621
  * Pre-defined --log-date option for controlling timestamp output.
556
622
  *
@@ -585,7 +651,7 @@ declare const logColorfulOption: ICommandOptionConfig<boolean>;
585
651
  *
586
652
  * @example
587
653
  * ```typescript
588
- * import { silentOption } from '@guanghechen/commander'
654
+ * import { silentOption } from '@guanghechen/commander/browser'
589
655
  *
590
656
  * const cmd = new Command('app')
591
657
  * .option(silentOption)
@@ -660,5 +726,5 @@ declare class PwshCompletion {
660
726
  generate(): string;
661
727
  }
662
728
 
663
- export { BashCompletion, Coerce, Command, CommanderError, CompletionCommand, FishCompletion, PwshCompletion, createBrowserCommandRuntime, createNodeCommandRuntime, getDefaultCommandRuntime, isDomain, isIp, isIpv4, isIpv6, logColorfulOption, logDateOption, logLevelOption, setDefaultCommandRuntime, silentOption };
664
- 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, ICommandPresetProfileDefaults, ICommandPresetProfileItem, ICommandPresetProfileManifest, ICommandPresetProfileOptionValue, ICommandPresetProfileVariantItem, ICommandPresetResult, ICommandResolveResult, ICommandRouteResult, ICommandRunParams, ICommandRuntime, ICommandRuntimeStats, ICommandShiftResult, ICommandToken, ICommandTokenType, ICommandTokenizeResult, ICommanderErrorKind, ICompletionArgumentMeta, ICompletionCommandConfig, ICompletionMeta, ICompletionOptionMeta, ICompletionPaths, ICompletionShellType, IHelpArgumentLine, IHelpCommandLine, IHelpData, IHelpExampleLine, IHelpOptionLine, ISubcommandEntry };
729
+ export { BashCompletion, Coerce, Command, CommanderError, CompletionCommand, FishCompletion, PwshCompletion, createBrowserCommandRuntime, createNodeCommandRuntime, getDefaultCommandRuntime, isDomain, isIp, isIpv4, isIpv6, logColorfulOption, logDateOption, setDefaultCommandRuntime, silentOption };
730
+ export type { ICommand, ICommandAction, ICommandActionParams, ICommandArgumentConfig, ICommandArgumentKind, ICommandArgumentType, ICommandArgvSegment, ICommandBuiltinConfig, ICommandBuiltinOptionConfig, ICommandBuiltinOptionResolved, ICommandBuiltinParsedOptions, ICommandBuiltinResolved, ICommandConfig, ICommandContext, ICommandControlScanResult, ICommandControls, ICommandErrorIssue, ICommandErrorIssueCode, ICommandErrorMeta, ICommandExample, ICommandHintIssue, ICommandHintIssueCode, ICommandInputSources, ICommandIssue, ICommandIssueBase, ICommandIssueCode, ICommandIssueKind, ICommandIssueReason, ICommandIssueScope, ICommandIssueSourceAttribution, ICommandOptionArgs, ICommandOptionConfig, ICommandOptionType, ICommandParseResult, ICommandParsedArgs, ICommandParsedOpts, ICommandPresetConfig, ICommandPresetIssueMeta, ICommandPresetProfileDefaults, ICommandPresetProfileItem, ICommandPresetProfileManifest, ICommandPresetProfileOptionValue, ICommandPresetProfileVariantItem, ICommandPresetResult, ICommandPresetSourceMeta, ICommandPresetSourceState, ICommandResolveResult, ICommandRouteResult, ICommandRunParams, ICommandRuntime, ICommandRuntimeStats, ICommandShiftResult, ICommandStage, ICommandToken, ICommandTokenSource, ICommandTokenType, ICommandTokenizeResult, ICommanderErrorKind, ICompletionArgumentMeta, ICompletionCommandConfig, ICompletionMeta, ICompletionOptionMeta, ICompletionPaths, ICompletionShellType, IHelpArgumentLine, IHelpCommandLine, IHelpData, IHelpExampleLine, IHelpOptionLine, ISubcommandEntry };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guanghechen/commander",
3
- "version": "4.7.7",
3
+ "version": "4.7.9",
4
4
  "description": "A minimal, type-safe command-line interface builder with fluent API",
5
5
  "author": {
6
6
  "name": "guanghechen",