zod-args-parser 1.2.7 → 1.2.8
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 +13 -13
- package/lib/commonjs/autocomplete-scripts/{bash-autocomplete-script.js → bash-autocomplete-script.cjs} +2 -2
- package/lib/commonjs/autocomplete-scripts/bash-autocomplete-script.cjs.map +1 -0
- package/lib/commonjs/autocomplete-scripts/{powershell-autocomplete-script.js → powershell-autocomplete-script.cjs} +3 -3
- package/lib/commonjs/autocomplete-scripts/powershell-autocomplete-script.cjs.map +1 -0
- package/lib/commonjs/autocomplete-scripts/{zsh-autocomplete-script.js → zsh-autocomplete-script.cjs} +4 -4
- package/lib/commonjs/autocomplete-scripts/zsh-autocomplete-script.cjs.map +1 -0
- package/lib/commonjs/help-message/format-arguments.cjs +1 -0
- package/lib/commonjs/help-message/format-arguments.cjs.map +1 -0
- package/lib/commonjs/help-message/format-cli.cjs +1 -0
- package/lib/commonjs/help-message/format-cli.cjs.map +1 -0
- package/lib/commonjs/help-message/format-options.cjs +1 -0
- package/lib/commonjs/help-message/format-options.cjs.map +1 -0
- package/lib/commonjs/help-message/format-subcommands.cjs +1 -0
- package/lib/commonjs/help-message/format-subcommands.cjs.map +1 -0
- package/lib/commonjs/help-message/styles.cjs +1 -0
- package/lib/commonjs/help-message/styles.cjs.map +1 -0
- package/lib/commonjs/index.cjs +1 -0
- package/lib/commonjs/index.cjs.map +1 -0
- package/lib/commonjs/markdown/generate-markdown.cjs +1 -0
- package/lib/commonjs/markdown/generate-markdown.cjs.map +1 -0
- package/lib/commonjs/metadata/get-arguments-metadata.cjs +1 -0
- package/lib/commonjs/metadata/get-arguments-metadata.cjs.map +1 -0
- package/lib/commonjs/metadata/get-cli-metadata.cjs +1 -0
- package/lib/commonjs/metadata/{get-cli-metadata.js.map → get-cli-metadata.cjs.map} +1 -1
- package/lib/commonjs/metadata/get-options-metadata.cjs +1 -0
- package/lib/commonjs/metadata/get-options-metadata.cjs.map +1 -0
- package/lib/commonjs/metadata/get-subcommands-metadata.cjs +1 -0
- package/lib/commonjs/metadata/get-subcommands-metadata.cjs.map +1 -0
- package/lib/commonjs/parser/parse/parse.cjs +1 -0
- package/lib/commonjs/parser/parse/parse.cjs.map +1 -0
- package/lib/commonjs/parser/parse/parser-helpers.cjs +1 -0
- package/lib/commonjs/parser/parse/parser-helpers.cjs.map +1 -0
- package/lib/commonjs/parser/safe-parse.cjs +1 -0
- package/lib/commonjs/parser/safe-parse.cjs.map +1 -0
- package/lib/commonjs/parser/unsafe-parse.cjs +1 -0
- package/lib/commonjs/parser/unsafe-parse.cjs.map +1 -0
- package/lib/commonjs/parser/validate/validate-type.cjs.map +1 -0
- package/lib/commonjs/parser/validate/validate.cjs +1 -0
- package/lib/commonjs/parser/validate/validate.cjs.map +1 -0
- package/lib/commonjs/utilities.cjs +1 -0
- package/lib/commonjs/utilities.cjs.map +1 -0
- package/lib/commonjs/zod-utilities.cjs +1 -0
- package/lib/commonjs/zod-utilities.cjs.map +1 -0
- package/lib/module/autocomplete-scripts/{bash-autocomplete-script.js → bash-autocomplete-script.mjs} +2 -2
- package/lib/module/autocomplete-scripts/bash-autocomplete-script.mjs.map +1 -0
- package/lib/module/autocomplete-scripts/powershell-autocomplete-script.mjs +31 -0
- package/lib/module/autocomplete-scripts/powershell-autocomplete-script.mjs.map +1 -0
- package/lib/module/autocomplete-scripts/{zsh-autocomplete-script.js → zsh-autocomplete-script.mjs} +4 -4
- package/lib/module/autocomplete-scripts/zsh-autocomplete-script.mjs.map +1 -0
- package/lib/module/help-message/format-arguments.mjs +1 -0
- package/lib/module/help-message/format-arguments.mjs.map +1 -0
- package/lib/module/help-message/format-cli.mjs +1 -0
- package/lib/module/help-message/format-cli.mjs.map +1 -0
- package/lib/module/help-message/format-options.mjs +1 -0
- package/lib/module/help-message/format-options.mjs.map +1 -0
- package/lib/module/help-message/format-subcommands.mjs +1 -0
- package/lib/module/help-message/format-subcommands.mjs.map +1 -0
- package/lib/module/help-message/styles.mjs +1 -0
- package/lib/module/help-message/styles.mjs.map +1 -0
- package/lib/module/index.mjs +1 -0
- package/lib/module/index.mjs.map +1 -0
- package/lib/module/markdown/generate-markdown.mjs +1 -0
- package/lib/module/markdown/generate-markdown.mjs.map +1 -0
- package/lib/module/metadata/get-arguments-metadata.mjs +1 -0
- package/lib/module/metadata/get-arguments-metadata.mjs.map +1 -0
- package/lib/module/metadata/get-cli-metadata.mjs +1 -0
- package/lib/module/metadata/{get-cli-metadata.js.map → get-cli-metadata.mjs.map} +1 -1
- package/lib/module/metadata/get-options-metadata.mjs +1 -0
- package/lib/module/metadata/get-options-metadata.mjs.map +1 -0
- package/lib/module/metadata/get-subcommands-metadata.mjs +1 -0
- package/lib/module/metadata/get-subcommands-metadata.mjs.map +1 -0
- package/lib/module/parser/parse/parse.mjs +1 -0
- package/lib/module/parser/parse/parse.mjs.map +1 -0
- package/lib/module/parser/parse/parser-helpers.mjs +1 -0
- package/lib/module/parser/parse/parser-helpers.mjs.map +1 -0
- package/lib/module/parser/safe-parse.mjs +1 -0
- package/lib/module/parser/safe-parse.mjs.map +1 -0
- package/lib/module/parser/unsafe-parse.mjs +1 -0
- package/lib/module/parser/unsafe-parse.mjs.map +1 -0
- package/lib/module/parser/validate/validate-type.mjs.map +1 -0
- package/lib/module/parser/validate/validate.mjs +1 -0
- package/lib/module/parser/validate/validate.mjs.map +1 -0
- package/lib/module/utilities.mjs +1 -0
- package/lib/module/utilities.mjs.map +1 -0
- package/lib/module/zod-utilities.mjs +1 -0
- package/lib/module/zod-utilities.mjs.map +1 -0
- package/lib/typescript/autocomplete-scripts/bash-autocomplete-script.d.ts +1 -1
- package/lib/typescript/autocomplete-scripts/bash-autocomplete-script.d.ts.map +1 -1
- package/lib/typescript/autocomplete-scripts/powershell-autocomplete-script.d.ts +1 -1
- package/lib/typescript/autocomplete-scripts/powershell-autocomplete-script.d.ts.map +1 -1
- package/lib/typescript/autocomplete-scripts/zsh-autocomplete-script.d.ts +1 -1
- package/lib/typescript/autocomplete-scripts/zsh-autocomplete-script.d.ts.map +1 -1
- package/lib/typescript/help-message/format-arguments.d.ts +2 -2
- package/lib/typescript/help-message/format-arguments.d.ts.map +1 -1
- package/lib/typescript/help-message/format-cli.d.ts +5 -5
- package/lib/typescript/help-message/format-cli.d.ts.map +1 -1
- package/lib/typescript/help-message/format-options.d.ts +2 -2
- package/lib/typescript/help-message/format-options.d.ts.map +1 -1
- package/lib/typescript/help-message/format-subcommands.d.ts +2 -2
- package/lib/typescript/help-message/format-subcommands.d.ts.map +1 -1
- package/lib/typescript/help-message/styles.d.ts +14 -13
- package/lib/typescript/help-message/styles.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +10 -10
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/markdown/generate-markdown.d.ts +1 -1
- package/lib/typescript/markdown/generate-markdown.d.ts.map +1 -1
- package/lib/typescript/metadata/get-arguments-metadata.d.ts +1 -1
- package/lib/typescript/metadata/get-arguments-metadata.d.ts.map +1 -1
- package/lib/typescript/metadata/get-options-metadata.d.ts.map +1 -1
- package/lib/typescript/metadata/get-subcommands-metadata.d.ts.map +1 -1
- package/lib/typescript/parser/parse/parse-types.d.ts +5 -5
- package/lib/typescript/parser/parse/parse-types.d.ts.map +1 -1
- package/lib/typescript/parser/parse/parse.d.ts +2 -2
- package/lib/typescript/parser/parse/parse.d.ts.map +1 -1
- package/lib/typescript/parser/parse/parser-helpers.d.ts +7 -7
- package/lib/typescript/parser/parse/parser-helpers.d.ts.map +1 -1
- package/lib/typescript/parser/safe-parse.d.ts +2 -2
- package/lib/typescript/parser/safe-parse.d.ts.map +1 -1
- package/lib/typescript/parser/unsafe-parse.d.ts +2 -2
- package/lib/typescript/parser/unsafe-parse.d.ts.map +1 -1
- package/lib/typescript/parser/validate/validate-type.d.ts +4 -4
- package/lib/typescript/parser/validate/validate-type.d.ts.map +1 -1
- package/lib/typescript/parser/validate/validate.d.ts +4 -4
- package/lib/typescript/parser/validate/validate.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +19 -15
- package/lib/typescript/types.d.ts.map +1 -1
- package/lib/typescript/{utils.d.ts → utilities.d.ts} +9 -9
- package/lib/typescript/utilities.d.ts.map +1 -0
- package/lib/typescript/{zod-utils.d.ts → zod-utilities.d.ts} +5 -4
- package/lib/typescript/zod-utilities.d.ts.map +1 -0
- package/package.json +17 -18
- package/src/autocomplete-scripts/bash-autocomplete-script.ts +10 -10
- package/src/autocomplete-scripts/powershell-autocomplete-script.ts +17 -15
- package/src/autocomplete-scripts/zsh-autocomplete-script.ts +8 -5
- package/src/help-message/format-arguments.ts +20 -13
- package/src/help-message/format-cli.ts +65 -45
- package/src/help-message/format-options.ts +19 -12
- package/src/help-message/format-subcommands.ts +9 -9
- package/src/help-message/styles.ts +18 -18
- package/src/index.ts +21 -21
- package/src/markdown/generate-markdown.ts +55 -55
- package/src/metadata/get-arguments-metadata.ts +10 -10
- package/src/metadata/get-options-metadata.ts +8 -6
- package/src/metadata/get-subcommands-metadata.ts +3 -1
- package/src/parser/parse/parse-types.ts +5 -5
- package/src/parser/parse/parse.ts +67 -64
- package/src/parser/parse/parser-helpers.ts +24 -25
- package/src/parser/safe-parse.ts +24 -18
- package/src/parser/unsafe-parse.ts +34 -34
- package/src/parser/validate/validate-type.ts +4 -4
- package/src/parser/validate/validate.ts +17 -17
- package/src/types.ts +21 -17
- package/src/{utils.ts → utilities.ts} +44 -28
- package/src/{zod-utils.ts → zod-utilities.ts} +56 -32
- package/lib/commonjs/autocomplete-scripts/bash-autocomplete-script.js.map +0 -1
- package/lib/commonjs/autocomplete-scripts/powershell-autocomplete-script.js.map +0 -1
- package/lib/commonjs/autocomplete-scripts/zsh-autocomplete-script.js.map +0 -1
- package/lib/commonjs/help-message/format-arguments.js +0 -1
- package/lib/commonjs/help-message/format-arguments.js.map +0 -1
- package/lib/commonjs/help-message/format-cli.js +0 -1
- package/lib/commonjs/help-message/format-cli.js.map +0 -1
- package/lib/commonjs/help-message/format-options.js +0 -1
- package/lib/commonjs/help-message/format-options.js.map +0 -1
- package/lib/commonjs/help-message/format-subcommands.js +0 -1
- package/lib/commonjs/help-message/format-subcommands.js.map +0 -1
- package/lib/commonjs/help-message/styles.js +0 -1
- package/lib/commonjs/help-message/styles.js.map +0 -1
- package/lib/commonjs/index.js +0 -1
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/markdown/generate-markdown.js +0 -1
- package/lib/commonjs/markdown/generate-markdown.js.map +0 -1
- package/lib/commonjs/metadata/get-arguments-metadata.js +0 -1
- package/lib/commonjs/metadata/get-arguments-metadata.js.map +0 -1
- package/lib/commonjs/metadata/get-cli-metadata.js +0 -1
- package/lib/commonjs/metadata/get-options-metadata.js +0 -1
- package/lib/commonjs/metadata/get-options-metadata.js.map +0 -1
- package/lib/commonjs/metadata/get-subcommands-metadata.js +0 -1
- package/lib/commonjs/metadata/get-subcommands-metadata.js.map +0 -1
- package/lib/commonjs/parser/parse/parse.js +0 -1
- package/lib/commonjs/parser/parse/parse.js.map +0 -1
- package/lib/commonjs/parser/parse/parser-helpers.js +0 -1
- package/lib/commonjs/parser/parse/parser-helpers.js.map +0 -1
- package/lib/commonjs/parser/safe-parse.js +0 -1
- package/lib/commonjs/parser/safe-parse.js.map +0 -1
- package/lib/commonjs/parser/unsafe-parse.js +0 -1
- package/lib/commonjs/parser/unsafe-parse.js.map +0 -1
- package/lib/commonjs/parser/validate/validate-type.js.map +0 -1
- package/lib/commonjs/parser/validate/validate.js +0 -1
- package/lib/commonjs/parser/validate/validate.js.map +0 -1
- package/lib/commonjs/utils.js +0 -1
- package/lib/commonjs/utils.js.map +0 -1
- package/lib/commonjs/zod-utils.js +0 -1
- package/lib/commonjs/zod-utils.js.map +0 -1
- package/lib/module/autocomplete-scripts/bash-autocomplete-script.js.map +0 -1
- package/lib/module/autocomplete-scripts/powershell-autocomplete-script.js +0 -31
- package/lib/module/autocomplete-scripts/powershell-autocomplete-script.js.map +0 -1
- package/lib/module/autocomplete-scripts/zsh-autocomplete-script.js.map +0 -1
- package/lib/module/help-message/format-arguments.js +0 -1
- package/lib/module/help-message/format-arguments.js.map +0 -1
- package/lib/module/help-message/format-cli.js +0 -1
- package/lib/module/help-message/format-cli.js.map +0 -1
- package/lib/module/help-message/format-options.js +0 -1
- package/lib/module/help-message/format-options.js.map +0 -1
- package/lib/module/help-message/format-subcommands.js +0 -1
- package/lib/module/help-message/format-subcommands.js.map +0 -1
- package/lib/module/help-message/styles.js +0 -1
- package/lib/module/help-message/styles.js.map +0 -1
- package/lib/module/index.js +0 -1
- package/lib/module/index.js.map +0 -1
- package/lib/module/markdown/generate-markdown.js +0 -1
- package/lib/module/markdown/generate-markdown.js.map +0 -1
- package/lib/module/metadata/get-arguments-metadata.js +0 -1
- package/lib/module/metadata/get-arguments-metadata.js.map +0 -1
- package/lib/module/metadata/get-cli-metadata.js +0 -1
- package/lib/module/metadata/get-options-metadata.js +0 -1
- package/lib/module/metadata/get-options-metadata.js.map +0 -1
- package/lib/module/metadata/get-subcommands-metadata.js +0 -1
- package/lib/module/metadata/get-subcommands-metadata.js.map +0 -1
- package/lib/module/parser/parse/parse.js +0 -1
- package/lib/module/parser/parse/parse.js.map +0 -1
- package/lib/module/parser/parse/parser-helpers.js +0 -1
- package/lib/module/parser/parse/parser-helpers.js.map +0 -1
- package/lib/module/parser/safe-parse.js +0 -1
- package/lib/module/parser/safe-parse.js.map +0 -1
- package/lib/module/parser/unsafe-parse.js +0 -1
- package/lib/module/parser/unsafe-parse.js.map +0 -1
- package/lib/module/parser/validate/validate-type.js.map +0 -1
- package/lib/module/parser/validate/validate.js +0 -1
- package/lib/module/parser/validate/validate.js.map +0 -1
- package/lib/module/utils.js +0 -1
- package/lib/module/utils.js.map +0 -1
- package/lib/module/zod-utils.js +0 -1
- package/lib/module/zod-utils.js.map +0 -1
- package/lib/typescript/utils.d.ts.map +0 -1
- package/lib/typescript/zod-utils.d.ts.map +0 -1
- /package/lib/commonjs/parser/validate/{validate-type.js → validate-type.cjs} +0 -0
- /package/lib/module/parser/validate/{validate-type.js → validate-type.mjs} +0 -0
|
@@ -8,8 +8,8 @@ import type { Option, Subcommand } from "../../types.js";
|
|
|
8
8
|
* @param subcommandArr - An array of `Subcommand` objects to search within.
|
|
9
9
|
* @returns The matching `Subcommand` object if found; otherwise, `undefined`.
|
|
10
10
|
*/
|
|
11
|
-
export function findSubcommand(subCmdName: string | undefined,
|
|
12
|
-
return
|
|
11
|
+
export function findSubcommand(subCmdName: string | undefined, subcommandArray: Subcommand[]): Subcommand | undefined {
|
|
12
|
+
return subcommandArray.find(c => {
|
|
13
13
|
// match for undefined too
|
|
14
14
|
if (c.name === subCmdName) {
|
|
15
15
|
return true;
|
|
@@ -29,16 +29,16 @@ export function findSubcommand(subCmdName: string | undefined, subcommandArr: Su
|
|
|
29
29
|
* @param options - An array of `Option` objects to search through.
|
|
30
30
|
* @returns The matching `Option` object if found; otherwise, `undefined`.
|
|
31
31
|
*/
|
|
32
|
-
export function findOption(
|
|
33
|
-
const
|
|
34
|
-
const isNegative =
|
|
32
|
+
export function findOption(optionArgument: string, options: [Option, ...Option[]]): Option | undefined {
|
|
33
|
+
const validVariableNames = optionArgumentToVariableNames(optionArgument);
|
|
34
|
+
const isNegative = optionArgument.startsWith("--no-");
|
|
35
35
|
|
|
36
36
|
const option = options.find(o => {
|
|
37
|
-
if (
|
|
37
|
+
if (validVariableNames.has(o.name)) {
|
|
38
38
|
return true;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
if (isNegative &&
|
|
41
|
+
if (isNegative && validVariableNames.has(negateOption(o.name))) {
|
|
42
42
|
return true;
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -46,11 +46,11 @@ export function findOption(optionArg: string, options: [Option, ...Option[]]): O
|
|
|
46
46
|
return false;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
if (o.aliases.some(a =>
|
|
49
|
+
if (o.aliases.some(a => validVariableNames.has(a))) {
|
|
50
50
|
return true;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
if (isNegative && o.aliases.map(negateOption).some(a =>
|
|
53
|
+
if (isNegative && o.aliases.map(alias => negateOption(alias)).some(a => validVariableNames.has(a))) {
|
|
54
54
|
return true;
|
|
55
55
|
}
|
|
56
56
|
|
|
@@ -61,25 +61,24 @@ export function findOption(optionArg: string, options: [Option, ...Option[]]): O
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
/** - Decouple flags E.g. `-rf` -> `-r, -f` */
|
|
64
|
-
export function decoupleFlags(
|
|
64
|
+
export function decoupleFlags(arguments_: string[]): string[] {
|
|
65
65
|
const flagsRe = /^-[a-z0-9]{2,}$/i;
|
|
66
66
|
|
|
67
67
|
const result = [];
|
|
68
|
-
for (
|
|
69
|
-
const
|
|
70
|
-
const isCoupled = flagsRe.test(arg);
|
|
68
|
+
for (const argument of arguments_) {
|
|
69
|
+
const isCoupled = flagsRe.test(argument);
|
|
71
70
|
|
|
72
71
|
if (!isCoupled) {
|
|
73
|
-
result.push(
|
|
72
|
+
result.push(argument);
|
|
74
73
|
continue;
|
|
75
74
|
}
|
|
76
75
|
|
|
77
|
-
const
|
|
78
|
-
.
|
|
76
|
+
const decoupledArray = argument
|
|
77
|
+
.slice(1)
|
|
79
78
|
.split("")
|
|
80
79
|
.map(c => "-" + c);
|
|
81
80
|
|
|
82
|
-
result.push(...
|
|
81
|
+
result.push(...decoupledArray);
|
|
83
82
|
}
|
|
84
83
|
|
|
85
84
|
return result;
|
|
@@ -90,12 +89,12 @@ export function decoupleFlags(args: string[]): string[] {
|
|
|
90
89
|
*
|
|
91
90
|
* @param name - Should start with `'--'` or `'-'`
|
|
92
91
|
*/
|
|
93
|
-
export function
|
|
92
|
+
export function optionArgumentToVariableNames(name: string): Set<string> {
|
|
94
93
|
if (!name.startsWith("-")) {
|
|
95
94
|
throw new Error(`[parseArgOptionName] Invalid arg name: ${name}`, { cause: "zod-args-parser" });
|
|
96
95
|
}
|
|
97
96
|
|
|
98
|
-
name = name.startsWith("--") ? name.
|
|
97
|
+
name = name.startsWith("--") ? name.slice(2) : name.slice(1); // remove prefix
|
|
99
98
|
name = name.toLowerCase(); // lowercase
|
|
100
99
|
|
|
101
100
|
const results = new Set<string>();
|
|
@@ -118,22 +117,22 @@ export function optionArgToVarNames(name: string): Set<string> {
|
|
|
118
117
|
}
|
|
119
118
|
|
|
120
119
|
/** - Check if an arg string is a short arg. E.g. `-i` -> `true` */
|
|
121
|
-
export function
|
|
120
|
+
export function isFlagArgument(name: string): boolean {
|
|
122
121
|
return /^-[A-Za-z]$/.test(name);
|
|
123
122
|
}
|
|
124
123
|
|
|
125
124
|
/** - Check if an arg string is a long arg. E.g. `--input-dir` -> `true` */
|
|
126
|
-
function
|
|
125
|
+
function isLongArgument(name: string): boolean {
|
|
127
126
|
return /^--[A-Za-z-]+[A-Za-z0-9]$/.test(name);
|
|
128
127
|
}
|
|
129
128
|
|
|
130
129
|
/** - Check if an arg string is an options arg. E.g. `--input-dir` -> `true` , `-i` -> `true` */
|
|
131
|
-
export function
|
|
130
|
+
export function isOptionArgument(name: string | boolean): boolean {
|
|
132
131
|
if (typeof name !== "string") {
|
|
133
132
|
return false;
|
|
134
133
|
}
|
|
135
134
|
|
|
136
|
-
return
|
|
135
|
+
return isFlagArgument(name) || isLongArgument(name);
|
|
137
136
|
}
|
|
138
137
|
|
|
139
138
|
/**
|
|
@@ -149,7 +148,7 @@ export function negateOption(name: string): string {
|
|
|
149
148
|
}
|
|
150
149
|
|
|
151
150
|
/** - Reverse of `transformArg`. E.g. `InputDir` -> `--input-dir` , `i` -> `-i` */
|
|
152
|
-
export function
|
|
151
|
+
export function transformOptionToArgument(name: string): string {
|
|
153
152
|
// single letter option name
|
|
154
153
|
if (name.length === 1) {
|
|
155
154
|
return `-${name.toLowerCase()}`;
|
|
@@ -162,6 +161,6 @@ export function transformOptionToArg(name: string): string {
|
|
|
162
161
|
}
|
|
163
162
|
|
|
164
163
|
// camelCase, PascalCase
|
|
165
|
-
name = name.replace(/[A-Z]/g, (
|
|
164
|
+
name = name.replace(/[A-Z]/g, (match, index: number) => (index > 0 ? "-" + match : match)); // add "-" before camel case letters except for the first letter
|
|
166
165
|
return `--${name.toLowerCase()}`;
|
|
167
166
|
}
|
package/src/parser/safe-parse.ts
CHANGED
|
@@ -6,19 +6,19 @@ import type { Cli, NoSubcommand, PrintMethods, SafeParseResult, Subcommand } fro
|
|
|
6
6
|
|
|
7
7
|
export function safeParse<T extends Subcommand[], U extends Cli>(
|
|
8
8
|
argsv: string[],
|
|
9
|
-
...
|
|
9
|
+
...parameters: [U, ...T]
|
|
10
10
|
): SafeParseResult<[...T, NoSubcommand & U]> {
|
|
11
|
-
const cliOptions = ("cliName" in
|
|
12
|
-
const
|
|
11
|
+
const cliOptions = ("cliName" in parameters[0] ? parameters[0] : {}) as U;
|
|
12
|
+
const subcommandArray = parameters as Subcommand[];
|
|
13
13
|
|
|
14
14
|
type PrintTypes = PrintMethods<T[number]["name"]>;
|
|
15
15
|
type PrintCli = PrintTypes["printCliHelp"];
|
|
16
16
|
type PrintSubcommand = PrintTypes["printSubcommandHelp"];
|
|
17
17
|
|
|
18
|
-
const printCliHelp: PrintCli = style => help.printCliHelp(
|
|
18
|
+
const printCliHelp: PrintCli = style => help.printCliHelp(parameters, style);
|
|
19
19
|
|
|
20
20
|
const printSubcommandHelp: PrintSubcommand = (subCmdName, style) => {
|
|
21
|
-
const subcommand = findSubcommand(subCmdName,
|
|
21
|
+
const subcommand = findSubcommand(subCmdName, subcommandArray);
|
|
22
22
|
if (!subcommand) {
|
|
23
23
|
return console.error(`Cannot print help for subcommand "${subCmdName}" as it does not exist`);
|
|
24
24
|
}
|
|
@@ -27,7 +27,7 @@ export function safeParse<T extends Subcommand[], U extends Cli>(
|
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
try {
|
|
30
|
-
const data = unsafeParse(argsv, ...
|
|
30
|
+
const data = unsafeParse(argsv, ...parameters);
|
|
31
31
|
// @ts-expect-error The operand of a 'delete' operator must be optional.
|
|
32
32
|
delete data.printCliHelp;
|
|
33
33
|
// @ts-expect-errorThe operand of a 'delete' operator must be optional.
|
|
@@ -39,11 +39,14 @@ export function safeParse<T extends Subcommand[], U extends Cli>(
|
|
|
39
39
|
printCliHelp,
|
|
40
40
|
printSubcommandHelp,
|
|
41
41
|
} as SafeParseResult<[...T, NoSubcommand & U]>;
|
|
42
|
-
} catch (
|
|
43
|
-
if (!(
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (!(error instanceof Error) || error.cause !== "zod-args-parser") {
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
|
|
44
47
|
return {
|
|
45
48
|
success: false,
|
|
46
|
-
error
|
|
49
|
+
error,
|
|
47
50
|
printCliHelp,
|
|
48
51
|
printSubcommandHelp,
|
|
49
52
|
} as SafeParseResult<[...T, NoSubcommand & U]>;
|
|
@@ -52,19 +55,19 @@ export function safeParse<T extends Subcommand[], U extends Cli>(
|
|
|
52
55
|
|
|
53
56
|
export async function safeParseAsync<T extends Subcommand[], U extends Cli>(
|
|
54
57
|
argsv: string[],
|
|
55
|
-
...
|
|
58
|
+
...parameters: [U, ...T]
|
|
56
59
|
): Promise<SafeParseResult<[...T, NoSubcommand & U]>> {
|
|
57
|
-
const cliOptions = ("cliName" in
|
|
58
|
-
const
|
|
60
|
+
const cliOptions = ("cliName" in parameters[0] ? parameters[0] : {}) as U;
|
|
61
|
+
const subcommandArray = parameters as Subcommand[];
|
|
59
62
|
|
|
60
63
|
type PrintTypes = PrintMethods<T[number]["name"]>;
|
|
61
64
|
type PrintCli = PrintTypes["printCliHelp"];
|
|
62
65
|
type PrintSubcommand = PrintTypes["printSubcommandHelp"];
|
|
63
66
|
|
|
64
|
-
const printCliHelp: PrintCli = style => help.printCliHelp(
|
|
67
|
+
const printCliHelp: PrintCli = style => help.printCliHelp(parameters, style);
|
|
65
68
|
|
|
66
69
|
const printSubcommandHelp: PrintSubcommand = (subCmdName, style) => {
|
|
67
|
-
const subcommand = findSubcommand(subCmdName,
|
|
70
|
+
const subcommand = findSubcommand(subCmdName, subcommandArray);
|
|
68
71
|
if (!subcommand) {
|
|
69
72
|
return console.error(`Cannot print help for subcommand "${subCmdName}" as it does not exist`);
|
|
70
73
|
}
|
|
@@ -73,7 +76,7 @@ export async function safeParseAsync<T extends Subcommand[], U extends Cli>(
|
|
|
73
76
|
};
|
|
74
77
|
|
|
75
78
|
try {
|
|
76
|
-
const data = await unsafeParseAsync(argsv, ...
|
|
79
|
+
const data = await unsafeParseAsync(argsv, ...parameters);
|
|
77
80
|
// @ts-expect-error The operand of a 'delete' operator must be optional.
|
|
78
81
|
delete data.printCliHelp;
|
|
79
82
|
// @ts-expect-errorThe operand of a 'delete' operator must be optional.
|
|
@@ -85,11 +88,14 @@ export async function safeParseAsync<T extends Subcommand[], U extends Cli>(
|
|
|
85
88
|
printCliHelp,
|
|
86
89
|
printSubcommandHelp,
|
|
87
90
|
} as SafeParseResult<[...T, NoSubcommand & U]>;
|
|
88
|
-
} catch (
|
|
89
|
-
if (!(
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (!(error instanceof Error) || error.cause !== "zod-args-parser") {
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
|
|
90
96
|
return {
|
|
91
97
|
success: false,
|
|
92
|
-
error
|
|
98
|
+
error,
|
|
93
99
|
printCliHelp,
|
|
94
100
|
printSubcommandHelp,
|
|
95
101
|
} as SafeParseResult<[...T, NoSubcommand & U]>;
|
|
@@ -1,51 +1,51 @@
|
|
|
1
1
|
import * as help from "../help-message/format-cli.js";
|
|
2
|
+
import { parse } from "./parse/parse.js";
|
|
2
3
|
import { findSubcommand } from "./parse/parser-helpers.js";
|
|
3
4
|
import { validate } from "./validate/validate.js";
|
|
4
|
-
import { parse } from "./parse/parse.js";
|
|
5
5
|
|
|
6
|
-
import type { Cli,
|
|
6
|
+
import type { Cli, HelpMessageStyle, NoSubcommand, Subcommand, UnsafeParseResult } from "../types.js";
|
|
7
7
|
|
|
8
8
|
export function unsafeParse<T extends Subcommand[], U extends Cli>(
|
|
9
9
|
argv: string[],
|
|
10
|
-
...
|
|
10
|
+
...parameters: [U, ...T]
|
|
11
11
|
): UnsafeParseResult<[...T, NoSubcommand & U]> {
|
|
12
|
-
const cliOptions = ("cliName" in
|
|
13
|
-
const
|
|
12
|
+
const cliOptions = ("cliName" in parameters[0] ? parameters[0] : {}) as U;
|
|
13
|
+
const subcommandArray = parameters as unknown as T;
|
|
14
14
|
|
|
15
15
|
// Parse
|
|
16
|
-
const parsedData = parse(argv, ...
|
|
16
|
+
const parsedData = parse(argv, ...parameters);
|
|
17
17
|
|
|
18
|
-
const
|
|
19
|
-
if (!
|
|
18
|
+
const subcommandObject = findSubcommand(parsedData.subcommand, subcommandArray);
|
|
19
|
+
if (!subcommandObject) {
|
|
20
20
|
throw new Error(`Subcommand "${parsedData.subcommand}" does not exist`, { cause: "zod-args-parser" });
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
// Fire preValidation hook
|
|
24
|
-
if (
|
|
25
|
-
|
|
24
|
+
if (subcommandObject.preValidation) {
|
|
25
|
+
subcommandObject.preValidation(parsedData);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// Validate
|
|
29
29
|
const validateResult = validate(parsedData);
|
|
30
30
|
|
|
31
31
|
Object.assign(validateResult, {
|
|
32
|
-
printCliHelp(style?: Partial<
|
|
33
|
-
help.printCliHelp(
|
|
32
|
+
printCliHelp(style?: Partial<HelpMessageStyle>) {
|
|
33
|
+
help.printCliHelp(parameters, style);
|
|
34
34
|
},
|
|
35
|
-
printSubcommandHelp(subCmdName: string, style?: Partial<
|
|
36
|
-
const
|
|
37
|
-
if (!
|
|
35
|
+
printSubcommandHelp(subCmdName: string, style?: Partial<HelpMessageStyle>) {
|
|
36
|
+
const subcommandObject = findSubcommand(subCmdName, subcommandArray);
|
|
37
|
+
if (!subcommandObject) {
|
|
38
38
|
console.error(`Cannot print help for subcommand "${subCmdName}" as it does not exist`);
|
|
39
39
|
return;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
help.printSubcommandHelp(
|
|
42
|
+
help.printSubcommandHelp(subcommandObject, style, cliOptions.cliName);
|
|
43
43
|
},
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
// Fire action
|
|
47
|
-
if (
|
|
48
|
-
|
|
47
|
+
if (subcommandObject.action) {
|
|
48
|
+
subcommandObject.action(validateResult);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
return validateResult as UnsafeParseResult<[...T, NoSubcommand & U]>;
|
|
@@ -53,45 +53,45 @@ export function unsafeParse<T extends Subcommand[], U extends Cli>(
|
|
|
53
53
|
|
|
54
54
|
export async function unsafeParseAsync<T extends Subcommand[], U extends Cli>(
|
|
55
55
|
argv: string[],
|
|
56
|
-
...
|
|
56
|
+
...parameters: [U, ...T]
|
|
57
57
|
): Promise<UnsafeParseResult<[...T, NoSubcommand & U]>> {
|
|
58
|
-
const cliOptions = ("cliName" in
|
|
59
|
-
const
|
|
58
|
+
const cliOptions = ("cliName" in parameters[0] ? parameters[0] : {}) as U;
|
|
59
|
+
const subcommandArray = parameters as unknown as T;
|
|
60
60
|
|
|
61
61
|
// Parse
|
|
62
|
-
const parsedData = parse(argv, ...
|
|
62
|
+
const parsedData = parse(argv, ...parameters);
|
|
63
63
|
|
|
64
|
-
const
|
|
65
|
-
if (!
|
|
64
|
+
const subcommandObject = findSubcommand(parsedData.subcommand, subcommandArray);
|
|
65
|
+
if (!subcommandObject) {
|
|
66
66
|
throw new Error(`Subcommand "${parsedData.subcommand}" does not exist`, { cause: "zod-args-parser" });
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
// Fire preValidation hook
|
|
70
|
-
if (
|
|
71
|
-
await
|
|
70
|
+
if (subcommandObject.preValidation) {
|
|
71
|
+
await subcommandObject.preValidation(parsedData);
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
// Validate
|
|
75
75
|
const validateResult = validate(parsedData);
|
|
76
76
|
|
|
77
77
|
Object.assign(validateResult, {
|
|
78
|
-
printCliHelp(style?: Partial<
|
|
79
|
-
help.printCliHelp(
|
|
78
|
+
printCliHelp(style?: Partial<HelpMessageStyle>) {
|
|
79
|
+
help.printCliHelp(parameters, style);
|
|
80
80
|
},
|
|
81
|
-
printSubcommandHelp(subCmdName: string, style?: Partial<
|
|
82
|
-
const
|
|
83
|
-
if (!
|
|
81
|
+
printSubcommandHelp(subCmdName: string, style?: Partial<HelpMessageStyle>) {
|
|
82
|
+
const subcommandObject = findSubcommand(subCmdName, subcommandArray);
|
|
83
|
+
if (!subcommandObject) {
|
|
84
84
|
console.error(`Cannot print help for subcommand "${subCmdName}" as it does not exist`);
|
|
85
85
|
return;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
help.printSubcommandHelp(
|
|
88
|
+
help.printSubcommandHelp(subcommandObject, style, cliOptions.cliName);
|
|
89
89
|
},
|
|
90
90
|
});
|
|
91
91
|
|
|
92
92
|
// Fire action
|
|
93
|
-
if (
|
|
94
|
-
await
|
|
93
|
+
if (subcommandObject.action) {
|
|
94
|
+
await subcommandObject.action(validateResult);
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
return validateResult as UnsafeParseResult<[...T, NoSubcommand & U]>;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { Argument, Option, Prettify, Schema, Subcommand, ToOptional, ZodInferOutput } from "../../types.js";
|
|
2
2
|
import type { ParseResult } from "../parse/parse-types.js";
|
|
3
3
|
|
|
4
|
-
type
|
|
4
|
+
type OptionsArray2RecordType<T extends Option[] | undefined> = T extends Option[]
|
|
5
5
|
? ToOptional<{ [K in T[number]["name"]]: ZodInferOutput<Extract<T[number], { name: K }>["type"]> }>
|
|
6
6
|
: object;
|
|
7
7
|
|
|
8
|
-
type
|
|
8
|
+
type ArgumentsArray2ArrayType<T extends Argument[] | undefined> = T extends Argument[]
|
|
9
9
|
? { [K in keyof T]: T[K] extends { type: Schema } ? ZodInferOutput<T[K]["type"]> : never }
|
|
10
10
|
: never;
|
|
11
11
|
|
|
@@ -13,9 +13,9 @@ export type ValidateResult<S extends Partial<Subcommand>[]> = {
|
|
|
13
13
|
[K in keyof S]: Prettify<
|
|
14
14
|
{
|
|
15
15
|
subcommand: S[K]["name"] extends string ? S[K]["name"] : undefined;
|
|
16
|
-
arguments:
|
|
16
|
+
arguments: ArgumentsArray2ArrayType<S[K]["arguments"]>;
|
|
17
17
|
positional: S[K]["allowPositional"] extends true ? string[] : never;
|
|
18
18
|
ctx: ParseResult<S>;
|
|
19
|
-
} &
|
|
19
|
+
} & OptionsArray2RecordType<S[K]["options"]>
|
|
20
20
|
>;
|
|
21
21
|
}[number];
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { prettifyError } from "zod/v4";
|
|
1
|
+
import { prettifyError } from "zod/v4/core";
|
|
2
2
|
|
|
3
|
-
import { generateOrdinalSuffix, stringToBoolean } from "../../
|
|
4
|
-
import { isBooleanSchema, safeParseSchema } from "../../zod-
|
|
3
|
+
import { generateOrdinalSuffix, stringToBoolean } from "../../utilities.js";
|
|
4
|
+
import { isBooleanSchema, safeParseSchema } from "../../zod-utilities.js";
|
|
5
5
|
|
|
6
|
-
import type {
|
|
6
|
+
import type { ParsedContext } from "../parse/parse-types.js";
|
|
7
7
|
|
|
8
8
|
/** The return result object temporarily type. used inside the `parse` function */
|
|
9
|
-
type
|
|
9
|
+
type ResultsTemporaryType = Record<string, unknown> & {
|
|
10
10
|
subcommand: string | undefined;
|
|
11
11
|
positional?: string[];
|
|
12
12
|
arguments?: unknown[];
|
|
13
|
-
ctx:
|
|
13
|
+
ctx: ParsedContext;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
export function validate(parsedData:
|
|
17
|
-
const results:
|
|
16
|
+
export function validate(parsedData: ParsedContext) {
|
|
17
|
+
const results: ResultsTemporaryType = {
|
|
18
18
|
subcommand: parsedData.subcommand,
|
|
19
19
|
positional: parsedData.positional,
|
|
20
20
|
ctx: parsedData,
|
|
@@ -33,14 +33,14 @@ export function validate(parsedData: ParseCtx) {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
const
|
|
37
|
-
if (!
|
|
38
|
-
throw new Error(`Invalid value "${rawValue}" for "${flag}": ${prettifyError(
|
|
36
|
+
const safeParseResult = safeParseSchema(schema, optionsValue);
|
|
37
|
+
if (!safeParseResult.success) {
|
|
38
|
+
throw new Error(`Invalid value "${rawValue}" for "${flag}": ${prettifyError(safeParseResult.error)}`, {
|
|
39
39
|
cause: "zod-args-parser",
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
results[optionName] =
|
|
43
|
+
results[optionName] = safeParseResult.data;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
// validate arguments
|
|
@@ -48,17 +48,17 @@ export function validate(parsedData: ParseCtx) {
|
|
|
48
48
|
if (!results.arguments) results.arguments = [];
|
|
49
49
|
|
|
50
50
|
for (const { schema, rawValue } of parsedData.arguments) {
|
|
51
|
-
const
|
|
51
|
+
const argumentValue = rawValue && isBooleanSchema(schema) ? stringToBoolean(rawValue) : rawValue;
|
|
52
52
|
|
|
53
|
-
const
|
|
54
|
-
if (!
|
|
53
|
+
const safeParseResult = safeParseSchema(schema, argumentValue);
|
|
54
|
+
if (!safeParseResult.success) {
|
|
55
55
|
throw new Error(
|
|
56
|
-
`The ${generateOrdinalSuffix(results.arguments.length)} argument "${rawValue}" is invalid: ${prettifyError(
|
|
56
|
+
`The ${generateOrdinalSuffix(results.arguments.length)} argument "${rawValue}" is invalid: ${prettifyError(safeParseResult.error)}`,
|
|
57
57
|
{ cause: "zod-args-parser" },
|
|
58
58
|
);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
results.arguments.push(
|
|
61
|
+
results.arguments.push(safeParseResult.data);
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
package/src/types.ts
CHANGED
|
@@ -85,7 +85,7 @@ export interface Subcommand {
|
|
|
85
85
|
* const helpCommand = createSubcommand({ name: "help", options: [...] });
|
|
86
86
|
* helpCommand.setAction(res => console.log(res));
|
|
87
87
|
*/
|
|
88
|
-
action?: (
|
|
88
|
+
action?: (data?: any) => any;
|
|
89
89
|
|
|
90
90
|
/**
|
|
91
91
|
* - The preValidation hook is executed before the action.
|
|
@@ -95,7 +95,7 @@ export interface Subcommand {
|
|
|
95
95
|
* const helpCommand = createSubcommand({ name: "help", options: [...] });
|
|
96
96
|
* helpCommand.setPreValidationHook(ctx => console.log(ctx));
|
|
97
97
|
*/
|
|
98
|
-
preValidation?: (
|
|
98
|
+
preValidation?: (context?: any) => any;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
export type Cli = Prettify<
|
|
@@ -171,10 +171,10 @@ export interface Argument {
|
|
|
171
171
|
example?: string;
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
export type
|
|
174
|
+
export type ColorFunctionType = (...text: unknown[]) => string;
|
|
175
175
|
|
|
176
176
|
/** - The colors to use for the help message. */
|
|
177
|
-
export type
|
|
177
|
+
export type HelpMessageStyle = Record<
|
|
178
178
|
| "title"
|
|
179
179
|
| "description"
|
|
180
180
|
| "default"
|
|
@@ -186,7 +186,7 @@ export type HelpMsgStyle = Record<
|
|
|
186
186
|
| "argument"
|
|
187
187
|
| "placeholder"
|
|
188
188
|
| "punctuation",
|
|
189
|
-
|
|
189
|
+
ColorFunctionType
|
|
190
190
|
>;
|
|
191
191
|
|
|
192
192
|
/**
|
|
@@ -264,8 +264,8 @@ export type ToOptional<T> = Prettify<
|
|
|
264
264
|
export type NoSubcommand = { name: undefined };
|
|
265
265
|
|
|
266
266
|
export type PrintMethods<N extends Subcommand["name"]> = {
|
|
267
|
-
printCliHelp: (style?: Partial<
|
|
268
|
-
printSubcommandHelp: (subcommand: LiteralUnion<NonNullable<N>>, style?: Partial<
|
|
267
|
+
printCliHelp: (style?: Partial<HelpMessageStyle>) => void;
|
|
268
|
+
printSubcommandHelp: (subcommand: LiteralUnion<NonNullable<N>>, style?: Partial<HelpMessageStyle>) => void;
|
|
269
269
|
};
|
|
270
270
|
|
|
271
271
|
export type UnsafeParseResult<S extends Partial<Subcommand>[]> =
|
|
@@ -281,18 +281,22 @@ export type SafeParseResult<S extends Partial<Subcommand>[]> =
|
|
|
281
281
|
PrintMethods<NonNullable<S[number]["name"]>>
|
|
282
282
|
>;
|
|
283
283
|
|
|
284
|
-
export type
|
|
285
|
-
setAction: (actions: (
|
|
286
|
-
setPreValidationHook: (
|
|
284
|
+
export type ActionsFunctions<T extends Subcommand | Cli> = {
|
|
285
|
+
setAction: (actions: (data: UnsafeParseResult<[T]>) => void) => void;
|
|
286
|
+
setPreValidationHook: (hook: (context: ParseResult<[T]>) => void) => void;
|
|
287
287
|
};
|
|
288
288
|
|
|
289
289
|
/** - Combine `name` and `aliases` to a `string[]` */
|
|
290
|
-
type
|
|
290
|
+
type MapNameAndAliasesToStringArray<T extends { name?: string; aliases?: string[] }[]> = T extends [
|
|
291
291
|
infer First extends Subcommand,
|
|
292
292
|
...infer Rest,
|
|
293
293
|
]
|
|
294
294
|
? Rest extends { name?: string; aliases?: string[] }[]
|
|
295
|
-
? [
|
|
295
|
+
? [
|
|
296
|
+
First["name"],
|
|
297
|
+
...(First["aliases"] extends string[] ? First["aliases"] : []),
|
|
298
|
+
...MapNameAndAliasesToStringArray<Rest>,
|
|
299
|
+
]
|
|
296
300
|
: [First["name"], ...(First["aliases"] extends string[] ? First["aliases"] : [])]
|
|
297
301
|
: [];
|
|
298
302
|
|
|
@@ -300,11 +304,11 @@ type MapNameAndAliases2StrArr<T extends { name?: string; aliases?: string[] }[]>
|
|
|
300
304
|
* - Find duplicated items in an array and return it
|
|
301
305
|
* - Return `false` if not found
|
|
302
306
|
*/
|
|
303
|
-
type
|
|
307
|
+
type IsDuplicatesInArray<Input extends any[]> = Input extends [infer Item, ...infer Rest]
|
|
304
308
|
? Rest extends any[]
|
|
305
309
|
? Item extends Rest[number]
|
|
306
310
|
? Item
|
|
307
|
-
:
|
|
311
|
+
: IsDuplicatesInArray<Rest>
|
|
308
312
|
: false
|
|
309
313
|
: false;
|
|
310
314
|
|
|
@@ -314,7 +318,7 @@ type IsDuplicatesInArr<Input extends any[]> = Input extends [infer Item, ...infe
|
|
|
314
318
|
* - Return `undefined` if not found
|
|
315
319
|
*/
|
|
316
320
|
export type CheckDuplicatedOptions<T extends { options?: Option[] }> = T["options"] extends infer O extends Option[]
|
|
317
|
-
?
|
|
321
|
+
? IsDuplicatesInArray<MapNameAndAliasesToStringArray<O>> extends infer Name extends string
|
|
318
322
|
? `>>> Error: Duplicated Options. Check the options with the name \`${Name}\` <<<`
|
|
319
323
|
: undefined
|
|
320
324
|
: undefined;
|
|
@@ -325,7 +329,7 @@ export type CheckDuplicatedOptions<T extends { options?: Option[] }> = T["option
|
|
|
325
329
|
* - Return the `undefined` if no error
|
|
326
330
|
*/
|
|
327
331
|
type CheckDuplicatedSubcommands<T extends Partial<Subcommand>[]> =
|
|
328
|
-
|
|
332
|
+
IsDuplicatesInArray<MapNameAndAliasesToStringArray<T>> extends infer Name extends string
|
|
329
333
|
? `>>> Error: Duplicated Subcommand. Check the subcommands with the name \`${Name}\` <<<`
|
|
330
334
|
: undefined;
|
|
331
335
|
|
|
@@ -336,7 +340,7 @@ type CheckDuplicatedSubcommands<T extends Partial<Subcommand>[]> =
|
|
|
336
340
|
*/
|
|
337
341
|
export type CheckDuplicatedArguments<T extends { arguments?: Argument[] }> = T["arguments"] extends infer A extends
|
|
338
342
|
Argument[]
|
|
339
|
-
?
|
|
343
|
+
? IsDuplicatesInArray<MapNameAndAliasesToStringArray<A>> extends infer Name extends string
|
|
340
344
|
? `>>> Error: Duplicated Arguments. Check the arguments with the name \`${Name}\` <<<`
|
|
341
345
|
: undefined
|
|
342
346
|
: undefined;
|