/
command.ts
3365 lines (3036 loc) 路 88.8 KB
/
command.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// deno-lint-ignore-file no-explicit-any
import {
UnknownTypeError,
ValidationError as FlagsValidationError,
} from "../flags/_errors.ts";
import { MissingRequiredEnvVarError } from "./_errors.ts";
import { parseFlags } from "../flags/flags.ts";
import type { ParseFlagsContext } from "../flags/types.ts";
import {
getDescription,
parseArgumentsDefinition,
splitArguments,
} from "./_utils.ts";
import { blue, bold, red, yellow } from "./deps.ts";
import {
CommandExecutableNotFoundError,
CommandNotFoundError,
DefaultCommandNotFoundError,
DuplicateCommandAliasError,
DuplicateCommandNameError,
DuplicateCompletionError,
DuplicateEnvVarError,
DuplicateExampleError,
DuplicateOptionNameError,
DuplicateTypeError,
MissingArgumentError,
MissingArgumentsError,
MissingCommandNameError,
NoArgumentsAllowedError,
TooManyArgumentsError,
TooManyEnvVarValuesError,
UnexpectedOptionalEnvVarValueError,
UnexpectedVariadicEnvVarValueError,
UnknownCommandError,
ValidationError,
} from "./_errors.ts";
import { DefaultValue, OptionValueHandler } from "./types.ts";
import { BooleanType } from "./types/boolean.ts";
import { FileType } from "./types/file.ts";
import { NumberType } from "./types/number.ts";
import { StringType } from "./types/string.ts";
import { Type } from "./type.ts";
import { HelpGenerator } from "./help/_help_generator.ts";
import type { HelpOptions } from "./help/_help_generator.ts";
import type {
ActionHandler,
Argument,
ArgumentValue,
CommandResult,
CompleteHandler,
CompleteOptions,
Completion,
Description,
EnvVar,
EnvVarOptions,
EnvVarValueHandler,
Example,
GlobalEnvVarOptions,
GlobalOptionOptions,
HelpHandler,
MapTypes,
Option,
OptionOptions,
TypeDef,
TypeOptions,
TypeOrTypeHandler,
VersionHandler,
} from "./types.ts";
import { IntegerType } from "./types/integer.ts";
import { underscoreToCamelCase } from "../flags/_utils.ts";
export class Command<
TParentCommandGlobals extends Record<string, unknown> | void = void,
TParentCommandTypes extends Record<string, unknown> | void =
TParentCommandGlobals extends number ? any : void,
TCommandOptions extends Record<string, unknown> | void =
TParentCommandGlobals extends number ? any : void,
TCommandArguments extends Array<unknown> = TParentCommandGlobals extends
number ? any : [],
TCommandGlobals extends Record<string, unknown> | void =
TParentCommandGlobals extends number ? any : void,
TCommandTypes extends Record<string, unknown> | void =
TParentCommandGlobals extends number ? any : {
number: number;
integer: number;
string: string;
boolean: boolean;
file: string;
},
TCommandGlobalTypes extends Record<string, unknown> | void =
TParentCommandGlobals extends number ? any : void,
TParentCommand extends Command<any> | undefined =
TParentCommandGlobals extends number ? any : undefined,
> {
private types: Map<string, TypeDef> = new Map();
private rawArgs: Array<string> = [];
private literalArgs: Array<string> = [];
// @TODO: get script name: https://github.com/denoland/deno/pull/5034
// private name: string = location.pathname.split( '/' ).pop() as string;
private _name = "COMMAND";
private _parent?: TParentCommand;
private _globalParent?: Command<any>;
private ver?: VersionHandler;
private desc: Description = "";
private _usage?: string;
private fn?: ActionHandler;
private options: Array<Option> = [];
private commands: Map<string, Command<any>> = new Map();
private examples: Array<Example> = [];
private envVars: Array<EnvVar> = [];
private aliases: Array<string> = [];
private completions: Map<string, Completion> = new Map();
private cmd: Command<any> = this;
private argsDefinition?: string;
private isExecutable = false;
private throwOnError = false;
private _allowEmpty = false;
private _stopEarly = false;
private defaultCommand?: string;
private _useRawArgs = false;
private args: Array<Argument> = [];
private isHidden = false;
private isGlobal = false;
private hasDefaults = false;
private _versionOptions?: DefaultOption | false;
private _helpOptions?: DefaultOption | false;
private _versionOption?: Option;
private _helpOption?: Option;
private _help?: HelpHandler;
private _shouldExit?: boolean;
private _meta: Record<string, string> = {};
private _groupName?: string;
private _noGlobals = false;
/** Disable version option. */
public versionOption(enable: false): this;
/**
* Set global version option.
* @param flags The flags of the version option.
* @param desc The description of the version option.
* @param opts Version option options.
*/
public versionOption(
flags: string,
desc?: string,
opts?:
& OptionOptions<
Partial<TCommandOptions>,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>
& {
global: true;
},
): this;
/**
* Set version option.
* @param flags The flags of the version option.
* @param desc The description of the version option.
* @param opts Version option options.
*/
public versionOption(
flags: string,
desc?: string,
opts?: OptionOptions<
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>,
): this;
/**
* Set version option.
* @param flags The flags of the version option.
* @param desc The description of the version option.
* @param opts The action of the version option.
*/
public versionOption(
flags: string,
desc?: string,
opts?: ActionHandler<
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>,
): this;
public versionOption(
flags: string | false,
desc?: string,
opts?:
| ActionHandler<
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>
| OptionOptions<
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>
| OptionOptions<
Partial<TCommandOptions>,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>
& {
global: true;
},
): this {
this._versionOptions = flags === false ? flags : {
flags,
desc,
opts: typeof opts === "function" ? { action: opts } : opts,
};
return this;
}
/** Disable help option. */
public helpOption(enable: false): this;
/**
* Set global help option.
* @param flags The flags of the help option.
* @param desc The description of the help option.
* @param opts Help option options.
*/
public helpOption(
flags: string,
desc?: string,
opts?:
& OptionOptions<
Partial<TCommandOptions>,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>
& {
global: true;
},
): this;
/**
* Set help option.
* @param flags The flags of the help option.
* @param desc The description of the help option.
* @param opts Help option options.
*/
public helpOption(
flags: string,
desc?: string,
opts?: OptionOptions<
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>,
): this;
/**
* Set help option.
* @param flags The flags of the help option.
* @param desc The description of the help option.
* @param opts The action of the help option.
*/
public helpOption(
flags: string,
desc?: string,
opts?: ActionHandler<
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>,
): this;
public helpOption(
flags: string | false,
desc?: string,
opts?:
| ActionHandler<
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>
| OptionOptions<
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>
| OptionOptions<
Partial<TCommandOptions>,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>
& {
global: true;
},
): this {
this._helpOptions = flags === false ? flags : {
flags,
desc,
opts: typeof opts === "function" ? { action: opts } : opts,
};
return this;
}
/**
* Add new sub-command.
* @param name Command definition. E.g: `my-command <input-file:string> <output-file:string>`
* @param cmd The new child command to register.
* @param override Override existing child command.
*/
public command<
TCommand extends Command<
(TGlobalOptions & Record<string, unknown>) | void | undefined,
TGlobalTypes | void | undefined,
Record<string, unknown> | void,
Array<unknown>,
Record<string, unknown> | void,
Record<string, unknown> | void,
Record<string, unknown> | void,
Command<
TGlobalOptions | void | undefined,
TGlobalTypes | void | undefined,
Record<string, unknown> | void,
Array<unknown>,
Record<string, unknown> | void,
Record<string, unknown> | void,
Record<string, unknown> | void,
undefined
>
>,
TGlobalOptions
extends (TParentCommand extends Command<any> ? TParentCommandGlobals
: Merge<TParentCommandGlobals, TCommandGlobals>),
TGlobalTypes
extends (TParentCommand extends Command<any> ? TParentCommandTypes
: Merge<TParentCommandTypes, TCommandTypes>),
>(
name: string,
cmd: TCommand,
override?: boolean,
): ReturnType<TCommand["reset"]> extends Command<
Record<string, unknown> | void,
Record<string, unknown> | void,
infer Options,
infer Arguments,
infer GlobalOptions,
infer Types,
infer GlobalTypes,
undefined
> ? Command<
TGlobalOptions,
TGlobalTypes,
Options,
Arguments,
GlobalOptions,
Types,
GlobalTypes,
OneOf<TParentCommand, this>
>
: never;
/**
* Add new sub-command.
* @param name Command definition. E.g: `my-command <input-file:string> <output-file:string>`
* @param cmd The new child command to register.
* @param override Override existing child command.
*/
public command<
TCommand extends Command<
TGlobalOptions | void | undefined,
TGlobalTypes | void | undefined,
Record<string, unknown> | void,
Array<unknown>,
Record<string, unknown> | void,
Record<string, unknown> | void,
Record<string, unknown> | void,
OneOf<TParentCommand, this> | undefined
>,
TGlobalOptions
extends (TParentCommand extends Command<any> ? TParentCommandGlobals
: Merge<TParentCommandGlobals, TCommandGlobals>),
TGlobalTypes
extends (TParentCommand extends Command<any> ? TParentCommandTypes
: Merge<TParentCommandTypes, TCommandTypes>),
>(
name: string,
cmd: TCommand,
override?: boolean,
): TCommand extends Command<
Record<string, unknown> | void,
Record<string, unknown> | void,
infer Options,
infer Arguments,
infer GlobalOptions,
infer Types,
infer GlobalTypes,
OneOf<TParentCommand, this> | undefined
> ? Command<
TGlobalOptions,
TGlobalTypes,
Options,
Arguments,
GlobalOptions,
Types,
GlobalTypes,
OneOf<TParentCommand, this>
>
: never;
/**
* Add new sub-command.
* @param nameAndArguments Command definition. E.g: `my-command <input-file:string> <output-file:string>`
* @param desc The description of the new child command.
* @param override Override existing child command.
*/
public command<
TNameAndArguments extends string,
TArguments extends TypedCommandArguments<
TNameAndArguments,
TParentCommand extends Command<any> ? TParentCommandTypes
: Merge<TParentCommandTypes, TCommandGlobalTypes>
>,
>(
nameAndArguments: TNameAndArguments,
desc?: string,
override?: boolean,
): TParentCommandGlobals extends number ? Command<any> : Command<
TParentCommand extends Command<any> ? TParentCommandGlobals
: Merge<TParentCommandGlobals, TCommandGlobals>,
TParentCommand extends Command<any> ? TParentCommandTypes
: Merge<TParentCommandTypes, TCommandGlobalTypes>,
void,
TArguments,
void,
void,
void,
OneOf<TParentCommand, this>
>;
/**
* Add new sub-command.
* @param nameAndArguments Command definition. E.g: `my-command <input-file:string> <output-file:string>`
* @param cmdOrDescription The description of the new child command.
* @param override Override existing child command.
*/
command(
nameAndArguments: string,
cmdOrDescription?: Command<any> | string,
override?: boolean,
): Command<any> {
this.reset();
const result = splitArguments(nameAndArguments);
const name: string | undefined = result.flags.shift();
const aliases: string[] = result.flags;
if (!name) {
throw new MissingCommandNameError();
}
if (this.getBaseCommand(name, true)) {
if (!override) {
throw new DuplicateCommandNameError(name);
}
this.removeCommand(name);
}
let description: string | undefined;
let cmd: Command<any>;
if (typeof cmdOrDescription === "string") {
description = cmdOrDescription;
}
if (cmdOrDescription instanceof Command) {
cmd = cmdOrDescription.reset();
} else {
cmd = new Command();
}
cmd._name = name;
cmd._parent = this;
if (description) {
cmd.description(description);
}
if (result.typeDefinition) {
cmd.arguments(result.typeDefinition);
}
aliases.forEach((alias: string) => cmd.alias(alias));
this.commands.set(name, cmd);
this.select(name);
return this;
}
/**
* Add new command alias.
* @param alias Tha name of the alias.
*/
public alias(alias: string): this {
if (this.cmd._name === alias || this.cmd.aliases.includes(alias)) {
throw new DuplicateCommandAliasError(alias);
}
this.cmd.aliases.push(alias);
return this;
}
/** Reset internal command reference to main command. */
public reset(): OneOf<TParentCommand, this> {
this._groupName = undefined;
this.cmd = this;
return this as OneOf<TParentCommand, this>;
}
/**
* Set internal command pointer to child command with given name.
* @param name The name of the command to select.
*/
public select<
TOptions extends Record<string, unknown> | void = any,
TArguments extends Array<unknown> = any,
TGlobalOptions extends Record<string, unknown> | void = any,
>(
name: string,
): Command<
TParentCommandGlobals,
TParentCommandTypes,
TOptions,
TArguments,
TGlobalOptions,
TCommandTypes,
TCommandGlobalTypes,
TParentCommand
> {
const cmd = this.getBaseCommand(name, true);
if (!cmd) {
throw new CommandNotFoundError(name, this.getBaseCommands(true));
}
this.cmd = cmd;
return this as Command<any>;
}
/*****************************************************************************
**** SUB HANDLER ************************************************************
*****************************************************************************/
/** Set command name. */
public name(name: string): this {
this.cmd._name = name;
return this;
}
/**
* Set command version.
* @param version Semantic version string string or method that returns the version string.
*/
public version(
version:
| string
| VersionHandler<
Partial<TCommandOptions>,
Partial<TCommandArguments>,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>,
): this {
if (typeof version === "string") {
this.cmd.ver = () => version;
} else if (typeof version === "function") {
this.cmd.ver = version;
}
return this;
}
public meta(name: string, value: string): this {
this.cmd._meta[name] = value;
return this;
}
public getMeta(): Record<string, string>;
public getMeta(name: string): string;
public getMeta(name?: string): Record<string, string> | string {
return typeof name === "undefined" ? this._meta : this._meta[name];
}
/**
* Set command help.
* @param help Help string, method, or config for generator that returns the help string.
*/
public help(
help:
| string
| HelpHandler<
Partial<TCommandOptions>,
Partial<TCommandArguments>,
TCommandGlobals,
TParentCommandGlobals
>
| HelpOptions,
): this {
if (typeof help === "string") {
this.cmd._help = () => help;
} else if (typeof help === "function") {
this.cmd._help = help;
} else {
this.cmd._help = (cmd: Command, options: HelpOptions): string =>
HelpGenerator.generate(cmd, { ...help, ...options });
}
return this;
}
/**
* Set the long command description.
* @param description The command description.
*/
public description(
description: Description<
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>,
): this {
this.cmd.desc = description;
return this;
}
/**
* Set the command usage. Defaults to arguments.
* @param usage The command usage.
*/
public usage(usage: string): this {
this.cmd._usage = usage;
return this;
}
/**
* Hide command from help, completions, etc.
*/
public hidden(): this {
this.cmd.isHidden = true;
return this;
}
/** Make command globally available. */
public global(): this {
this.cmd.isGlobal = true;
return this;
}
/** Make command executable. */
public executable(): this {
this.cmd.isExecutable = true;
return this;
}
/**
* Set command arguments:
*
* <requiredArg:string> [optionalArg: number] [...restArgs:string]
*/
public arguments<
TArguments extends TypedArguments<
TArgs,
Merge<TParentCommandTypes, Merge<TCommandGlobalTypes, TCommandTypes>>
>,
TArgs extends string = string,
>(
args: TArgs,
): Command<
TParentCommandGlobals,
TParentCommandTypes,
TCommandOptions,
TArguments,
TCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommand
> {
this.cmd.argsDefinition = args;
return this as Command<any>;
}
/**
* Set command callback method.
* @param fn Command action handler.
*/
public action(
fn: ActionHandler<
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>,
): this {
this.cmd.fn = fn;
return this;
}
/**
* Don't throw an error if the command was called without arguments.
* @param allowEmpty Enable/disable allow empty.
*/
// public allowEmpty<TAllowEmpty extends boolean | undefined = undefined>(
// allowEmpty?: TAllowEmpty,
// ): false extends TAllowEmpty ? this
// : Command<
// Partial<TParentCommandGlobals>,
// TParentCommandTypes,
// Partial<TCommandOptions>,
// TCommandArguments,
// TCommandGlobals,
// TCommandTypes,
// TCommandGlobalTypes,
// TParentCommand
// > {
// this.cmd._allowEmpty = allowEmpty !== false;
// return this;
// }
public allowEmpty<TAllowEmpty extends boolean | undefined = undefined>(
allowEmpty?: TAllowEmpty,
): false extends TAllowEmpty ? this
: Command<
Partial<TParentCommandGlobals>,
TParentCommandTypes,
Partial<TCommandOptions>,
TCommandArguments,
TCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommand
> {
this.cmd._allowEmpty = allowEmpty !== false;
return this as false extends TAllowEmpty ? this
: Command<
Partial<TParentCommandGlobals>,
TParentCommandTypes,
Partial<TCommandOptions>,
TCommandArguments,
TCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommand
>;
}
/**
* Enable stop early. If enabled, all arguments starting from the first non
* option argument will be passed as arguments with type string to the command
* action handler.
*
* For example:
* `command --debug-level warning server --port 80`
*
* Will result in:
* - options: `{debugLevel: 'warning'}`
* - args: `['server', '--port', '80']`
*
* @param stopEarly Enable/disable stop early.
*/
public stopEarly(stopEarly = true): this {
this.cmd._stopEarly = stopEarly;
return this;
}
/**
* Disable parsing arguments. If enabled the raw arguments will be passed to
* the action handler. This has no effect for parent or child commands. Only
* for the command on which this method was called.
* @param useRawArgs Enable/disable raw arguments.
*/
public useRawArgs(
useRawArgs = true,
): Command<
void,
void,
void,
Array<string>,
void,
void,
void,
TParentCommand
> {
this.cmd._useRawArgs = useRawArgs;
return this as Command<any>;
}
/**
* Set default command. The default command is executed when the program
* was called without any argument and if no action handler is registered.
* @param name Name of the default command.
*/
public default(name: string): this {
this.cmd.defaultCommand = name;
return this;
}
public globalType<
THandler extends TypeOrTypeHandler<unknown>,
TName extends string = string,
>(
name: TName,
handler: THandler,
options?: Omit<TypeOptions, "global">,
): Command<
TParentCommandGlobals,
TParentCommandTypes,
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TCommandTypes,
Merge<TCommandGlobalTypes, TypedType<TName, THandler>>,
TParentCommand
> {
return this.type(name, handler, { ...options, global: true });
}
/**
* Register custom type.
* @param name The name of the type.
* @param handler The callback method to parse the type.
* @param options Type options.
*/
public type<
THandler extends TypeOrTypeHandler<unknown>,
TName extends string = string,
>(
name: TName,
handler: THandler,
options?: TypeOptions,
): Command<
TParentCommandGlobals,
TParentCommandTypes,
TCommandOptions,
TCommandArguments,
TCommandGlobals,
Merge<TCommandTypes, TypedType<TName, THandler>>,
TCommandGlobalTypes,
TParentCommand
> {
if (this.cmd.types.get(name) && !options?.override) {
throw new DuplicateTypeError(name);
}
this.cmd.types.set(name, {
...options,
name,
handler: handler as TypeOrTypeHandler<unknown>,
});
if (
handler instanceof Type &&
(typeof handler.complete !== "undefined" ||
typeof handler.values !== "undefined")
) {
const completeHandler: CompleteHandler = (
cmd: Command,
parent?: Command,
) => handler.complete?.(cmd, parent) || [];
this.complete(name, completeHandler, options);
}
return this as Command<any>;
}
public globalComplete(
name: string,
complete: CompleteHandler,
options?: Omit<CompleteOptions, "global">,
): this {
return this.complete(name, complete, { ...options, global: true });
}
/**
* Register command specific custom type.
* @param name The name of the completion.
* @param complete The callback method to complete the type.
* @param options Complete options.
*/
public complete(
name: string,
complete: CompleteHandler<
Partial<TCommandOptions>,
Partial<TCommandArguments>,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
any
>,
options: CompleteOptions & { global: boolean },
): this;
public complete(
name: string,
complete: CompleteHandler<
TCommandOptions,
TCommandArguments,
TCommandGlobals,
TParentCommandGlobals,
TCommandTypes,
TCommandGlobalTypes,
TParentCommandTypes,
TParentCommand
>,
options?: CompleteOptions,
): this;
public complete(
name: string,
complete:
| CompleteHandler<
TCommandOptions,
TCommandArguments,