zod-args-parser 1.2.6 → 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 +19 -19
- 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} +11 -10
- 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-utilities.ts +214 -0
- 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/src/zod-utils.ts +0 -199
- /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
|
@@ -3,8 +3,8 @@ import { getCliMetadata } from "../metadata/get-cli-metadata.js";
|
|
|
3
3
|
import type { ArgumentMetadata, OptionMetadata, SubcommandMetadata } from "../metadata/metadata-types.js";
|
|
4
4
|
import type { Cli, Subcommand } from "../types.js";
|
|
5
5
|
|
|
6
|
-
export function generateMarkdown(...
|
|
7
|
-
const metadata = getCliMetadata(
|
|
6
|
+
export function generateMarkdown(...parameters: [Cli, ...Subcommand[]]): string {
|
|
7
|
+
const metadata = getCliMetadata(parameters);
|
|
8
8
|
|
|
9
9
|
let md = "<!-- Auto-generated by zod-args-parser -->\n\n";
|
|
10
10
|
|
|
@@ -22,19 +22,19 @@ export function generateMarkdown(...params: [Cli, ...Subcommand[]]): string {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// Options
|
|
25
|
-
if (metadata.options.length) {
|
|
25
|
+
if (metadata.options.length > 0) {
|
|
26
26
|
md += "\n## Options:\n\n";
|
|
27
27
|
md += renderOptions(metadata.options, 3);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
// Arguments
|
|
31
|
-
if (metadata.arguments.length) {
|
|
31
|
+
if (metadata.arguments.length > 0) {
|
|
32
32
|
md += "\n## Arguments:\n\n";
|
|
33
33
|
md += renderArguments(metadata.arguments, 3);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
// Subcommands
|
|
37
|
-
if (metadata.subcommands.length) {
|
|
37
|
+
if (metadata.subcommands.length > 0) {
|
|
38
38
|
md += "\n## Subcommands:\n\n";
|
|
39
39
|
md += renderSubcommands(metadata.subcommands);
|
|
40
40
|
}
|
|
@@ -46,129 +46,129 @@ export function generateMarkdown(...params: [Cli, ...Subcommand[]]): string {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
function renderExamples(examples: string, list = false) {
|
|
49
|
-
let
|
|
49
|
+
let outString = "";
|
|
50
50
|
|
|
51
51
|
const lang = "bash";
|
|
52
52
|
|
|
53
53
|
const listIndent = " ".repeat(list ? 2 : 0);
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
outString += "\n" + (list ? "- " : "");
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
outString += "```" + lang + "\n";
|
|
58
|
+
outString += listIndent + examples.replace(/\n/g, "\n" + listIndent);
|
|
59
|
+
outString += "\n" + listIndent + "```\n";
|
|
60
60
|
|
|
61
|
-
return
|
|
61
|
+
return outString;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
function renderOptions(optionsMetadata: OptionMetadata[] = [], h: number) {
|
|
65
|
-
let
|
|
65
|
+
let outString = "";
|
|
66
66
|
|
|
67
|
-
for (let
|
|
68
|
-
const metadata = optionsMetadata[
|
|
67
|
+
for (let index = 0; index < optionsMetadata.length; index++) {
|
|
68
|
+
const metadata = optionsMetadata[index];
|
|
69
69
|
|
|
70
70
|
const aliases = [metadata.nameAsArg].concat(metadata.aliasesAsArgs).join(", ");
|
|
71
71
|
|
|
72
72
|
const placeholder = metadata.placeholder && ` ${metadata.placeholder}`;
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
outString += `${"#".repeat(h)} \`${aliases + placeholder}\``;
|
|
75
75
|
|
|
76
76
|
if (metadata.optional) {
|
|
77
|
-
|
|
77
|
+
outString += " **[optional]**";
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
outString += "\n\n";
|
|
81
81
|
|
|
82
82
|
if (metadata.description) {
|
|
83
|
-
|
|
83
|
+
outString += `- ${metadata.description.replace(/(\n+)/g, " $1 ")}\n`;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
if (
|
|
87
|
-
|
|
86
|
+
if (metadata.defaultValue !== undefined) {
|
|
87
|
+
outString += `- **default:** \`${metadata.defaultValueAsString}\`\n`;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
if (metadata.example) {
|
|
91
|
-
|
|
91
|
+
outString += renderExamples(metadata.example, true);
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
if (
|
|
95
|
-
|
|
94
|
+
if (index < optionsMetadata.length - 1) {
|
|
95
|
+
outString += "\n";
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
return
|
|
99
|
+
return outString;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
function renderArguments(
|
|
103
|
-
let
|
|
102
|
+
function renderArguments(argumentsMetadata: ArgumentMetadata[] = [], h: number) {
|
|
103
|
+
let outString = "";
|
|
104
104
|
|
|
105
|
-
for (let
|
|
106
|
-
const metadata =
|
|
105
|
+
for (let index = 0; index < argumentsMetadata.length; index++) {
|
|
106
|
+
const metadata = argumentsMetadata[index];
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
outString += `${"#".repeat(h)} \`${metadata.name}\``;
|
|
109
109
|
|
|
110
110
|
if (metadata.optional) {
|
|
111
|
-
|
|
111
|
+
outString += " **[optional]**";
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
outString += "\n\n";
|
|
115
115
|
|
|
116
116
|
if (metadata.description) {
|
|
117
|
-
|
|
117
|
+
outString += `- ${metadata.description.replace(/(\n+)/g, " $1 ")}\n`;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
if (
|
|
121
|
-
|
|
120
|
+
if (metadata.defaultValue !== undefined) {
|
|
121
|
+
outString += `- **default:** \`${metadata.defaultValueAsString}\`\n`;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
if (metadata.example) {
|
|
125
|
-
|
|
125
|
+
outString += renderExamples(metadata.example);
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
if (
|
|
129
|
-
|
|
128
|
+
if (index < argumentsMetadata.length - 1) {
|
|
129
|
+
outString += "\n";
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
return
|
|
133
|
+
return outString;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
136
|
function renderSubcommands(subcommandsMetadata: SubcommandMetadata[]) {
|
|
137
|
-
let
|
|
137
|
+
let outString = "";
|
|
138
138
|
|
|
139
|
-
for (let
|
|
140
|
-
const metadata = subcommandsMetadata[
|
|
139
|
+
for (let index = 0; index < subcommandsMetadata.length; index++) {
|
|
140
|
+
const metadata = subcommandsMetadata[index];
|
|
141
141
|
|
|
142
142
|
const aliases = [metadata.name].concat(metadata.aliases).join(", ");
|
|
143
143
|
|
|
144
144
|
const placeholder = metadata.placeholder && ` ${metadata.placeholder}`;
|
|
145
145
|
|
|
146
|
-
|
|
146
|
+
outString += `### ${aliases + placeholder}\n\n`;
|
|
147
147
|
|
|
148
148
|
if (metadata.description) {
|
|
149
|
-
|
|
149
|
+
outString += `${metadata.description}\n`;
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
if (metadata.example) {
|
|
153
|
-
|
|
153
|
+
outString += renderExamples(metadata.example);
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
if (metadata.options.length) {
|
|
157
|
-
|
|
158
|
-
|
|
156
|
+
if (metadata.options.length > 0) {
|
|
157
|
+
outString += "\n#### Options:\n\n";
|
|
158
|
+
outString += renderOptions(metadata.options, 4);
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
if (metadata.arguments.length) {
|
|
162
|
-
|
|
163
|
-
|
|
161
|
+
if (metadata.arguments.length > 0) {
|
|
162
|
+
outString += "\n#### Arguments:\n\n";
|
|
163
|
+
outString += renderArguments(metadata.arguments, 4);
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
-
if (
|
|
167
|
-
|
|
166
|
+
if (index < subcommandsMetadata.length - 1) {
|
|
167
|
+
outString += "\n---\n\n";
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
return
|
|
171
|
+
return outString;
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
function escapeHtmlTags(markdown: string) {
|
|
@@ -186,13 +186,13 @@ function escapeHtmlTags(markdown: string) {
|
|
|
186
186
|
/** Credits: https://github.com/chalk/ansi-regex */
|
|
187
187
|
function ansiRegex({ onlyFirst = false } = {}) {
|
|
188
188
|
// Valid string terminator sequences are BEL, ESC\, and 0x9c
|
|
189
|
-
const ST =
|
|
189
|
+
const ST = String.raw`(?:\u0007|\u001B\u005C|\u009C)`;
|
|
190
190
|
|
|
191
191
|
// OSC sequences only: ESC ] ... ST (non-greedy until the first ST)
|
|
192
192
|
const osc = `(?:\\u001B\\][\\s\\S]*?${ST})`;
|
|
193
193
|
|
|
194
194
|
// CSI and related: ESC/C1, optional intermediates, optional params (supports ; and :) then final byte
|
|
195
|
-
const csi =
|
|
195
|
+
const csi = String.raw`[\u001B\u009B][[\]()#;?]*(?:\d{1,4}(?:[;:]\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]`;
|
|
196
196
|
|
|
197
197
|
const pattern = `${osc}|${csi}`;
|
|
198
198
|
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { stringifyValue } from "../
|
|
2
|
-
import { isOptionalSchema, schemaDefaultValue, schemaDescription } from "../zod-
|
|
1
|
+
import { stringifyValue } from "../utilities.js";
|
|
2
|
+
import { isOptionalSchema, schemaDefaultValue, schemaDescription } from "../zod-utilities.js";
|
|
3
3
|
|
|
4
4
|
import type { Argument } from "../types.js";
|
|
5
5
|
import type { ArgumentMetadata } from "./metadata-types.js";
|
|
6
6
|
|
|
7
|
-
export function getArgumentsMetadata(
|
|
7
|
+
export function getArgumentsMetadata(arguments_: Argument[]): ArgumentMetadata[] {
|
|
8
8
|
const outputMetadata: ArgumentMetadata[] = [];
|
|
9
9
|
|
|
10
|
-
for (const
|
|
11
|
-
const defaultValue = schemaDefaultValue(
|
|
10
|
+
for (const argument of arguments_) {
|
|
11
|
+
const defaultValue = schemaDefaultValue(argument.type);
|
|
12
12
|
|
|
13
13
|
outputMetadata.push({
|
|
14
|
-
name:
|
|
15
|
-
description:
|
|
14
|
+
name: argument.name,
|
|
15
|
+
description: argument.description ?? schemaDescription(argument.type) ?? "",
|
|
16
16
|
defaultValue,
|
|
17
17
|
defaultValueAsString: stringifyValue(defaultValue),
|
|
18
|
-
optional: isOptionalSchema(
|
|
19
|
-
example:
|
|
20
|
-
type:
|
|
18
|
+
optional: isOptionalSchema(argument.type),
|
|
19
|
+
example: argument.example ?? "",
|
|
20
|
+
type: argument.type,
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
23
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { stringifyValue } from "../
|
|
3
|
-
import { isOptionalSchema, schemaDefaultValue, schemaDescription } from "../zod-
|
|
1
|
+
import { transformOptionToArgument } from "../parser/parse/parser-helpers.js";
|
|
2
|
+
import { stringifyValue } from "../utilities.js";
|
|
3
|
+
import { isOptionalSchema, schemaDefaultValue, schemaDescription } from "../zod-utilities.js";
|
|
4
4
|
|
|
5
5
|
import type { Option } from "../types.js";
|
|
6
6
|
import type { OptionMetadata } from "./metadata-types.js";
|
|
@@ -8,7 +8,9 @@ import type { OptionMetadata } from "./metadata-types.js";
|
|
|
8
8
|
export function getOptionsMetadata(options: Option[]): OptionMetadata[] {
|
|
9
9
|
const outputMetadata: OptionMetadata[] = [];
|
|
10
10
|
|
|
11
|
-
if (!options ||
|
|
11
|
+
if (!options || options.length === 0) {
|
|
12
|
+
return outputMetadata;
|
|
13
|
+
}
|
|
12
14
|
|
|
13
15
|
for (const option of options) {
|
|
14
16
|
const defaultValue = schemaDefaultValue(option.type);
|
|
@@ -16,9 +18,9 @@ export function getOptionsMetadata(options: Option[]): OptionMetadata[] {
|
|
|
16
18
|
|
|
17
19
|
outputMetadata.push({
|
|
18
20
|
name: option.name,
|
|
19
|
-
nameAsArg:
|
|
21
|
+
nameAsArg: transformOptionToArgument(option.name),
|
|
20
22
|
aliases,
|
|
21
|
-
aliasesAsArgs: aliases.map(
|
|
23
|
+
aliasesAsArgs: aliases.map(alias => transformOptionToArgument(alias)),
|
|
22
24
|
placeholder: option.placeholder ?? "",
|
|
23
25
|
description: option.description ?? schemaDescription(option.type) ?? "",
|
|
24
26
|
optional: isOptionalSchema(option.type),
|
|
@@ -7,7 +7,9 @@ import type { SubcommandMetadata } from "./metadata-types.js";
|
|
|
7
7
|
export function getSubcommandsMetadata(subcommands: Subcommand[]): SubcommandMetadata[] {
|
|
8
8
|
const outputMetadata: SubcommandMetadata[] = [];
|
|
9
9
|
|
|
10
|
-
if (!subcommands ||
|
|
10
|
+
if (!subcommands || subcommands.length === 0) {
|
|
11
|
+
return outputMetadata;
|
|
12
|
+
}
|
|
11
13
|
|
|
12
14
|
for (const subcommand of subcommands) {
|
|
13
15
|
const optionsMetadata = subcommand.options ? getOptionsMetadata(subcommand.options) : [];
|
|
@@ -64,11 +64,11 @@ type ParsedArgument<S extends Schema = Schema> =
|
|
|
64
64
|
source: "default";
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
-
type
|
|
67
|
+
type OptionsArray2Record<T extends Option[] | undefined> = T extends Option[]
|
|
68
68
|
? { [K in T[number]["name"]]: ParsedOption<Extract<T[number], { name: K }>["type"], K> }
|
|
69
69
|
: Record<never, never>;
|
|
70
70
|
|
|
71
|
-
type
|
|
71
|
+
type ArgumentsArray2ArrayType<T extends Argument[] | undefined> = T extends Argument[]
|
|
72
72
|
? { [K in keyof T]: ParsedArgument<T[K] extends { type: Schema } ? T[K]["type"] : never> }
|
|
73
73
|
: never;
|
|
74
74
|
|
|
@@ -76,12 +76,12 @@ export type ParseResult<S extends Partial<Subcommand>[]> = {
|
|
|
76
76
|
[K in keyof S]: Prettify<{
|
|
77
77
|
subcommand: S[K]["name"] extends string ? S[K]["name"] : undefined;
|
|
78
78
|
positional: S[K]["allowPositional"] extends true ? string[] : never;
|
|
79
|
-
options:
|
|
80
|
-
arguments:
|
|
79
|
+
options: OptionsArray2Record<S[K]["options"]>;
|
|
80
|
+
arguments: ArgumentsArray2ArrayType<S[K]["arguments"]>;
|
|
81
81
|
}>;
|
|
82
82
|
}[number];
|
|
83
83
|
|
|
84
|
-
export type
|
|
84
|
+
export type ParsedContext = {
|
|
85
85
|
subcommand: string | undefined;
|
|
86
86
|
options: Record<string, ParsedOption>;
|
|
87
87
|
arguments?: ParsedArgument[];
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import { generateOrdinalSuffix } from "../../
|
|
2
|
-
import { isBooleanSchema, isOptionalSchema, schemaDefaultValue } from "../../zod-
|
|
1
|
+
import { generateOrdinalSuffix } from "../../utilities.js";
|
|
2
|
+
import { isBooleanSchema, isOptionalSchema, schemaDefaultValue } from "../../zod-utilities.js";
|
|
3
3
|
import {
|
|
4
4
|
decoupleFlags,
|
|
5
5
|
findOption,
|
|
6
6
|
findSubcommand,
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
isFlagArgument,
|
|
8
|
+
isOptionArgument,
|
|
9
|
+
transformOptionToArgument,
|
|
10
10
|
} from "./parser-helpers.js";
|
|
11
11
|
|
|
12
12
|
import type { Cli, Subcommand } from "../../types.js";
|
|
13
|
-
import type {
|
|
13
|
+
import type { ParsedContext } from "./parse-types.js";
|
|
14
14
|
|
|
15
|
-
export function parse(argv: string[], ...
|
|
16
|
-
const
|
|
17
|
-
const allSubcommands = new Set<string>(
|
|
15
|
+
export function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {
|
|
16
|
+
const subcommandArray = parameters as Subcommand[];
|
|
17
|
+
const allSubcommands = new Set<string>(subcommandArray.flatMap(c => [c.name, ...(c.aliases || [])]));
|
|
18
18
|
|
|
19
19
|
argv = decoupleFlags(argv); // decouple flags E.g. `-rf` -> `-r, -f`
|
|
20
20
|
|
|
21
|
-
const results:
|
|
21
|
+
const results: ParsedContext = {
|
|
22
22
|
subcommand: undefined,
|
|
23
23
|
options: {},
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
/** - Get current subcommand object */
|
|
27
|
-
const
|
|
27
|
+
const getSubcommandObject = () => findSubcommand(results.subcommand, subcommandArray);
|
|
28
28
|
|
|
29
|
-
for (let
|
|
30
|
-
const
|
|
29
|
+
for (let index = 0; index < argv.length; index++) {
|
|
30
|
+
const argument_ = argv[index];
|
|
31
31
|
|
|
32
32
|
// * Subcommand check
|
|
33
|
-
if (
|
|
34
|
-
results.subcommand = allSubcommands.has(
|
|
33
|
+
if (index === 0) {
|
|
34
|
+
results.subcommand = allSubcommands.has(argument_) ? argument_ : undefined;
|
|
35
35
|
|
|
36
36
|
// add positional and arguments arrays
|
|
37
|
-
const
|
|
38
|
-
if (
|
|
37
|
+
const subcommandObject = getSubcommandObject();
|
|
38
|
+
if (subcommandObject && subcommandObject.allowPositional) {
|
|
39
39
|
results.positional = [];
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
if (
|
|
42
|
+
if (subcommandObject && subcommandObject.arguments?.length) {
|
|
43
43
|
results.arguments = [];
|
|
44
44
|
}
|
|
45
45
|
|
|
@@ -50,22 +50,22 @@ export function parse(argv: string[], ...params: [Cli, ...Subcommand[]]) {
|
|
|
50
50
|
// * Option check
|
|
51
51
|
|
|
52
52
|
// Check for `--option=value` or `--option value`
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const argument =
|
|
56
|
-
const
|
|
53
|
+
const argumentAndValue = argument_.split("=").filter(Boolean);
|
|
54
|
+
const argumentWithEquals = argument_.includes("=");
|
|
55
|
+
const argument = argumentAndValue[0];
|
|
56
|
+
const argumentValue: string | undefined = argumentAndValue[1];
|
|
57
57
|
|
|
58
|
-
if (
|
|
59
|
-
if (
|
|
60
|
-
throw new Error(`Flag arguments cannot be assigned using "=": "${
|
|
58
|
+
if (isOptionArgument(argument)) {
|
|
59
|
+
if (isFlagArgument(argument) && argumentWithEquals) {
|
|
60
|
+
throw new Error(`Flag arguments cannot be assigned using "=": "${argument_}"`, { cause: "zod-args-parser" });
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
const
|
|
64
|
-
if (!
|
|
63
|
+
const subcommandObject = getSubcommandObject();
|
|
64
|
+
if (!subcommandObject) {
|
|
65
65
|
throw new Error(`Unknown subcommand: "${results.subcommand}"`, { cause: "zod-args-parser" });
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
if (!
|
|
68
|
+
if (!subcommandObject.options) {
|
|
69
69
|
if (!results.subcommand) {
|
|
70
70
|
throw new Error(`Error: options are not allowed here: "${argument}"`, { cause: "zod-args-parser" });
|
|
71
71
|
}
|
|
@@ -75,7 +75,7 @@ export function parse(argv: string[], ...params: [Cli, ...Subcommand[]]) {
|
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
const option = findOption(argument,
|
|
78
|
+
const option = findOption(argument, subcommandObject.options);
|
|
79
79
|
if (!option) {
|
|
80
80
|
throw new Error(`Unknown option: "${argument}"`, { cause: "zod-args-parser" });
|
|
81
81
|
}
|
|
@@ -85,21 +85,21 @@ export function parse(argv: string[], ...params: [Cli, ...Subcommand[]]) {
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
const isTypeBoolean = isBooleanSchema(option.type);
|
|
88
|
-
const
|
|
88
|
+
const nextArgument = argv[index + 1];
|
|
89
89
|
|
|
90
|
-
let optionValue: string | boolean =
|
|
90
|
+
let optionValue: string | boolean = argumentWithEquals ? argumentValue : nextArgument;
|
|
91
91
|
|
|
92
92
|
// infer value for boolean options
|
|
93
|
-
if (isTypeBoolean && !
|
|
93
|
+
if (isTypeBoolean && !argumentWithEquals) {
|
|
94
94
|
optionValue = "true";
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
if (
|
|
97
|
+
if (optionValue === undefined) {
|
|
98
98
|
throw new Error(`Expected a value for "${argument}" but got nothing`, { cause: "zod-args-parser" });
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
if (!
|
|
102
|
-
throw new Error(`Expected a value for "${argument}" but got an argument "${
|
|
101
|
+
if (!argumentWithEquals && isOptionArgument(optionValue)) {
|
|
102
|
+
throw new Error(`Expected a value for "${argument}" but got an argument "${nextArgument}"`, {
|
|
103
103
|
cause: "zod-args-parser",
|
|
104
104
|
});
|
|
105
105
|
}
|
|
@@ -113,29 +113,29 @@ export function parse(argv: string[], ...params: [Cli, ...Subcommand[]]) {
|
|
|
113
113
|
};
|
|
114
114
|
|
|
115
115
|
// Skip to the next argument if it is the current option’s value.
|
|
116
|
-
if (!
|
|
117
|
-
|
|
116
|
+
if (!argumentWithEquals && !isTypeBoolean) {
|
|
117
|
+
index++;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
continue;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
const
|
|
123
|
+
const subcommandObject = getSubcommandObject();
|
|
124
124
|
|
|
125
125
|
// * Arguments check
|
|
126
|
-
if (
|
|
126
|
+
if (subcommandObject?.arguments?.length) {
|
|
127
127
|
if (!results.arguments) {
|
|
128
128
|
results.arguments = [];
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
const
|
|
131
|
+
const currentArgumentCount = results.arguments.length;
|
|
132
132
|
|
|
133
133
|
// Any extra arguments are possibly positional
|
|
134
|
-
if (
|
|
135
|
-
const
|
|
134
|
+
if (currentArgumentCount < subcommandObject.arguments.length) {
|
|
135
|
+
const argumentType = subcommandObject.arguments[currentArgumentCount].type;
|
|
136
136
|
results.arguments.push({
|
|
137
|
-
schema:
|
|
138
|
-
rawValue:
|
|
137
|
+
schema: argumentType,
|
|
138
|
+
rawValue: argument_,
|
|
139
139
|
source: "cli",
|
|
140
140
|
});
|
|
141
141
|
continue;
|
|
@@ -143,37 +143,37 @@ export function parse(argv: string[], ...params: [Cli, ...Subcommand[]]) {
|
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
// * Positional check
|
|
146
|
-
if (
|
|
146
|
+
if (subcommandObject?.allowPositional) {
|
|
147
147
|
if (!results.positional) {
|
|
148
148
|
results.positional = [];
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
-
results.positional.push(
|
|
151
|
+
results.positional.push(argument_);
|
|
152
152
|
continue;
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
// * Unexpected
|
|
156
156
|
if (!results.subcommand) {
|
|
157
|
-
throw new Error(`Unexpected argument "${
|
|
157
|
+
throw new Error(`Unexpected argument "${argument_}": positional arguments are not allowed here`, {
|
|
158
158
|
cause: "zod-args-parser",
|
|
159
159
|
});
|
|
160
160
|
}
|
|
161
161
|
|
|
162
162
|
throw new Error(
|
|
163
|
-
`Unexpected argument "${
|
|
163
|
+
`Unexpected argument "${argument_}": positional arguments are not allowed for subcommand "${results.subcommand}"`,
|
|
164
164
|
{ cause: "zod-args-parser" },
|
|
165
165
|
);
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
// * Check for missing options - set defaults - add `source`
|
|
169
|
-
const
|
|
170
|
-
if (!
|
|
169
|
+
const subcommandObject = getSubcommandObject();
|
|
170
|
+
if (!subcommandObject) {
|
|
171
171
|
throw new Error(`Unknown subcommand: "${results.subcommand}"`, { cause: "zod-args-parser" });
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
// Options
|
|
175
|
-
if (
|
|
176
|
-
for (const option of
|
|
175
|
+
if (subcommandObject.options?.length) {
|
|
176
|
+
for (const option of subcommandObject.options) {
|
|
177
177
|
// option already exists
|
|
178
178
|
if (option.name in results.options) continue;
|
|
179
179
|
|
|
@@ -181,7 +181,7 @@ export function parse(argv: string[], ...params: [Cli, ...Subcommand[]]) {
|
|
|
181
181
|
const defaultValue = schemaDefaultValue(option.type);
|
|
182
182
|
|
|
183
183
|
if (optional) {
|
|
184
|
-
if (
|
|
184
|
+
if (defaultValue === undefined) {
|
|
185
185
|
continue;
|
|
186
186
|
}
|
|
187
187
|
|
|
@@ -189,24 +189,26 @@ export function parse(argv: string[], ...params: [Cli, ...Subcommand[]]) {
|
|
|
189
189
|
continue;
|
|
190
190
|
}
|
|
191
191
|
|
|
192
|
-
throw new Error(`Missing required option: ${
|
|
192
|
+
throw new Error(`Missing required option: ${transformOptionToArgument(option.name)}`, {
|
|
193
|
+
cause: "zod-args-parser",
|
|
194
|
+
});
|
|
193
195
|
}
|
|
194
196
|
}
|
|
195
197
|
|
|
196
198
|
// Arguments
|
|
197
|
-
if (
|
|
198
|
-
const
|
|
199
|
-
const
|
|
199
|
+
if (subcommandObject.arguments?.length) {
|
|
200
|
+
const currentArgumentCount = results.arguments?.length ?? 0;
|
|
201
|
+
const subcommandArgumentCount = subcommandObject.arguments.length;
|
|
200
202
|
|
|
201
203
|
// missing arguments
|
|
202
|
-
if (
|
|
203
|
-
for (let
|
|
204
|
-
const argumentType =
|
|
204
|
+
if (currentArgumentCount < subcommandArgumentCount) {
|
|
205
|
+
for (let index = currentArgumentCount; index < subcommandArgumentCount; index++) {
|
|
206
|
+
const argumentType = subcommandObject.arguments[index].type;
|
|
205
207
|
const optional = isOptionalSchema(argumentType);
|
|
206
208
|
const defaultValue = schemaDefaultValue(argumentType);
|
|
207
209
|
|
|
208
210
|
if (optional) {
|
|
209
|
-
if (
|
|
211
|
+
if (defaultValue === undefined) {
|
|
210
212
|
continue;
|
|
211
213
|
}
|
|
212
214
|
|
|
@@ -216,9 +218,10 @@ export function parse(argv: string[], ...params: [Cli, ...Subcommand[]]) {
|
|
|
216
218
|
continue;
|
|
217
219
|
}
|
|
218
220
|
|
|
219
|
-
throw new Error(
|
|
220
|
-
|
|
221
|
-
|
|
221
|
+
throw new Error(
|
|
222
|
+
`the ${generateOrdinalSuffix(index)} argument is required: "${subcommandObject.arguments[index].name}"`,
|
|
223
|
+
{ cause: "zod-args-parser" },
|
|
224
|
+
);
|
|
222
225
|
}
|
|
223
226
|
}
|
|
224
227
|
}
|