cli-forge 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/ban-types */
1
2
  import {
2
3
  type ConfigurationFiles,
3
4
  OptionConfig,
@@ -10,12 +11,59 @@ import {
10
11
  BooleanOptionConfig,
11
12
  ArrayOptionConfig,
12
13
  ResolveProperties,
13
- AdditionalPropertiesType,
14
14
  WithOptional,
15
+ MakeUndefinedPropertiesOptional,
16
+ WithAdditionalProperties,
15
17
  } from '@cli-forge/parser';
16
18
 
17
19
  import { InternalCLI } from './internal-cli';
18
20
 
21
+ /**
22
+ * Extracts the command name from a Command type.
23
+ * Works with both CLI instances and command config objects.
24
+ */
25
+ export type ExtractCommandName<T> = T extends CLI<any, any, any>
26
+ ? T extends InternalCLI<any, any, any>
27
+ ? string
28
+ : string
29
+ : T extends { name: infer N }
30
+ ? N extends string
31
+ ? N
32
+ : string
33
+ : string;
34
+
35
+ /**
36
+ * Extracts the args type from a Command.
37
+ * Works with both CLI instances and command config objects.
38
+ */
39
+ export type ExtractCommandArgs<T> = T extends CLI<infer A, any, any>
40
+ ? A
41
+ : T extends CLICommandOptions<any, infer A, any, any>
42
+ ? A
43
+ : ParsedArgs;
44
+
45
+ /**
46
+ * Extracts the handler return type from a Command.
47
+ */
48
+ export type ExtractCommandHandlerReturn<T> = T extends CLI<any, infer R, any>
49
+ ? R
50
+ : T extends CLICommandOptions<any, any, infer R, any>
51
+ ? R
52
+ : void;
53
+
54
+ /**
55
+ * Converts a Command to its child CLI entry for TChildren tracking.
56
+ * TParentCLI is the parent CLI type that will be set as the child's TParent.
57
+ */
58
+ export type CommandToChildEntry<T, TParentCLI = undefined> = {
59
+ [K in ExtractCommandName<T>]: CLI<
60
+ ExtractCommandArgs<T>,
61
+ ExtractCommandHandlerReturn<T>,
62
+ {},
63
+ TParentCLI
64
+ >;
65
+ };
66
+
19
67
  /**
20
68
  * The interface for a CLI application or subcommands.
21
69
  *
@@ -35,10 +83,31 @@ import { InternalCLI } from './internal-cli';
35
83
  * }).forge();
36
84
  * ```
37
85
  */
38
- export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
86
+ export interface CLI<
87
+ TArgs extends ParsedArgs = ParsedArgs,
88
+ THandlerReturn = void,
89
+ // eslint-disable-next-line @typescript-eslint/ban-types
90
+ TChildren = {},
91
+ TParent = undefined
92
+ > {
39
93
  command<TCommandArgs extends TArgs>(
40
94
  cmd: Command<TArgs, TCommandArgs>
41
- ): CLI<TArgs>;
95
+ ): CLI<
96
+ TArgs,
97
+ THandlerReturn,
98
+ TChildren &
99
+ (typeof cmd extends Command<TArgs, infer TCommandArgs, infer TCmdName>
100
+ ? {
101
+ [key in TCmdName]: CLI<
102
+ TCommandArgs,
103
+ void,
104
+ {},
105
+ CLI<TArgs, THandlerReturn, TChildren, TParent>
106
+ >;
107
+ }
108
+ : {}),
109
+ TParent
110
+ >;
42
111
 
43
112
  /**
44
113
  * Registers a new command with the CLI.
@@ -46,35 +115,300 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
46
115
  * @param options Settings for the new command. See {@link CLICommandOptions}.
47
116
  * @returns Updated CLI instance with the new command registered.
48
117
  */
49
- command<TCommandArgs extends TArgs>(
50
- key: string,
51
- options: CLICommandOptions<TArgs, TCommandArgs>
52
- ): CLI<TArgs>;
118
+ command<
119
+ TCommandArgs extends TArgs,
120
+ TChildHandlerReturn,
121
+ TKey extends string,
122
+ // eslint-disable-next-line @typescript-eslint/ban-types
123
+ TChildChildren = {}
124
+ >(
125
+ key: TKey,
126
+ options: CLICommandOptions<
127
+ TArgs,
128
+ TCommandArgs,
129
+ TChildHandlerReturn,
130
+ any,
131
+ CLI<TArgs, THandlerReturn, TChildren, TParent>,
132
+ TChildChildren
133
+ >
134
+ ): CLI<
135
+ TArgs,
136
+ THandlerReturn,
137
+ TChildren & {
138
+ [key in TKey]: CLI<
139
+ TCommandArgs,
140
+ TChildHandlerReturn,
141
+ TChildChildren,
142
+ CLI<TArgs, THandlerReturn, TChildren, TParent>
143
+ >;
144
+ },
145
+ TParent
146
+ >;
53
147
 
54
148
  /**
55
149
  * Registers multiple subcommands with the CLI.
56
150
  * @param commands Several commands to register. Can be the result of a call to {@link cli} or a configuration object.
151
+ * @returns Updated CLI instance with the commands registered and their types tracked in TChildren.
57
152
  */
58
- commands(commands: Command[]): CLI<TArgs>;
59
- /**
60
- * Registers multiple subcommands with the CLI.
61
- * @param commands Several commands to register. Can be the result of a call to {@link cli} or a configuration object.
62
- */
63
- commands(...commands: Command[]): CLI<TArgs>;
153
+ // Typed overloads for 1-10 commands to preserve individual command types
154
+ // Each child command gets this CLI as its TParent
155
+ commands<C1 extends Command>(
156
+ c1: C1
157
+ ): CLI<
158
+ TArgs,
159
+ THandlerReturn,
160
+ TChildren &
161
+ CommandToChildEntry<C1, CLI<TArgs, THandlerReturn, TChildren, TParent>>,
162
+ TParent
163
+ >;
164
+ commands<C1 extends Command, C2 extends Command>(
165
+ c1: C1,
166
+ c2: C2
167
+ ): CLI<
168
+ TArgs,
169
+ THandlerReturn,
170
+ TChildren &
171
+ CommandToChildEntry<C1, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
172
+ CommandToChildEntry<C2, CLI<TArgs, THandlerReturn, TChildren, TParent>>,
173
+ TParent
174
+ >;
175
+ commands<C1 extends Command, C2 extends Command, C3 extends Command>(
176
+ c1: C1,
177
+ c2: C2,
178
+ c3: C3
179
+ ): CLI<
180
+ TArgs,
181
+ THandlerReturn,
182
+ TChildren &
183
+ CommandToChildEntry<C1, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
184
+ CommandToChildEntry<C2, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
185
+ CommandToChildEntry<C3, CLI<TArgs, THandlerReturn, TChildren, TParent>>,
186
+ TParent
187
+ >;
188
+ commands<
189
+ C1 extends Command,
190
+ C2 extends Command,
191
+ C3 extends Command,
192
+ C4 extends Command
193
+ >(
194
+ c1: C1,
195
+ c2: C2,
196
+ c3: C3,
197
+ c4: C4
198
+ ): CLI<
199
+ TArgs,
200
+ THandlerReturn,
201
+ TChildren &
202
+ CommandToChildEntry<C1, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
203
+ CommandToChildEntry<C2, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
204
+ CommandToChildEntry<C3, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
205
+ CommandToChildEntry<C4, CLI<TArgs, THandlerReturn, TChildren, TParent>>,
206
+ TParent
207
+ >;
208
+ commands<
209
+ C1 extends Command,
210
+ C2 extends Command,
211
+ C3 extends Command,
212
+ C4 extends Command,
213
+ C5 extends Command
214
+ >(
215
+ c1: C1,
216
+ c2: C2,
217
+ c3: C3,
218
+ c4: C4,
219
+ c5: C5
220
+ ): CLI<
221
+ TArgs,
222
+ THandlerReturn,
223
+ TChildren &
224
+ CommandToChildEntry<C1, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
225
+ CommandToChildEntry<C2, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
226
+ CommandToChildEntry<C3, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
227
+ CommandToChildEntry<C4, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
228
+ CommandToChildEntry<C5, CLI<TArgs, THandlerReturn, TChildren, TParent>>,
229
+ TParent
230
+ >;
231
+ commands<
232
+ C1 extends Command,
233
+ C2 extends Command,
234
+ C3 extends Command,
235
+ C4 extends Command,
236
+ C5 extends Command,
237
+ C6 extends Command
238
+ >(
239
+ c1: C1,
240
+ c2: C2,
241
+ c3: C3,
242
+ c4: C4,
243
+ c5: C5,
244
+ c6: C6
245
+ ): CLI<
246
+ TArgs,
247
+ THandlerReturn,
248
+ TChildren &
249
+ CommandToChildEntry<C1, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
250
+ CommandToChildEntry<C2, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
251
+ CommandToChildEntry<C3, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
252
+ CommandToChildEntry<C4, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
253
+ CommandToChildEntry<C5, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
254
+ CommandToChildEntry<C6, CLI<TArgs, THandlerReturn, TChildren, TParent>>,
255
+ TParent
256
+ >;
257
+ commands<
258
+ C1 extends Command,
259
+ C2 extends Command,
260
+ C3 extends Command,
261
+ C4 extends Command,
262
+ C5 extends Command,
263
+ C6 extends Command,
264
+ C7 extends Command
265
+ >(
266
+ c1: C1,
267
+ c2: C2,
268
+ c3: C3,
269
+ c4: C4,
270
+ c5: C5,
271
+ c6: C6,
272
+ c7: C7
273
+ ): CLI<
274
+ TArgs,
275
+ THandlerReturn,
276
+ TChildren &
277
+ CommandToChildEntry<C1, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
278
+ CommandToChildEntry<C2, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
279
+ CommandToChildEntry<C3, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
280
+ CommandToChildEntry<C4, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
281
+ CommandToChildEntry<C5, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
282
+ CommandToChildEntry<C6, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
283
+ CommandToChildEntry<C7, CLI<TArgs, THandlerReturn, TChildren, TParent>>,
284
+ TParent
285
+ >;
286
+ commands<
287
+ C1 extends Command,
288
+ C2 extends Command,
289
+ C3 extends Command,
290
+ C4 extends Command,
291
+ C5 extends Command,
292
+ C6 extends Command,
293
+ C7 extends Command,
294
+ C8 extends Command
295
+ >(
296
+ c1: C1,
297
+ c2: C2,
298
+ c3: C3,
299
+ c4: C4,
300
+ c5: C5,
301
+ c6: C6,
302
+ c7: C7,
303
+ c8: C8
304
+ ): CLI<
305
+ TArgs,
306
+ THandlerReturn,
307
+ TChildren &
308
+ CommandToChildEntry<C1, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
309
+ CommandToChildEntry<C2, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
310
+ CommandToChildEntry<C3, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
311
+ CommandToChildEntry<C4, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
312
+ CommandToChildEntry<C5, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
313
+ CommandToChildEntry<C6, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
314
+ CommandToChildEntry<C7, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
315
+ CommandToChildEntry<C8, CLI<TArgs, THandlerReturn, TChildren, TParent>>,
316
+ TParent
317
+ >;
318
+ commands<
319
+ C1 extends Command,
320
+ C2 extends Command,
321
+ C3 extends Command,
322
+ C4 extends Command,
323
+ C5 extends Command,
324
+ C6 extends Command,
325
+ C7 extends Command,
326
+ C8 extends Command,
327
+ C9 extends Command
328
+ >(
329
+ c1: C1,
330
+ c2: C2,
331
+ c3: C3,
332
+ c4: C4,
333
+ c5: C5,
334
+ c6: C6,
335
+ c7: C7,
336
+ c8: C8,
337
+ c9: C9
338
+ ): CLI<
339
+ TArgs,
340
+ THandlerReturn,
341
+ TChildren &
342
+ CommandToChildEntry<C1, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
343
+ CommandToChildEntry<C2, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
344
+ CommandToChildEntry<C3, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
345
+ CommandToChildEntry<C4, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
346
+ CommandToChildEntry<C5, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
347
+ CommandToChildEntry<C6, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
348
+ CommandToChildEntry<C7, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
349
+ CommandToChildEntry<C8, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
350
+ CommandToChildEntry<C9, CLI<TArgs, THandlerReturn, TChildren, TParent>>,
351
+ TParent
352
+ >;
353
+ commands<
354
+ C1 extends Command,
355
+ C2 extends Command,
356
+ C3 extends Command,
357
+ C4 extends Command,
358
+ C5 extends Command,
359
+ C6 extends Command,
360
+ C7 extends Command,
361
+ C8 extends Command,
362
+ C9 extends Command,
363
+ C10 extends Command
364
+ >(
365
+ c1: C1,
366
+ c2: C2,
367
+ c3: C3,
368
+ c4: C4,
369
+ c5: C5,
370
+ c6: C6,
371
+ c7: C7,
372
+ c8: C8,
373
+ c9: C9,
374
+ c10: C10
375
+ ): CLI<
376
+ TArgs,
377
+ THandlerReturn,
378
+ TChildren &
379
+ CommandToChildEntry<C1, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
380
+ CommandToChildEntry<C2, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
381
+ CommandToChildEntry<C3, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
382
+ CommandToChildEntry<C4, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
383
+ CommandToChildEntry<C5, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
384
+ CommandToChildEntry<C6, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
385
+ CommandToChildEntry<C7, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
386
+ CommandToChildEntry<C8, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
387
+ CommandToChildEntry<C9, CLI<TArgs, THandlerReturn, TChildren, TParent>> &
388
+ CommandToChildEntry<C10, CLI<TArgs, THandlerReturn, TChildren, TParent>>,
389
+ TParent
390
+ >;
391
+ // Fallback for arrays or more than 10 commands (loses individual type tracking)
392
+ commands(commands: Command[]): CLI<TArgs, THandlerReturn, TChildren, TParent>;
393
+ commands(
394
+ ...commands: Command[]
395
+ ): CLI<TArgs, THandlerReturn, TChildren, TParent>;
64
396
 
65
397
  /**
66
398
  * Register's a configuration provider for the CLI. See {@link ConfigurationProviders} for built-in providers.
67
399
  *
68
400
  * @param provider Provider to register.
69
401
  */
70
- config(provider: ConfigurationFiles.ConfigurationProvider<TArgs>): CLI<TArgs>;
402
+ config(
403
+ provider: ConfigurationFiles.ConfigurationProvider<TArgs>
404
+ ): CLI<TArgs, THandlerReturn, TChildren, TParent>;
71
405
 
72
406
  /**
73
407
  * Enables the ability to run CLI commands that contain subcommands as an interactive shell.
74
408
  * This presents as a small shell that only knows the current command and its subcommands.
75
409
  * Any flags already consumed by the command will be passed to every subcommand invocation.
76
410
  */
77
- enableInteractiveShell(): CLI<TArgs>;
411
+ enableInteractiveShell(): CLI<TArgs, THandlerReturn, TChildren, TParent>;
78
412
 
79
413
  /**
80
414
  * Registers a custom global error handler for the CLI. This handler will be called when an error is thrown
@@ -84,7 +418,9 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
84
418
  * @param handler Typically called with an Error object, but you should be prepared to handle any type of error.
85
419
  * @param actions Actions that can be taken by the error handler. Prefer using these over process.exit for better support of interactive shells.
86
420
  */
87
- errorHandler(handler: ErrorHandler): CLI<TArgs>;
421
+ errorHandler(
422
+ handler: ErrorHandler
423
+ ): CLI<TArgs, THandlerReturn, TChildren, TParent>;
88
424
 
89
425
  /**
90
426
  * Registers a new option for the CLI command. This option will be accessible
@@ -106,14 +442,21 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
106
442
  name: TOption,
107
443
  config: ObjectOptionConfig<TCoerce, TProps, TAdditionalProps>
108
444
  ): CLI<
109
- TArgs & {
110
- [key in TOption]: WithOptional<
111
- unknown extends TCoerce
112
- ? ResolveProperties<TProps> & AdditionalPropertiesType<TAdditionalProps>
113
- : TCoerce,
114
- ObjectOptionConfig<TCoerce, TProps, TAdditionalProps>
115
- >;
116
- }
445
+ TArgs &
446
+ MakeUndefinedPropertiesOptional<{
447
+ [key in TOption]: WithOptional<
448
+ unknown extends TCoerce
449
+ ? WithAdditionalProperties<
450
+ ResolveProperties<TProps>,
451
+ TAdditionalProps
452
+ >
453
+ : TCoerce,
454
+ ObjectOptionConfig<TCoerce, TProps, TAdditionalProps>
455
+ >;
456
+ }>,
457
+ THandlerReturn,
458
+ TChildren,
459
+ TParent
117
460
  >;
118
461
  // String option overload
119
462
  option<
@@ -123,9 +466,13 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
123
466
  name: TOption,
124
467
  config: TConfig
125
468
  ): CLI<
126
- TArgs & {
127
- [key in TOption]: OptionConfigToType<TConfig>;
128
- }
469
+ TArgs &
470
+ MakeUndefinedPropertiesOptional<{
471
+ [key in TOption]: OptionConfigToType<TConfig>;
472
+ }>,
473
+ THandlerReturn,
474
+ TChildren,
475
+ TParent
129
476
  >;
130
477
  // Number option overload
131
478
  option<
@@ -135,9 +482,13 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
135
482
  name: TOption,
136
483
  config: TConfig
137
484
  ): CLI<
138
- TArgs & {
139
- [key in TOption]: OptionConfigToType<TConfig>;
140
- }
485
+ TArgs &
486
+ MakeUndefinedPropertiesOptional<{
487
+ [key in TOption]: OptionConfigToType<TConfig>;
488
+ }>,
489
+ THandlerReturn,
490
+ TChildren,
491
+ TParent
141
492
  >;
142
493
  // Boolean option overload
143
494
  option<
@@ -147,9 +498,13 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
147
498
  name: TOption,
148
499
  config: TConfig
149
500
  ): CLI<
150
- TArgs & {
151
- [key in TOption]: OptionConfigToType<TConfig>;
152
- }
501
+ TArgs &
502
+ MakeUndefinedPropertiesOptional<{
503
+ [key in TOption]: OptionConfigToType<TConfig>;
504
+ }>,
505
+ THandlerReturn,
506
+ TChildren,
507
+ TParent
153
508
  >;
154
509
  // Array option overload
155
510
  option<
@@ -159,9 +514,13 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
159
514
  name: TOption,
160
515
  config: TConfig
161
516
  ): CLI<
162
- TArgs & {
163
- [key in TOption]: OptionConfigToType<TConfig>;
164
- }
517
+ TArgs &
518
+ MakeUndefinedPropertiesOptional<{
519
+ [key in TOption]: OptionConfigToType<TConfig>;
520
+ }>,
521
+ THandlerReturn,
522
+ TChildren,
523
+ TParent
165
524
  >;
166
525
  // Generic fallback overload
167
526
  option<
@@ -171,9 +530,13 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
171
530
  name: TOption,
172
531
  config: TOptionConfig
173
532
  ): CLI<
174
- TArgs & {
175
- [key in TOption]: OptionConfigToType<TOptionConfig>;
176
- }
533
+ TArgs &
534
+ MakeUndefinedPropertiesOptional<{
535
+ [key in TOption]: OptionConfigToType<TOptionConfig>;
536
+ }>,
537
+ THandlerReturn,
538
+ TChildren,
539
+ TParent
177
540
  >;
178
541
 
179
542
  /**
@@ -195,14 +558,21 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
195
558
  name: TOption,
196
559
  config: ObjectOptionConfig<TCoerce, TProps, TAdditionalProps>
197
560
  ): CLI<
198
- TArgs & {
199
- [key in TOption]: WithOptional<
200
- unknown extends TCoerce
201
- ? ResolveProperties<TProps> & AdditionalPropertiesType<TAdditionalProps>
202
- : TCoerce,
203
- ObjectOptionConfig<TCoerce, TProps, TAdditionalProps>
204
- >;
205
- }
561
+ TArgs &
562
+ MakeUndefinedPropertiesOptional<{
563
+ [key in TOption]: WithOptional<
564
+ unknown extends TCoerce
565
+ ? WithAdditionalProperties<
566
+ ResolveProperties<TProps>,
567
+ TAdditionalProps
568
+ >
569
+ : TCoerce,
570
+ ObjectOptionConfig<TCoerce, TProps, TAdditionalProps>
571
+ >;
572
+ }>,
573
+ THandlerReturn,
574
+ TChildren,
575
+ TParent
206
576
  >;
207
577
  // String option overload
208
578
  positional<
@@ -212,9 +582,13 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
212
582
  name: TOption,
213
583
  config: TConfig
214
584
  ): CLI<
215
- TArgs & {
216
- [key in TOption]: OptionConfigToType<TConfig>;
217
- }
585
+ TArgs &
586
+ MakeUndefinedPropertiesOptional<{
587
+ [key in TOption]: OptionConfigToType<TConfig>;
588
+ }>,
589
+ THandlerReturn,
590
+ TChildren,
591
+ TParent
218
592
  >;
219
593
  // Number option overload
220
594
  positional<
@@ -224,9 +598,13 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
224
598
  name: TOption,
225
599
  config: TConfig
226
600
  ): CLI<
227
- TArgs & {
228
- [key in TOption]: OptionConfigToType<TConfig>;
229
- }
601
+ TArgs &
602
+ MakeUndefinedPropertiesOptional<{
603
+ [key in TOption]: OptionConfigToType<TConfig>;
604
+ }>,
605
+ THandlerReturn,
606
+ TChildren,
607
+ TParent
230
608
  >;
231
609
  // Boolean option overload
232
610
  positional<
@@ -236,9 +614,13 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
236
614
  name: TOption,
237
615
  config: TConfig
238
616
  ): CLI<
239
- TArgs & {
240
- [key in TOption]: OptionConfigToType<TConfig>;
241
- }
617
+ TArgs &
618
+ MakeUndefinedPropertiesOptional<{
619
+ [key in TOption]: OptionConfigToType<TConfig>;
620
+ }>,
621
+ THandlerReturn,
622
+ TChildren,
623
+ TParent
242
624
  >;
243
625
  // Array option overload
244
626
  positional<
@@ -248,9 +630,13 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
248
630
  name: TOption,
249
631
  config: TConfig
250
632
  ): CLI<
251
- TArgs & {
252
- [key in TOption]: OptionConfigToType<TConfig>;
253
- }
633
+ TArgs &
634
+ MakeUndefinedPropertiesOptional<{
635
+ [key in TOption]: OptionConfigToType<TConfig>;
636
+ }>,
637
+ THandlerReturn,
638
+ TChildren,
639
+ TParent
254
640
  >;
255
641
  // Generic fallback overload
256
642
  positional<
@@ -260,57 +646,68 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
260
646
  name: TOption,
261
647
  config: TOptionConfig
262
648
  ): CLI<
263
- TArgs & {
264
- [key in TOption]: OptionConfigToType<TOptionConfig>;
265
- }
649
+ TArgs &
650
+ MakeUndefinedPropertiesOptional<{
651
+ [key in TOption]: OptionConfigToType<TOptionConfig>;
652
+ }>,
653
+ THandlerReturn,
654
+ TChildren,
655
+ TParent
266
656
  >;
267
657
 
268
658
  /**
269
659
  * Adds support for reading CLI options from environment variables.
270
660
  * @param prefix The prefix to use when looking up environment variables. Defaults to the command name.
271
661
  */
272
- env(prefix?: string): CLI<TArgs>;
662
+ env(prefix?: string): CLI<TArgs, THandlerReturn, TChildren, TParent>;
273
663
 
274
- env(options: EnvOptionConfig): CLI<TArgs>;
664
+ env(options: EnvOptionConfig): CLI<TArgs, THandlerReturn, TChildren, TParent>;
275
665
 
276
666
  /**
277
667
  * Sets a group of options as mutually exclusive. If more than one option is provided, there will be a validation error.
278
668
  * @param options The options that should be mutually exclusive.
279
669
  */
280
- conflicts(...options: [string, string, ...string[]]): CLI<TArgs>;
670
+ conflicts(
671
+ ...options: [string, string, ...string[]]
672
+ ): CLI<TArgs, THandlerReturn, TChildren, TParent>;
281
673
 
282
674
  /**
283
675
  * Sets a group of options as mutually inclusive. If one option is provided, all other options must also be provided.
284
676
  * @param option The option that implies the other options.
285
677
  * @param impliedOptions The options which become required when the option is provided.
286
678
  */
287
- implies(option: string, ...impliedOptions: string[]): CLI<TArgs>;
679
+ implies(
680
+ option: string,
681
+ ...impliedOptions: string[]
682
+ ): CLI<TArgs, THandlerReturn, TChildren, TParent>;
288
683
 
289
684
  /**
290
685
  * Requires a command to be provided when executing the CLI. Useful if your parent command
291
686
  * cannot be executed on its own.
292
687
  * @returns Updated CLI instance.
293
688
  */
294
- demandCommand(): CLI<TArgs>;
689
+ demandCommand(): CLI<TArgs, THandlerReturn, TChildren, TParent>;
295
690
 
296
691
  /**
297
692
  * Sets the usage text for the CLI. This text will be displayed in place of the default usage text
298
693
  * @param usageText Text displayed in place of the default usage text for `--help` and in generated docs.
299
694
  */
300
- usage(usageText: string): CLI<TArgs>;
695
+ usage(usageText: string): CLI<TArgs, THandlerReturn, TChildren, TParent>;
301
696
 
302
697
  /**
303
698
  * Sets the description for the CLI. This text will be displayed in the help text and generated docs.
304
699
  * @param examples Examples to display in the help text and generated docs.
305
700
  */
306
- examples(...examples: string[]): CLI<TArgs>;
701
+ examples(
702
+ ...examples: string[]
703
+ ): CLI<TArgs, THandlerReturn, TChildren, TParent>;
307
704
 
308
705
  /**
309
706
  * Allows overriding the version displayed when passing `--version`. Defaults to crawling
310
707
  * the file system to get the package.json of the currently executing command.
311
708
  * @param override
312
709
  */
313
- version(override?: string): CLI<TArgs>;
710
+ version(override?: string): CLI<TArgs, THandlerReturn, TChildren, TParent>;
314
711
 
315
712
  /**
316
713
  * Prints help text to stdout.
@@ -325,12 +722,20 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
325
722
  label: string;
326
723
  keys: (keyof TArgs)[];
327
724
  sortOrder: number;
328
- }): CLI<TArgs>;
329
- group(label: string, keys: (keyof TArgs)[]): CLI<TArgs>;
725
+ }): CLI<TArgs, THandlerReturn, TChildren, TParent>;
726
+ group(
727
+ label: string,
728
+ keys: (keyof TArgs)[]
729
+ ): CLI<TArgs, THandlerReturn, TChildren, TParent>;
330
730
 
331
731
  middleware<TArgs2>(
332
732
  callback: MiddlewareFunction<TArgs, TArgs2>
333
- ): CLI<TArgs2 extends void ? TArgs : TArgs & TArgs2>;
733
+ ): CLI<
734
+ TArgs2 extends void ? TArgs : TArgs & TArgs2,
735
+ THandlerReturn,
736
+ TChildren,
737
+ TParent
738
+ >;
334
739
 
335
740
  /**
336
741
  * Parses argv and executes the CLI
@@ -338,12 +743,62 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
338
743
  * @returns Promise that resolves when the handler completes.
339
744
  */
340
745
  forge(args?: string[]): Promise<TArgs>;
746
+
747
+ /**
748
+ * Returns the typed children commands registered with this CLI.
749
+ * The return type is determined by the commands registered via `command()` or `commands()`.
750
+ *
751
+ * @example
752
+ * ```ts
753
+ * const app = cli('app')
754
+ * .command('init', { ... })
755
+ * .command('build', { ... });
756
+ *
757
+ * const children = app.getChildren();
758
+ * // children.init and children.build are typed CLI instances
759
+ * const initHandler = children.init.getHandler();
760
+ * ```
761
+ */
762
+ getChildren(): TChildren;
763
+
764
+ /**
765
+ * Returns the parent CLI instance, if this command was registered as a subcommand.
766
+ * Returns undefined for root-level CLI instances.
767
+ *
768
+ * @example
769
+ * ```ts
770
+ * const build = cli('build', {
771
+ * handler: (args, ctx) => {
772
+ * const parent = ctx.command.getParent();
773
+ * const siblings = parent?.getChildren();
774
+ * // Access sibling commands
775
+ * }
776
+ * });
777
+ * ```
778
+ */
779
+ getParent(): TParent;
780
+
781
+ getBuilder<T extends ParsedArgs = ParsedArgs>(
782
+ initialCli?: CLI<T, any, any>
783
+ ):
784
+ | ((parser: CLI<T, any, any>) => CLI<TArgs, THandlerReturn, TChildren>)
785
+ | undefined;
786
+ getHandler():
787
+ | ((args: Omit<TArgs, keyof ParsedArgs>) => THandlerReturn)
788
+ | undefined;
341
789
  }
