@bemoje/cli 4.0.0 → 5.0.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.
- package/README.md +382 -372
- package/lib/index.cjs +442 -290
- package/lib/index.d.cts +247 -47
- package/lib/index.d.ts +247 -47
- package/lib/index.js +442 -290
- package/package.json +5 -2
package/lib/index.js
CHANGED
|
@@ -110,9 +110,7 @@ __name(valuesOf, "valuesOf");
|
|
|
110
110
|
|
|
111
111
|
// src/lib/helpers/findCommand.ts
|
|
112
112
|
function findCommand(cmd, nameOrAlias) {
|
|
113
|
-
return cmd.commands[nameOrAlias] ?? valuesOf(cmd.commands).find((c) =>
|
|
114
|
-
return c.aliases.includes(nameOrAlias);
|
|
115
|
-
});
|
|
113
|
+
return cmd.commands[nameOrAlias] ?? valuesOf(cmd.commands).find((c) => c.aliases.includes(nameOrAlias));
|
|
116
114
|
}
|
|
117
115
|
__name(findCommand, "findCommand");
|
|
118
116
|
|
|
@@ -344,13 +342,9 @@ var Help = class {
|
|
|
344
342
|
* Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one.
|
|
345
343
|
*/
|
|
346
344
|
visibleCommands() {
|
|
347
|
-
const res = Object.values(this.cmd.commands).filter((c) =>
|
|
348
|
-
return !c.hidden;
|
|
349
|
-
});
|
|
345
|
+
const res = Object.values(this.cmd.commands).filter((c) => !c.hidden);
|
|
350
346
|
if (this.sortSubcommands) {
|
|
351
|
-
res.sort((a, b) =>
|
|
352
|
-
return a.name.localeCompare(b.name);
|
|
353
|
-
});
|
|
347
|
+
res.sort((a, b) => a.name.localeCompare(b.name));
|
|
354
348
|
}
|
|
355
349
|
return res;
|
|
356
350
|
}
|
|
@@ -358,18 +352,17 @@ var Help = class {
|
|
|
358
352
|
* Compare options for sort.
|
|
359
353
|
*/
|
|
360
354
|
compareOptions(a, b) {
|
|
361
|
-
const getSortKey = /* @__PURE__ */ __name((option) =>
|
|
362
|
-
|
|
363
|
-
|
|
355
|
+
const getSortKey = /* @__PURE__ */ __name((option) => (
|
|
356
|
+
// WYSIWYG for order displayed in help. Short used for comparison if present. No special handling for negated.
|
|
357
|
+
option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "")
|
|
358
|
+
), "getSortKey");
|
|
364
359
|
return getSortKey(a).localeCompare(getSortKey(b));
|
|
365
360
|
}
|
|
366
361
|
/**
|
|
367
362
|
* Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one.
|
|
368
363
|
*/
|
|
369
364
|
visibleOptions() {
|
|
370
|
-
const res = this.cmd.options.filter((option) =>
|
|
371
|
-
return !option.hidden;
|
|
372
|
-
});
|
|
365
|
+
const res = this.cmd.options.filter((option) => !option.hidden);
|
|
373
366
|
if (this.sortOptions) {
|
|
374
367
|
res.sort(this.compareOptions);
|
|
375
368
|
}
|
|
@@ -385,9 +378,7 @@ var Help = class {
|
|
|
385
378
|
* Get the command term to show in the list of subcommands.
|
|
386
379
|
*/
|
|
387
380
|
subcommandTerm(sub) {
|
|
388
|
-
const args = sub.arguments.map((arg) =>
|
|
389
|
-
return arg.usage;
|
|
390
|
-
}).join(" ");
|
|
381
|
+
const args = sub.arguments.map((arg) => arg.usage).join(" ");
|
|
391
382
|
return (sub.aliases[0] ? `${sub.aliases[0].padEnd(this.longestSubcommandAliasLength(), " ")} | ` : "") + sub.name + (sub.options.length ? ` ${this.usageDisplayOptionsAs}` : "") + // simplistic check for non-help option
|
|
392
383
|
(args ? ` ${args}` : "");
|
|
393
384
|
}
|
|
@@ -407,33 +398,25 @@ var Help = class {
|
|
|
407
398
|
* Get the longest subcommand primary alias length.
|
|
408
399
|
*/
|
|
409
400
|
longestSubcommandAliasLength() {
|
|
410
|
-
return Math.max(0, ...this.visibleCommands().map((c) =>
|
|
411
|
-
return c.aliases[0]?.length || 0;
|
|
412
|
-
}));
|
|
401
|
+
return Math.max(0, ...this.visibleCommands().map((c) => c.aliases[0]?.length || 0));
|
|
413
402
|
}
|
|
414
403
|
/**
|
|
415
404
|
* Get the longest subcommand term length.
|
|
416
405
|
*/
|
|
417
406
|
longestSubcommandTermLength() {
|
|
418
|
-
return this.visibleCommands().reduce((max, command) =>
|
|
419
|
-
return Math.max(max, this.displayWidth(this.styleSubcommandTerm(this.subcommandTerm(command))));
|
|
420
|
-
}, 0);
|
|
407
|
+
return this.visibleCommands().reduce((max, command) => Math.max(max, this.displayWidth(this.styleSubcommandTerm(this.subcommandTerm(command)))), 0);
|
|
421
408
|
}
|
|
422
409
|
/**
|
|
423
410
|
* Get the longest option term length.
|
|
424
411
|
*/
|
|
425
412
|
longestOptionTermLength() {
|
|
426
|
-
return this.visibleOptions().reduce((max, option) =>
|
|
427
|
-
return Math.max(max, this.displayWidth(this.styleOptionTerm(this.optionTerm(option))));
|
|
428
|
-
}, 0);
|
|
413
|
+
return this.visibleOptions().reduce((max, option) => Math.max(max, this.displayWidth(this.styleOptionTerm(this.optionTerm(option)))), 0);
|
|
429
414
|
}
|
|
430
415
|
/**
|
|
431
416
|
* Get the longest argument term length.
|
|
432
417
|
*/
|
|
433
418
|
longestArgumentTermLength() {
|
|
434
|
-
return this.visibleArguments().reduce((max, argument) =>
|
|
435
|
-
return Math.max(max, this.displayWidth(this.styleArgumentTerm(this.argumentTerm(argument))));
|
|
436
|
-
}, 0);
|
|
419
|
+
return this.visibleArguments().reduce((max, argument) => Math.max(max, this.displayWidth(this.styleArgumentTerm(this.argumentTerm(argument)))), 0);
|
|
437
420
|
}
|
|
438
421
|
/**
|
|
439
422
|
* Get the command usage to be displayed at the top of the built-in help.
|
|
@@ -450,9 +433,7 @@ var Help = class {
|
|
|
450
433
|
...this.cmd.options.length ? [
|
|
451
434
|
this.usageDisplayOptionsAs
|
|
452
435
|
] : [],
|
|
453
|
-
...this.cmd.arguments.map((arg) => {
|
|
454
|
-
return arg.required ? arg.variadic ? `<${arg.name}...>` : `<${arg.name}>` : arg.variadic ? `[${arg.name}...]` : `[${arg.name}]`;
|
|
455
|
-
})
|
|
436
|
+
...this.cmd.arguments.map((arg) => arg.required ? arg.variadic ? `<${arg.name}...>` : `<${arg.name}>` : arg.variadic ? `[${arg.name}...]` : `[${arg.name}]`)
|
|
456
437
|
].join(" ")}`.trim();
|
|
457
438
|
}
|
|
458
439
|
/**
|
|
@@ -778,26 +759,16 @@ ${itemIndentStr}`);
|
|
|
778
759
|
""
|
|
779
760
|
]);
|
|
780
761
|
}
|
|
781
|
-
const argumentList = this.visibleArguments().map((argument) =>
|
|
782
|
-
return this.formatItem(this.styleArgumentTerm(this.argumentTerm(argument)), this.padWidth(), this.styleArgumentDescription(this.argumentDescription(argument)));
|
|
783
|
-
});
|
|
762
|
+
const argumentList = this.visibleArguments().map((argument) => this.formatItem(this.styleArgumentTerm(this.argumentTerm(argument)), this.padWidth(), this.styleArgumentDescription(this.argumentDescription(argument))));
|
|
784
763
|
output = output.concat(this.formatItemList("Arguments:", argumentList));
|
|
785
|
-
const optionGroups = this.groupItems(this.cmd.options, this.visibleOptions(), (option) =>
|
|
786
|
-
return option.group ?? "Options:";
|
|
787
|
-
});
|
|
764
|
+
const optionGroups = this.groupItems(this.cmd.options, this.visibleOptions(), (option) => option.group ?? "Options:");
|
|
788
765
|
optionGroups.forEach((options, group) => {
|
|
789
|
-
const optionList = options.map((option) =>
|
|
790
|
-
return this.formatItem(this.styleOptionTerm(this.optionTerm(option)), this.padWidth(), this.styleOptionDescription(this.optionDescription(option)));
|
|
791
|
-
});
|
|
766
|
+
const optionList = options.map((option) => this.formatItem(this.styleOptionTerm(this.optionTerm(option)), this.padWidth(), this.styleOptionDescription(this.optionDescription(option))));
|
|
792
767
|
output = output.concat(this.formatItemList(group, optionList));
|
|
793
768
|
});
|
|
794
|
-
const commandGroups = this.groupItems(Object.values(this.cmd.commands), this.visibleCommands(), (sub) =>
|
|
795
|
-
return sub.group || "Commands:";
|
|
796
|
-
});
|
|
769
|
+
const commandGroups = this.groupItems(Object.values(this.cmd.commands), this.visibleCommands(), (sub) => sub.group || "Commands:");
|
|
797
770
|
commandGroups.forEach((commands, group) => {
|
|
798
|
-
const commandList = commands.map((sub) =>
|
|
799
|
-
return this.formatItem(this.styleSubcommandTerm(this.subcommandTerm(sub)), this.padWidth(), this.styleSubcommandDescription(this.subcommandDescription(sub)));
|
|
800
|
-
});
|
|
771
|
+
const commandList = commands.map((sub) => this.formatItem(this.styleSubcommandTerm(this.subcommandTerm(sub)), this.padWidth(), this.styleSubcommandDescription(this.subcommandDescription(sub))));
|
|
801
772
|
output = output.concat(this.formatItemList(group, commandList));
|
|
802
773
|
});
|
|
803
774
|
return output.join("\n");
|
|
@@ -852,39 +823,275 @@ _ts_decorate([
|
|
|
852
823
|
_ts_metadata("design:returntype", Number)
|
|
853
824
|
], Help.prototype, "padWidth", null);
|
|
854
825
|
|
|
826
|
+
// src/lib/internal/isOptionLike.ts
|
|
827
|
+
function isOptionLike(word) {
|
|
828
|
+
return word.startsWith("-") && word !== "-" && !/^-(\d+\.?\d*|\.\d+)$/.test(word);
|
|
829
|
+
}
|
|
830
|
+
__name(isOptionLike, "isOptionLike");
|
|
831
|
+
|
|
832
|
+
// src/lib/internal/checkOptionValues.ts
|
|
833
|
+
function checkOptionValues(argv, options) {
|
|
834
|
+
const out = [];
|
|
835
|
+
const report = {
|
|
836
|
+
errors: [],
|
|
837
|
+
invalid: /* @__PURE__ */ new Set()
|
|
838
|
+
};
|
|
839
|
+
for (let i = 0; i < argv.length; i++) {
|
|
840
|
+
const word = argv[i];
|
|
841
|
+
if (word === "--") {
|
|
842
|
+
out.push(...argv.slice(i));
|
|
843
|
+
break;
|
|
844
|
+
}
|
|
845
|
+
const { keep, read } = word.startsWith("--") ? checkLong(word, argv[i + 1], options, report) : isOptionLike(word) ? checkShort(word, argv[i + 1], options, report) : {
|
|
846
|
+
keep: [
|
|
847
|
+
word
|
|
848
|
+
],
|
|
849
|
+
read: 1
|
|
850
|
+
};
|
|
851
|
+
out.push(...keep);
|
|
852
|
+
i += read - 1;
|
|
853
|
+
}
|
|
854
|
+
return {
|
|
855
|
+
argv: out,
|
|
856
|
+
...report
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
__name(checkOptionValues, "checkOptionValues");
|
|
860
|
+
function needsValue(def) {
|
|
861
|
+
return def.type === "string" && !(def.variadic && !def.required);
|
|
862
|
+
}
|
|
863
|
+
__name(needsValue, "needsValue");
|
|
864
|
+
function isNoValue(next) {
|
|
865
|
+
return next === void 0 || isOptionLike(next);
|
|
866
|
+
}
|
|
867
|
+
__name(isNoValue, "isNoValue");
|
|
868
|
+
function reportMissingValue(def, report) {
|
|
869
|
+
report.errors.push(`Option --${def.long} requires a value`);
|
|
870
|
+
report.invalid.add(def.name);
|
|
871
|
+
}
|
|
872
|
+
__name(reportMissingValue, "reportMissingValue");
|
|
873
|
+
function checkLong(word, next, options, report) {
|
|
874
|
+
const eq = word.indexOf("=");
|
|
875
|
+
const name = eq === -1 ? word.slice(2) : word.slice(2, eq);
|
|
876
|
+
const def = options.find((o) => o.name === name);
|
|
877
|
+
const negated = !def && name.startsWith("no-") ? options.find((o) => o.name === name.slice(3)) : void 0;
|
|
878
|
+
if (def?.type === "boolean" && eq !== -1) {
|
|
879
|
+
report.errors.push(`Option --${def.long} does not take a value: --${def.long}${word.slice(eq)}`);
|
|
880
|
+
report.invalid.add(def.name);
|
|
881
|
+
return {
|
|
882
|
+
keep: [],
|
|
883
|
+
read: 1
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
if (negated?.type === "string") {
|
|
887
|
+
report.errors.push(`Option --${negated.long} takes a value and cannot be negated: --no-${negated.long}`);
|
|
888
|
+
report.invalid.add(negated.name);
|
|
889
|
+
return {
|
|
890
|
+
keep: [],
|
|
891
|
+
read: 1
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
if (def?.type !== "string" || eq !== -1) {
|
|
895
|
+
return {
|
|
896
|
+
keep: [
|
|
897
|
+
word
|
|
898
|
+
],
|
|
899
|
+
read: 1
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
if (!isNoValue(next)) {
|
|
903
|
+
return {
|
|
904
|
+
keep: [
|
|
905
|
+
word,
|
|
906
|
+
next
|
|
907
|
+
],
|
|
908
|
+
read: 2
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
if (needsValue(def)) {
|
|
912
|
+
reportMissingValue(def, report);
|
|
913
|
+
return {
|
|
914
|
+
keep: [],
|
|
915
|
+
read: 1
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
return {
|
|
919
|
+
keep: [
|
|
920
|
+
word
|
|
921
|
+
],
|
|
922
|
+
read: 1
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
__name(checkLong, "checkLong");
|
|
926
|
+
function checkShort(word, next, options, report) {
|
|
927
|
+
for (let j = 1; j < word.length; j++) {
|
|
928
|
+
const def = options.find((o) => o.short === word[j]);
|
|
929
|
+
if (def?.type !== "string") {
|
|
930
|
+
continue;
|
|
931
|
+
}
|
|
932
|
+
const before = j > 1 ? [
|
|
933
|
+
word.slice(0, j)
|
|
934
|
+
] : [];
|
|
935
|
+
if (j < word.length - 1) {
|
|
936
|
+
return word[j + 1] === "=" ? {
|
|
937
|
+
keep: [
|
|
938
|
+
...before,
|
|
939
|
+
`--${def.name}${word.slice(j + 1)}`
|
|
940
|
+
],
|
|
941
|
+
read: 1
|
|
942
|
+
} : {
|
|
943
|
+
keep: [
|
|
944
|
+
word
|
|
945
|
+
],
|
|
946
|
+
read: 1
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
if (!isNoValue(next)) {
|
|
950
|
+
return {
|
|
951
|
+
keep: [
|
|
952
|
+
word,
|
|
953
|
+
next
|
|
954
|
+
],
|
|
955
|
+
read: 2
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
if (needsValue(def)) {
|
|
959
|
+
reportMissingValue(def, report);
|
|
960
|
+
return {
|
|
961
|
+
keep: before,
|
|
962
|
+
read: 1
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
break;
|
|
966
|
+
}
|
|
967
|
+
return {
|
|
968
|
+
keep: [
|
|
969
|
+
word
|
|
970
|
+
],
|
|
971
|
+
read: 1
|
|
972
|
+
};
|
|
973
|
+
}
|
|
974
|
+
__name(checkShort, "checkShort");
|
|
975
|
+
|
|
855
976
|
// src/lib/internal/collectVariadicOptionValues.ts
|
|
856
977
|
function collectVariadicOptionValues(parsed, options) {
|
|
978
|
+
const taken = /* @__PURE__ */ new Set();
|
|
979
|
+
const collected = /* @__PURE__ */ new Map();
|
|
857
980
|
for (let i = 0; i < parsed.tokens.length; i++) {
|
|
858
981
|
const token = parsed.tokens[i];
|
|
859
982
|
if (token.kind !== "option") {
|
|
860
983
|
continue;
|
|
861
984
|
}
|
|
862
|
-
const def = options.find((o) =>
|
|
863
|
-
return o.name === token.name;
|
|
864
|
-
});
|
|
985
|
+
const def = options.find((o) => o.name === token.name);
|
|
865
986
|
if (!def?.variadic || def.type !== "string") {
|
|
866
987
|
continue;
|
|
867
988
|
}
|
|
868
|
-
const values = [
|
|
869
|
-
|
|
870
|
-
|
|
989
|
+
const values = collected.get(token.name) ?? [];
|
|
990
|
+
collected.set(token.name, values);
|
|
991
|
+
if (token.value !== void 0) {
|
|
992
|
+
values.push(token.value);
|
|
993
|
+
}
|
|
871
994
|
let j = i + 1;
|
|
872
995
|
while (j < parsed.tokens.length && parsed.tokens[j].kind === "positional") {
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
const posIndex = parsed.positionals.indexOf(positionalToken.value);
|
|
876
|
-
if (posIndex !== -1) {
|
|
877
|
-
parsed.positionals.splice(posIndex, 1);
|
|
878
|
-
}
|
|
996
|
+
values.push(parsed.tokens[j].value);
|
|
997
|
+
taken.add(j);
|
|
879
998
|
j++;
|
|
880
999
|
}
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
1000
|
+
}
|
|
1001
|
+
if (taken.size) {
|
|
1002
|
+
parsed.positionals = parsed.tokens.filter((token, index) => token.kind === "positional" && !taken.has(index)).map((token) => token.value);
|
|
1003
|
+
}
|
|
1004
|
+
for (const [name, values] of collected) {
|
|
1005
|
+
Reflect.set(parsed.values, name, values);
|
|
884
1006
|
}
|
|
885
1007
|
}
|
|
886
1008
|
__name(collectVariadicOptionValues, "collectVariadicOptionValues");
|
|
887
1009
|
|
|
1010
|
+
// src/lib/internal/createOption.ts
|
|
1011
|
+
function createOption(flags, opts = {}) {
|
|
1012
|
+
const ins = {};
|
|
1013
|
+
const { short, long, name, argName } = parseOptionFlags(flags);
|
|
1014
|
+
ins.flags = flags;
|
|
1015
|
+
ins.short = short;
|
|
1016
|
+
ins.long = long;
|
|
1017
|
+
ins.name = name;
|
|
1018
|
+
ins.description = "";
|
|
1019
|
+
if (argName) {
|
|
1020
|
+
ins.type = "string";
|
|
1021
|
+
ins.argName = argName;
|
|
1022
|
+
if (flags.endsWith(">")) {
|
|
1023
|
+
if (flags.endsWith("...>")) {
|
|
1024
|
+
ins.required = true;
|
|
1025
|
+
ins.variadic = true;
|
|
1026
|
+
} else {
|
|
1027
|
+
ins.required = true;
|
|
1028
|
+
}
|
|
1029
|
+
} else if (flags.endsWith("]") && flags.endsWith("...]")) {
|
|
1030
|
+
ins.variadic = true;
|
|
1031
|
+
ins.defaultValue = opts.defaultValue ?? [];
|
|
1032
|
+
}
|
|
1033
|
+
} else {
|
|
1034
|
+
ins.type = "boolean";
|
|
1035
|
+
}
|
|
1036
|
+
for (const [key, value] of Object.entries(opts)) {
|
|
1037
|
+
if (value !== void 0) {
|
|
1038
|
+
Reflect.set(ins, key, value);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
if (ins.env && ins.defaultValue === void 0 && typeof process.env[ins.env] === "string") {
|
|
1042
|
+
ins.required = false;
|
|
1043
|
+
ins.flags = ins.flags.replace("<", "[").replace(">", "]");
|
|
1044
|
+
if (ins.type === "boolean") {
|
|
1045
|
+
ins.defaultValue = /^(t(rue)?|y(es)?|1)$/i.test(process.env[ins.env]);
|
|
1046
|
+
} else if (ins.variadic) {
|
|
1047
|
+
ins.defaultValue = process.env[ins.env].replace(/\[]/, "").split(",").map((v) => v.trim());
|
|
1048
|
+
} else {
|
|
1049
|
+
ins.defaultValue = process.env[ins.env];
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
return ins;
|
|
1053
|
+
}
|
|
1054
|
+
__name(createOption, "createOption");
|
|
1055
|
+
|
|
1056
|
+
// src/lib/internal/findSubcommandIndex.ts
|
|
1057
|
+
function findSubcommandIndex(cmd, argv) {
|
|
1058
|
+
const takesNext = /* @__PURE__ */ __name((next) => next !== void 0 && !isOptionLike(next) ? 1 : 0, "takesNext");
|
|
1059
|
+
for (let i = 0; i < argv.length; i++) {
|
|
1060
|
+
const word = argv[i];
|
|
1061
|
+
if (word === "--") {
|
|
1062
|
+
return -1;
|
|
1063
|
+
}
|
|
1064
|
+
if (!isOptionLike(word)) {
|
|
1065
|
+
return findCommand(cmd, word) ? i : -1;
|
|
1066
|
+
}
|
|
1067
|
+
if (word.startsWith("--")) {
|
|
1068
|
+
const name = word.slice(2);
|
|
1069
|
+
const def = cmd.options.find((o) => o.long === name || o.name === name);
|
|
1070
|
+
if (def?.type === "string") {
|
|
1071
|
+
if (def.variadic) {
|
|
1072
|
+
return -1;
|
|
1073
|
+
}
|
|
1074
|
+
i += takesNext(argv[i + 1]);
|
|
1075
|
+
}
|
|
1076
|
+
continue;
|
|
1077
|
+
}
|
|
1078
|
+
for (let j = 1; j < word.length; j++) {
|
|
1079
|
+
const def = cmd.options.find((o) => o.short === word[j]);
|
|
1080
|
+
if (def?.type === "string") {
|
|
1081
|
+
if (j === word.length - 1) {
|
|
1082
|
+
if (def.variadic) {
|
|
1083
|
+
return -1;
|
|
1084
|
+
}
|
|
1085
|
+
i += takesNext(argv[i + 1]);
|
|
1086
|
+
}
|
|
1087
|
+
break;
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
return -1;
|
|
1092
|
+
}
|
|
1093
|
+
__name(findSubcommandIndex, "findSubcommandIndex");
|
|
1094
|
+
|
|
888
1095
|
// src/lib/internal/mergeOptionDefaults.ts
|
|
889
1096
|
function mergeOptionDefaults(values, options) {
|
|
890
1097
|
for (const option of options) {
|
|
@@ -897,21 +1104,22 @@ __name(mergeOptionDefaults, "mergeOptionDefaults");
|
|
|
897
1104
|
|
|
898
1105
|
// src/lib/internal/normalizeArgv.ts
|
|
899
1106
|
function normalizeArgv(argv, options) {
|
|
1107
|
+
const end = argv.includes("--") ? argv.indexOf("--") : argv.length;
|
|
1108
|
+
const head = argv.slice(0, end);
|
|
900
1109
|
for (const o of options) {
|
|
901
1110
|
if (o.long === o.name) {
|
|
902
1111
|
continue;
|
|
903
1112
|
}
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
if (a === `--no-${o.long}`) {
|
|
909
|
-
|
|
1113
|
+
for (let i = 0; i < head.length; i++) {
|
|
1114
|
+
const a = head[i];
|
|
1115
|
+
if (a === `--${o.long}` || a.startsWith(`--${o.long}=`)) {
|
|
1116
|
+
head[i] = `--${o.name}${a.slice(2 + o.long.length)}`;
|
|
1117
|
+
} else if (a === `--no-${o.long}`) {
|
|
1118
|
+
head[i] = `--no-${o.name}`;
|
|
910
1119
|
}
|
|
911
|
-
|
|
912
|
-
});
|
|
1120
|
+
}
|
|
913
1121
|
}
|
|
914
|
-
return argv;
|
|
1122
|
+
return head.concat(argv.slice(end));
|
|
915
1123
|
}
|
|
916
1124
|
__name(normalizeArgv, "normalizeArgv");
|
|
917
1125
|
function resolveArguments(positionals, args) {
|
|
@@ -968,47 +1176,29 @@ function createLogger(name) {
|
|
|
968
1176
|
const cyanArgs = createColoredArgs(C.cyan);
|
|
969
1177
|
const yellowArgs = createColoredArgs(C.yellow);
|
|
970
1178
|
return {
|
|
971
|
-
start: /* @__PURE__ */ __name((...args) =>
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
return console.log(...NAME ? [
|
|
982
|
-
NAME,
|
|
983
|
-
...args
|
|
984
|
-
] : args);
|
|
985
|
-
}, "log"),
|
|
986
|
-
warn: /* @__PURE__ */ __name((...args) => {
|
|
987
|
-
return console.warn(...WARN, ...yellowArgs(args));
|
|
988
|
-
}, "warn"),
|
|
989
|
-
debug: /* @__PURE__ */ __name((...args) => {
|
|
990
|
-
return console.debug(...DEBUG, ...cyanArgs(args));
|
|
991
|
-
}, "debug"),
|
|
992
|
-
error: /* @__PURE__ */ __name((...args) => {
|
|
993
|
-
return args.forEach((arg) => {
|
|
994
|
-
return console.error(...ERROR, arg);
|
|
995
|
-
});
|
|
996
|
-
}, "error")
|
|
1179
|
+
start: /* @__PURE__ */ __name((...args) => console.info(...START, ...args), "start"),
|
|
1180
|
+
done: /* @__PURE__ */ __name((...args) => console.info(...DONE, ...args), "done"),
|
|
1181
|
+
info: /* @__PURE__ */ __name((...args) => console.info(...INFO, ...grayArgs(args)), "info"),
|
|
1182
|
+
log: /* @__PURE__ */ __name((...args) => console.log(...NAME ? [
|
|
1183
|
+
NAME,
|
|
1184
|
+
...args
|
|
1185
|
+
] : args), "log"),
|
|
1186
|
+
warn: /* @__PURE__ */ __name((...args) => console.warn(...WARN, ...yellowArgs(args)), "warn"),
|
|
1187
|
+
debug: /* @__PURE__ */ __name((...args) => console.debug(...DEBUG, ...cyanArgs(args)), "debug"),
|
|
1188
|
+
error: /* @__PURE__ */ __name((...args) => args.forEach((arg) => console.error(...ERROR, arg)), "error")
|
|
997
1189
|
};
|
|
998
1190
|
}
|
|
999
1191
|
__name(createLogger, "createLogger");
|
|
1000
1192
|
function createColoredArgs(colorFn) {
|
|
1001
|
-
return (args) => {
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
});
|
|
1011
|
-
};
|
|
1193
|
+
return (args) => args.map((arg) => {
|
|
1194
|
+
if (isString(arg)) {
|
|
1195
|
+
return arg === C.stripColor(arg) ? colorFn(arg) : arg;
|
|
1196
|
+
} else if (isPrimitive(arg)) {
|
|
1197
|
+
return colorFn(String(arg));
|
|
1198
|
+
} else {
|
|
1199
|
+
return arg;
|
|
1200
|
+
}
|
|
1201
|
+
});
|
|
1012
1202
|
}
|
|
1013
1203
|
__name(createColoredArgs, "createColoredArgs");
|
|
1014
1204
|
function timer(arg, task) {
|
|
@@ -1025,9 +1215,7 @@ function timer(arg, task) {
|
|
|
1025
1215
|
if (!isPromise(retval)) {
|
|
1026
1216
|
return done(retval);
|
|
1027
1217
|
}
|
|
1028
|
-
return retval.then((result) =>
|
|
1029
|
-
return done(result);
|
|
1030
|
-
});
|
|
1218
|
+
return retval.then((result) => done(result));
|
|
1031
1219
|
function done(retval2) {
|
|
1032
1220
|
if (process.exitCode) {
|
|
1033
1221
|
return retval2;
|
|
@@ -1041,7 +1229,7 @@ function timer(arg, task) {
|
|
|
1041
1229
|
__name(timer, "timer");
|
|
1042
1230
|
|
|
1043
1231
|
// src/lib/internal/validateParsed.ts
|
|
1044
|
-
function validateParsed(args, optionValues, argDefs, optionDefs) {
|
|
1232
|
+
function validateParsed(args, optionValues, argDefs, optionDefs, rawNames = {}, invalid = /* @__PURE__ */ new Set()) {
|
|
1045
1233
|
return argDefs.map((def, index) => {
|
|
1046
1234
|
const value = args[index];
|
|
1047
1235
|
if (def.required && (def.variadic ? Array.isArray(value) && value.length === 0 : value === void 0)) {
|
|
@@ -1049,34 +1237,25 @@ function validateParsed(args, optionValues, argDefs, optionDefs) {
|
|
|
1049
1237
|
}
|
|
1050
1238
|
if (def.choices && value !== void 0 && ![
|
|
1051
1239
|
value
|
|
1052
|
-
].flat().every((v) => {
|
|
1053
|
-
return def.choices.
|
|
1054
|
-
})) {
|
|
1055
|
-
return `Invalid argument [${index}] ${def.usage}: Got \`${value}\`. Accepted values: [${def.choices.map((c) => {
|
|
1056
|
-
return `\`${c}\``;
|
|
1057
|
-
}).join(",")}]`;
|
|
1240
|
+
].flat().every((v) => def.choices.includes(v))) {
|
|
1241
|
+
return `Invalid argument [${index}] ${def.usage}: Got \`${value}\`. Accepted values: [${def.choices.map((c) => `\`${c}\``).join(",")}]`;
|
|
1058
1242
|
}
|
|
1059
1243
|
}).concat(entriesOf(optionValues).map(([key, value]) => {
|
|
1060
|
-
const def = optionDefs.find((o) =>
|
|
1061
|
-
return o.name === key;
|
|
1062
|
-
});
|
|
1244
|
+
const def = optionDefs.find((o) => o.name === key);
|
|
1063
1245
|
if (!def) {
|
|
1064
|
-
return `Unknown option
|
|
1246
|
+
return `Unknown option ${rawNames[key] ?? `--${key}`}`;
|
|
1065
1247
|
}
|
|
1066
1248
|
if (def.choices && value !== void 0 && !(def.variadic ? value : [
|
|
1067
1249
|
value
|
|
1068
|
-
]).every((v) => {
|
|
1069
|
-
return def.choices.
|
|
1070
|
-
}
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
}).reduce((acc, curr) => {
|
|
1078
|
-
return (acc ?? []).concat(curr);
|
|
1079
|
-
}, void 0);
|
|
1250
|
+
]).every((v) => def.choices.includes(v))) {
|
|
1251
|
+
return `Invalid option value ${def.flags}: Got \`${value}\`. Accepted values: [${def.choices.map((c) => `\`${c}\``).join(",")}]`;
|
|
1252
|
+
}
|
|
1253
|
+
})).concat(optionDefs.map((def) => {
|
|
1254
|
+
const value = optionValues[def.name];
|
|
1255
|
+
if (def.required && !invalid.has(def.name) && (value === void 0 || Array.isArray(value) && !value.length)) {
|
|
1256
|
+
return `Missing option ${def.flags}`;
|
|
1257
|
+
}
|
|
1258
|
+
})).filter((s) => s !== void 0).reduce((acc, curr) => (acc ?? []).concat(curr), void 0);
|
|
1080
1259
|
}
|
|
1081
1260
|
__name(validateParsed, "validateParsed");
|
|
1082
1261
|
|
|
@@ -1092,6 +1271,7 @@ function _ts_metadata2(k, v) {
|
|
|
1092
1271
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
1093
1272
|
}
|
|
1094
1273
|
__name(_ts_metadata2, "_ts_metadata");
|
|
1274
|
+
var builtinOptions = /* @__PURE__ */ new WeakSet();
|
|
1095
1275
|
var Command = class _Command {
|
|
1096
1276
|
static {
|
|
1097
1277
|
__name(this, "Command");
|
|
@@ -1135,21 +1315,17 @@ var Command = class _Command {
|
|
|
1135
1315
|
enumerable: false
|
|
1136
1316
|
});
|
|
1137
1317
|
if (!parent) {
|
|
1138
|
-
this.
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
console.debug(inspect(cmd, {
|
|
1142
|
-
depth: 1,
|
|
1318
|
+
this.addBuiltinOption("-D, --debug", "Display debug information", ({ cmd, ...data }) => {
|
|
1319
|
+
data.logger.debug(inspect(cmd, {
|
|
1320
|
+
depth: 3,
|
|
1143
1321
|
colors: true
|
|
1144
1322
|
}));
|
|
1145
|
-
|
|
1146
|
-
depth:
|
|
1323
|
+
data.logger.debug(inspect(data, {
|
|
1324
|
+
depth: 1,
|
|
1147
1325
|
colors: true
|
|
1148
1326
|
}));
|
|
1149
1327
|
});
|
|
1150
|
-
this.
|
|
1151
|
-
description: "Display help information"
|
|
1152
|
-
}).addOptionHook("help", ({ cmd }) => {
|
|
1328
|
+
this.addBuiltinOption("-h, --help", "Display help information", ({ cmd }) => {
|
|
1153
1329
|
console.log(cmd.renderHelp());
|
|
1154
1330
|
process.exitCode = 0;
|
|
1155
1331
|
});
|
|
@@ -1181,23 +1357,17 @@ var Command = class _Command {
|
|
|
1181
1357
|
}
|
|
1182
1358
|
/** adds aliases to existing ones */
|
|
1183
1359
|
addAliases(...aliases) {
|
|
1184
|
-
const taken = this.parent ? valuesOf(this.parent.commands).flatMap((c) =>
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
}) : [];
|
|
1190
|
-
uniq(aliases.flat()).filter((a) => {
|
|
1191
|
-
return !this.aliases.includes(a) && a !== this.name;
|
|
1192
|
-
}).forEach((a) => {
|
|
1360
|
+
const taken = this.parent ? valuesOf(this.parent.commands).flatMap((c) => [
|
|
1361
|
+
c.name,
|
|
1362
|
+
...c.aliases
|
|
1363
|
+
]) : [];
|
|
1364
|
+
uniq(aliases.flat()).filter((a) => !this.aliases.includes(a) && a !== this.name).forEach((a) => {
|
|
1193
1365
|
if (taken.includes(a)) {
|
|
1194
1366
|
throw new Error(`Alias "${a}" is already used by a sibling command: ${findCommand(this.parent, a)?.name}`);
|
|
1195
1367
|
}
|
|
1196
1368
|
this.aliases.push(a);
|
|
1197
1369
|
});
|
|
1198
|
-
this.aliases.sort((a, b) =>
|
|
1199
|
-
return a.length - b.length;
|
|
1200
|
-
});
|
|
1370
|
+
this.aliases.sort((a, b) => a.length - b.length);
|
|
1201
1371
|
return this;
|
|
1202
1372
|
}
|
|
1203
1373
|
/** sets the command version */
|
|
@@ -1206,12 +1376,8 @@ var Command = class _Command {
|
|
|
1206
1376
|
if (findOption(this, "version")) {
|
|
1207
1377
|
return this;
|
|
1208
1378
|
}
|
|
1209
|
-
return this.
|
|
1210
|
-
|
|
1211
|
-
}).addOptionHook("version", ({ cmd }) => {
|
|
1212
|
-
console.log(getCommandAndAncestors(cmd).find((c) => {
|
|
1213
|
-
return c.version;
|
|
1214
|
-
})?.version);
|
|
1379
|
+
return this.addBuiltinOption("-V, --version", "Display semver version", ({ cmd }) => {
|
|
1380
|
+
console.log(getCommandAndAncestors(cmd).find((c) => c.version)?.version);
|
|
1215
1381
|
process.exitCode = 0;
|
|
1216
1382
|
});
|
|
1217
1383
|
}
|
|
@@ -1244,32 +1410,20 @@ var Command = class _Command {
|
|
|
1244
1410
|
const sub = this.createSubcommand(name);
|
|
1245
1411
|
const inherit = this.options;
|
|
1246
1412
|
sub.options.push(...inherit);
|
|
1247
|
-
const inheritHooks = this.hooks.filter((t) =>
|
|
1248
|
-
return inherit.some((i) => {
|
|
1249
|
-
return i.name === t.name;
|
|
1250
|
-
});
|
|
1251
|
-
});
|
|
1413
|
+
const inheritHooks = this.hooks.filter((t) => inherit.some((i) => i.name === t.name));
|
|
1252
1414
|
sub.hooks.push(...inheritHooks);
|
|
1253
|
-
const taken = valuesOf(this.commands).flatMap((c) =>
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
];
|
|
1258
|
-
});
|
|
1415
|
+
const taken = valuesOf(this.commands).flatMap((c) => [
|
|
1416
|
+
c.name,
|
|
1417
|
+
...c.aliases
|
|
1418
|
+
]);
|
|
1259
1419
|
if (taken.includes(name)) {
|
|
1260
|
-
throw new Error(`Command name "${getCommandAndAncestors(sub).map((c) => {
|
|
1261
|
-
return c.name;
|
|
1262
|
-
})}" is already used by this command or its aliases: ${taken.join(", ")}`);
|
|
1420
|
+
throw new Error(`Command name "${getCommandAndAncestors(sub).map((c) => c.name)}" is already used by this command or its aliases: ${taken.join(", ")}`);
|
|
1263
1421
|
}
|
|
1264
1422
|
const kebab = kebabCase(name);
|
|
1265
1423
|
const words = kebab.split("-");
|
|
1266
|
-
const initials = words.map((s) =>
|
|
1267
|
-
return s[0];
|
|
1268
|
-
}).join("");
|
|
1424
|
+
const initials = words.map((s) => s[0]).join("");
|
|
1269
1425
|
if (taken.includes(initials)) {
|
|
1270
|
-
const initials2 = words.map((s) =>
|
|
1271
|
-
return s[0] + s[1];
|
|
1272
|
-
}).join("");
|
|
1426
|
+
const initials2 = words.map((s) => s[0] + s[1]).join("");
|
|
1273
1427
|
if (!taken.includes(initials2)) {
|
|
1274
1428
|
sub.addAliases(initials2);
|
|
1275
1429
|
}
|
|
@@ -1320,59 +1474,46 @@ var Command = class _Command {
|
|
|
1320
1474
|
* adds command-line option with type inference. Parses format: `-s, --long [<value>|[value]|<value...>|[value...]]`
|
|
1321
1475
|
*/
|
|
1322
1476
|
addOption(flags, opts = {}) {
|
|
1323
|
-
const ins =
|
|
1324
|
-
const { short, long, name, argName } = parseOptionFlags(flags);
|
|
1477
|
+
const ins = createOption(flags, opts);
|
|
1325
1478
|
for (const opt of this.options) {
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
throw new Error(`Option short name, -${short} already in use by: ${opt.flags}`);
|
|
1331
|
-
}
|
|
1332
|
-
}
|
|
1333
|
-
ins.flags = flags;
|
|
1334
|
-
ins.short = short;
|
|
1335
|
-
ins.long = long;
|
|
1336
|
-
ins.name = name;
|
|
1337
|
-
ins.description = "";
|
|
1338
|
-
if (argName) {
|
|
1339
|
-
ins.type = "string";
|
|
1340
|
-
ins.argName = argName;
|
|
1341
|
-
if (flags.endsWith(">")) {
|
|
1342
|
-
if (flags.endsWith("...>")) {
|
|
1343
|
-
ins.required = true;
|
|
1344
|
-
ins.variadic = true;
|
|
1345
|
-
} else {
|
|
1346
|
-
ins.required = true;
|
|
1479
|
+
const builtin = builtinOptions.has(opt);
|
|
1480
|
+
if (opt.long === ins.long) {
|
|
1481
|
+
if (!builtin) {
|
|
1482
|
+
throw new Error(`Option long name, --${ins.long} already in use by: ${opt.flags}`);
|
|
1347
1483
|
}
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1484
|
+
this.options = this.options.filter((o) => o !== opt);
|
|
1485
|
+
this.hooks = this.hooks.filter((h) => h.name !== opt.name);
|
|
1486
|
+
continue;
|
|
1351
1487
|
}
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
if (ins.type === "boolean") {
|
|
1364
|
-
ins.defaultValue = /^(t(rue)?|y(es)?|1)$/i.test(process.env[ins.env]);
|
|
1365
|
-
} else if (ins.variadic) {
|
|
1366
|
-
ins.defaultValue = process.env[ins.env].replace(/\[]/, "").split(",").map((v) => {
|
|
1367
|
-
return v.trim();
|
|
1368
|
-
});
|
|
1369
|
-
} else {
|
|
1370
|
-
ins.defaultValue = process.env[ins.env];
|
|
1488
|
+
if (opt.short === ins.short) {
|
|
1489
|
+
if (!builtin) {
|
|
1490
|
+
throw new Error(`Option short name, -${ins.short} already in use by: ${opt.flags}`);
|
|
1491
|
+
}
|
|
1492
|
+
const longOnly = {
|
|
1493
|
+
...opt,
|
|
1494
|
+
short: void 0,
|
|
1495
|
+
flags: `--${opt.long}`
|
|
1496
|
+
};
|
|
1497
|
+
builtinOptions.add(longOnly);
|
|
1498
|
+
this.options = this.options.map((o) => o === opt ? longOnly : o);
|
|
1371
1499
|
}
|
|
1372
1500
|
}
|
|
1373
1501
|
this.options.push(ins);
|
|
1374
1502
|
return this;
|
|
1375
1503
|
}
|
|
1504
|
+
/** adds a built-in option with its hook; it goes without its short flag when an option already has that */
|
|
1505
|
+
addBuiltinOption(flags, description, action) {
|
|
1506
|
+
const ins = createOption(flags, {
|
|
1507
|
+
description
|
|
1508
|
+
});
|
|
1509
|
+
if (this.options.some((o) => o.short === ins.short)) {
|
|
1510
|
+
ins.short = void 0;
|
|
1511
|
+
ins.flags = `--${ins.long}`;
|
|
1512
|
+
}
|
|
1513
|
+
builtinOptions.add(ins);
|
|
1514
|
+
this.options.push(ins);
|
|
1515
|
+
return this.addOptionHook(ins.name, action);
|
|
1516
|
+
}
|
|
1376
1517
|
/**
|
|
1377
1518
|
* register an action to be invoked when an option is set to true or string value.
|
|
1378
1519
|
*
|
|
@@ -1382,38 +1523,41 @@ var Command = class _Command {
|
|
|
1382
1523
|
*/
|
|
1383
1524
|
addOptionHook(optionName, action) {
|
|
1384
1525
|
const def = findOption(this, optionName);
|
|
1526
|
+
if (!def) {
|
|
1527
|
+
throw new Error(`Cannot add a hook for option "${String(optionName)}": ${this.name} has no such option`);
|
|
1528
|
+
}
|
|
1385
1529
|
if (!def.group && /process\.exitCode ?= ?.+;?\s*}$/.test(action.toString())) {
|
|
1386
1530
|
def.group = "Command Options";
|
|
1387
1531
|
}
|
|
1388
1532
|
this.hooks.push({
|
|
1389
1533
|
name: optionName,
|
|
1390
|
-
predicate: setName(`has${upperFirst(optionName)}`, ({ opts }) =>
|
|
1391
|
-
return opts[optionName] !== void 0 && opts[optionName] !== false && !(Array.isArray(opts[optionName]) && opts[optionName].length === 0);
|
|
1392
|
-
}),
|
|
1534
|
+
predicate: setName(`has${upperFirst(optionName)}`, ({ opts }) => opts[optionName] !== void 0 && opts[optionName] !== false && !(Array.isArray(opts[optionName]) && opts[optionName].length === 0)),
|
|
1393
1535
|
action: setName(optionName, action)
|
|
1394
1536
|
});
|
|
1395
1537
|
return this;
|
|
1396
1538
|
}
|
|
1397
1539
|
/** parses command-line arguments with subcommand support and type-safe validation. */
|
|
1398
1540
|
parseArgv(argv = process.argv.slice(2)) {
|
|
1399
|
-
const
|
|
1400
|
-
if (
|
|
1401
|
-
|
|
1541
|
+
const subIndex = findSubcommandIndex(this, argv);
|
|
1542
|
+
if (subIndex !== -1) {
|
|
1543
|
+
const sub = findCommand(this, argv[subIndex]);
|
|
1544
|
+
return sub.parseArgv(argv.filter((_, i) => i !== subIndex));
|
|
1402
1545
|
}
|
|
1403
1546
|
argv = normalizeArgv(argv, this.options);
|
|
1547
|
+
const checked = checkOptionValues(argv, this.options);
|
|
1404
1548
|
const parsed = parseArgs({
|
|
1405
|
-
args: argv,
|
|
1406
|
-
options: Object.fromEntries(this.options.map((o) =>
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
}
|
|
1415
|
-
|
|
1416
|
-
|
|
1549
|
+
args: checked.argv,
|
|
1550
|
+
options: Object.fromEntries(this.options.map((o) => [
|
|
1551
|
+
o.name,
|
|
1552
|
+
{
|
|
1553
|
+
type: o.type,
|
|
1554
|
+
default: o.defaultValue,
|
|
1555
|
+
multiple: !!o.variadic,
|
|
1556
|
+
...o.short ? {
|
|
1557
|
+
short: o.short
|
|
1558
|
+
} : {}
|
|
1559
|
+
}
|
|
1560
|
+
])),
|
|
1417
1561
|
allowPositionals: true,
|
|
1418
1562
|
tokens: true,
|
|
1419
1563
|
strict: false,
|
|
@@ -1421,17 +1565,22 @@ var Command = class _Command {
|
|
|
1421
1565
|
});
|
|
1422
1566
|
collectVariadicOptionValues(parsed, this.options);
|
|
1423
1567
|
mergeOptionDefaults(parsed.values, this.options);
|
|
1424
|
-
parsed.values = objSortKeys(parsed.values, (a, b) =>
|
|
1425
|
-
return a[1] === false ? 1 : b[1] === false ? -1 : a[1] === true ? 1 : b[1] === true ? -1 : 0;
|
|
1426
|
-
});
|
|
1568
|
+
parsed.values = objSortKeys(parsed.values, (a, b) => a[1] === false ? 1 : b[1] === false ? -1 : a[1] === true ? 1 : b[1] === true ? -1 : 0);
|
|
1427
1569
|
const args = resolveArguments(parsed.positionals, this.arguments);
|
|
1428
|
-
const opts = filterObject(parsed.values, (value) =>
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1570
|
+
const opts = filterObject(parsed.values, (value) => value !== void 0);
|
|
1571
|
+
const rawNames = Object.fromEntries(parsed.tokens.flatMap((t) => t.kind === "option" ? [
|
|
1572
|
+
[
|
|
1573
|
+
t.name,
|
|
1574
|
+
t.rawName
|
|
1575
|
+
]
|
|
1576
|
+
] : []));
|
|
1577
|
+
const unexpected = this.arguments.some((a) => a.variadic) ? [] : parsed.positionals.slice(this.arguments.length);
|
|
1578
|
+
const errorList = checked.errors.concat(validateParsed(args, parsed.values, this.arguments, this.options, rawNames, checked.invalid) ?? []).concat(
|
|
1579
|
+
// a command with subcommands has no arguments of its own: the words after an unknown name were meant for it
|
|
1580
|
+
Object.keys(this.commands).length ? unexpected.slice(0, 1).map((word) => `Unknown command \`${word}\``) : unexpected.map((word) => `Unexpected argument \`${word}\``)
|
|
1581
|
+
);
|
|
1582
|
+
const errors = errorList.length ? errorList : void 0;
|
|
1583
|
+
const path = getCommandAncestors(this).map((c) => c.name);
|
|
1435
1584
|
const data = {
|
|
1436
1585
|
path,
|
|
1437
1586
|
name: this.name,
|
|
@@ -1441,42 +1590,43 @@ var Command = class _Command {
|
|
|
1441
1590
|
errors,
|
|
1442
1591
|
cmd: this
|
|
1443
1592
|
};
|
|
1444
|
-
const hooks = this.hooks.filter((t) =>
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1593
|
+
const hooks = this.hooks.filter((t) => t.predicate(data));
|
|
1594
|
+
const execute = /* @__PURE__ */ __name(async () => await timer([
|
|
1595
|
+
[
|
|
1596
|
+
...path,
|
|
1597
|
+
this.name
|
|
1598
|
+
].join(" "),
|
|
1599
|
+
this.summary || this.description
|
|
1600
|
+
], async (logger) => {
|
|
1448
1601
|
for (const hook of hooks) {
|
|
1449
|
-
await hook.action(
|
|
1602
|
+
await hook.action({
|
|
1603
|
+
...data,
|
|
1604
|
+
get logger() {
|
|
1605
|
+
return logger;
|
|
1606
|
+
}
|
|
1607
|
+
});
|
|
1450
1608
|
if (process.exitCode !== void 0) {
|
|
1451
1609
|
return;
|
|
1452
1610
|
}
|
|
1453
1611
|
}
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
opts,
|
|
1473
|
-
cmd: this,
|
|
1474
|
-
logger
|
|
1475
|
-
});
|
|
1476
|
-
}
|
|
1477
|
-
console.log(this.renderHelp());
|
|
1478
|
-
});
|
|
1479
|
-
}, "execute");
|
|
1612
|
+
if (errors) {
|
|
1613
|
+
errors.forEach((msg) => logger.error(C.red(msg)));
|
|
1614
|
+
process.exitCode = 1;
|
|
1615
|
+
return;
|
|
1616
|
+
}
|
|
1617
|
+
if (this.action) {
|
|
1618
|
+
return await this.action(...args, opts, {
|
|
1619
|
+
...data,
|
|
1620
|
+
args,
|
|
1621
|
+
opts,
|
|
1622
|
+
cmd: this,
|
|
1623
|
+
get logger() {
|
|
1624
|
+
return logger;
|
|
1625
|
+
}
|
|
1626
|
+
});
|
|
1627
|
+
}
|
|
1628
|
+
console.log(this.renderHelp());
|
|
1629
|
+
}), "execute");
|
|
1480
1630
|
return {
|
|
1481
1631
|
...data,
|
|
1482
1632
|
hooks,
|
|
@@ -1485,11 +1635,13 @@ var Command = class _Command {
|
|
|
1485
1635
|
}
|
|
1486
1636
|
/**
|
|
1487
1637
|
* sets the main action handler for this command, which is executed after any matching option hooks when the command is invoked.
|
|
1488
|
-
* The handler receives parsed arguments and options with correct typings.
|
|
1638
|
+
* The handler receives parsed arguments and options with correct typings. It may return a value (or a promise of one):
|
|
1639
|
+
* `execute()` of the parse result gives it back, typed.
|
|
1489
1640
|
*/
|
|
1490
1641
|
setAction(fn) {
|
|
1491
|
-
|
|
1492
|
-
|
|
1642
|
+
const typed = this;
|
|
1643
|
+
typed.action = setName(this.name, fn);
|
|
1644
|
+
return typed;
|
|
1493
1645
|
}
|
|
1494
1646
|
/** returns a new Command instance. Override this method in subclasses. */
|
|
1495
1647
|
createSubcommand(name) {
|