342
790
 
343
- export interface CLIHandlerContext {
344
- command: CLI<any>;
791
+ export interface CLIHandlerContext<TChildren = {}, TParent = any> {
792
+ command: CLI<any, any, TChildren, TParent>;
345
793
  }
346
794
 
795
+ /**
796
+ * Extracts the TChildren type parameter from a CLI type.
797
+ */
798
+ export type ExtractCLIChildren<T> = T extends CLI<any, any, infer C, any>
799
+ ? C
800
+ : {};
801
+
347
802
  /**
348
803
  * Represents the configuration needed to create a CLI command.
349
804
  */
@@ -355,7 +810,19 @@ export interface CLICommandOptions<
355
810
  /**
356
811
  * The type of the arguments that are registered after `builder` is invoked, and the type that is passed to the handler.
357
812
  */
358
- TArgs extends TInitial = TInitial
813
+ TArgs extends TInitial = TInitial,
814
+ THandlerReturn = void,
815
+ /**
816
+ * The children commands that exist before the builder runs.
817
+ */
818
+ // eslint-disable-next-line @typescript-eslint/ban-types
819
+ TInitialChildren = {},
820
+ TParent = any,
821
+ /**
822
+ * The children commands after the builder runs (includes TInitialChildren plus any added by builder).
823
+ */
824
+ // eslint-disable-next-line @typescript-eslint/ban-types
825
+ TChildren = {}
359
826
  > {
360
827
  /**
361
828
  * If set the command will be registered under the provided name and any aliases.
@@ -373,14 +840,21 @@ export interface CLICommandOptions<
373
840
  * The command builder. This function is called before the command is executed, and is used to register options and positional parameters.
374
841
  * @param parser The parser instance to register options and positionals with.
375
842
  */
376
- builder?: (parser: CLI<TInitial>) => CLI<TArgs>;
843
+ // Note: Builder uses 'any' for THandlerReturn to avoid inference conflicts with the handler.
844
+ // The handler's return type is inferred independently from the handler function itself.
845
+ builder?: (
846
+ parser: CLI<TInitial, any, TInitialChildren, TParent>
847
+ ) => CLI<TArgs, any, TChildren, any>;
377
848
 
378
849
  /**
379
850
  * The command handler. This function is called when the command is executed.
380
851
  * @param args The parsed arguments.
381
852
  * @param context Context for the handler. Contains the command instance.
382
853
  */
383
- handler?: (args: TArgs, context: CLIHandlerContext) => void | Promise<void>;
854
+ handler?: (
855
+ args: NoInfer<TArgs>,
856
+ context: CLIHandlerContext<NoInfer<TChildren>, TParent>
857
+ ) => THandlerReturn;
384
858
 
385
859
  /**
386
860
  * The usage text for the command. This text will be displayed in place of the default usage text in the help text and generated docs.
@@ -405,10 +879,11 @@ export interface CLICommandOptions<
405
879
 
406
880
  export type Command<
407
881
  TInitial extends ParsedArgs = any,
408
- TArgs extends TInitial = TInitial
882
+ TArgs extends TInitial = TInitial,
883
+ TCommandName extends string = string
409
884
  > =
410
885
  | ({
411
- name: string;
886
+ name: TCommandName;
412
887
  } & CLICommandOptions<TInitial, TArgs>)
413
888
  | CLI<TArgs>;
414
889
 
@@ -429,6 +904,8 @@ export type ErrorHandler = (
429
904
  }
430
905
  ) => void;
431
906
 
907
+ export type UnknownCLI = CLI<ParsedArgs, any, any, any>;
908
+
432
909
  export type MiddlewareFunction<TArgs extends ParsedArgs, TArgs2> = (
433
910
  args: TArgs
434
911
  ) => TArgs2 | Promise<TArgs2>;
@@ -439,11 +916,28 @@ export type MiddlewareFunction<TArgs extends ParsedArgs, TArgs2> = (
439
916
  * @param rootCommandConfiguration Configuration used when running the bare CLI. e.g. npx my-cli, rather than npx my-cli [cmd]
440
917
  * @returns A {@link CLI} instance.
441
918
  */
442
- export function cli<TArgs extends ParsedArgs>(
443
- name: string,
444
- rootCommandConfiguration?: CLICommandOptions<ParsedArgs, TArgs>
919
+ export function cli<
920
+ TArgs extends ParsedArgs,
921
+ THandlerReturn = void,
922
+ // eslint-disable-next-line @typescript-eslint/ban-types
923
+ TChildren = {},
924
+ TName extends string = string
925
+ >(
926
+ name: TName,
927
+ rootCommandConfiguration?: CLICommandOptions<
928
+ ParsedArgs,
929
+ TArgs,
930
+ THandlerReturn,
931
+ {},
932
+ any,
933
+ TChildren
934
+ >
445
935
  ) {
446
- return new InternalCLI(name, rootCommandConfiguration) as any as CLI<TArgs>;
936
+ return new InternalCLI(name, rootCommandConfiguration as any) as any as CLI<
937
+ TArgs,
938
+ THandlerReturn,
939
+ TChildren
940
+ >;
447
941
  }
448
942
 
449
943
  export default cli;