zod-args-parser 1.0.13 → 1.0.15

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.
Files changed (152) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +12 -1
  3. package/lib/commonjs/autocomplete-scripts/bash-autocomplete-script.js +42 -0
  4. package/lib/commonjs/autocomplete-scripts/bash-autocomplete-script.js.map +1 -0
  5. package/lib/commonjs/autocomplete-scripts/powershell-autocomplete-script.js +31 -0
  6. package/lib/commonjs/autocomplete-scripts/powershell-autocomplete-script.js.map +1 -0
  7. package/lib/commonjs/autocomplete-scripts/zsh-autocomplete-script.js +41 -0
  8. package/lib/commonjs/autocomplete-scripts/zsh-autocomplete-script.js.map +1 -0
  9. package/lib/commonjs/help-message/colors.js +1 -0
  10. package/lib/commonjs/help-message/colors.js.map +1 -0
  11. package/lib/commonjs/help-message/print-arguments.js +1 -0
  12. package/lib/commonjs/help-message/print-arguments.js.map +1 -0
  13. package/lib/commonjs/help-message/print-help-message.js +1 -0
  14. package/lib/commonjs/help-message/print-help-message.js.map +1 -0
  15. package/lib/commonjs/help-message/print-options.js +1 -0
  16. package/lib/commonjs/help-message/print-options.js.map +1 -0
  17. package/lib/commonjs/help-message/print-subcommands.js +1 -0
  18. package/lib/commonjs/help-message/print-subcommands.js.map +1 -0
  19. package/lib/commonjs/help-message/utils.js +1 -0
  20. package/lib/commonjs/help-message/utils.js.map +1 -0
  21. package/lib/commonjs/index.js +1 -1
  22. package/lib/commonjs/index.js.map +1 -1
  23. package/lib/commonjs/markdown/generate-markdown.js +1 -0
  24. package/lib/commonjs/markdown/generate-markdown.js.map +1 -0
  25. package/lib/commonjs/metadata/get-arguments-metadata.js +1 -0
  26. package/lib/commonjs/metadata/get-arguments-metadata.js.map +1 -0
  27. package/lib/commonjs/metadata/get-cli-metadata.js +1 -0
  28. package/lib/commonjs/metadata/get-cli-metadata.js.map +1 -0
  29. package/lib/commonjs/metadata/get-options-metadata.js +1 -0
  30. package/lib/commonjs/metadata/get-options-metadata.js.map +1 -0
  31. package/lib/commonjs/metadata/get-subcommands-metadata.js +1 -0
  32. package/lib/commonjs/metadata/get-subcommands-metadata.js.map +1 -0
  33. package/lib/commonjs/parser/parse.js +1 -0
  34. package/lib/commonjs/parser/parse.js.map +1 -0
  35. package/lib/commonjs/parser/safe-parse.js +1 -0
  36. package/lib/commonjs/parser/safe-parse.js.map +1 -0
  37. package/lib/commonjs/utils.js +1 -1
  38. package/lib/commonjs/utils.js.map +1 -1
  39. package/lib/module/autocomplete-scripts/bash-autocomplete-script.js +42 -0
  40. package/lib/module/autocomplete-scripts/bash-autocomplete-script.js.map +1 -0
  41. package/lib/module/autocomplete-scripts/powershell-autocomplete-script.js +31 -0
  42. package/lib/module/autocomplete-scripts/powershell-autocomplete-script.js.map +1 -0
  43. package/lib/module/autocomplete-scripts/zsh-autocomplete-script.js +41 -0
  44. package/lib/module/autocomplete-scripts/zsh-autocomplete-script.js.map +1 -0
  45. package/lib/module/help-message/colors.js +1 -0
  46. package/lib/module/help-message/colors.js.map +1 -0
  47. package/lib/module/help-message/print-arguments.js +1 -0
  48. package/lib/module/help-message/print-arguments.js.map +1 -0
  49. package/lib/module/help-message/print-help-message.js +1 -0
  50. package/lib/module/help-message/print-help-message.js.map +1 -0
  51. package/lib/module/help-message/print-options.js +1 -0
  52. package/lib/module/help-message/print-options.js.map +1 -0
  53. package/lib/module/help-message/print-subcommands.js +1 -0
  54. package/lib/module/help-message/print-subcommands.js.map +1 -0
  55. package/lib/module/help-message/utils.js +1 -0
  56. package/lib/module/help-message/utils.js.map +1 -0
  57. package/lib/module/index.js +1 -1
  58. package/lib/module/index.js.map +1 -1
  59. package/lib/module/markdown/generate-markdown.js +1 -0
  60. package/lib/module/markdown/generate-markdown.js.map +1 -0
  61. package/lib/module/metadata/get-arguments-metadata.js +1 -0
  62. package/lib/module/metadata/get-arguments-metadata.js.map +1 -0
  63. package/lib/module/metadata/get-cli-metadata.js +1 -0
  64. package/lib/module/metadata/get-cli-metadata.js.map +1 -0
  65. package/lib/module/metadata/get-options-metadata.js +1 -0
  66. package/lib/module/metadata/get-options-metadata.js.map +1 -0
  67. package/lib/module/metadata/get-subcommands-metadata.js +1 -0
  68. package/lib/module/metadata/get-subcommands-metadata.js.map +1 -0
  69. package/lib/module/parser/parse.js +1 -0
  70. package/lib/module/parser/parse.js.map +1 -0
  71. package/lib/module/parser/safe-parse.js +1 -0
  72. package/lib/module/parser/safe-parse.js.map +1 -0
  73. package/lib/module/utils.js +1 -1
  74. package/lib/module/utils.js.map +1 -1
  75. package/lib/typescript/autocomplete-scripts/bash-autocomplete-script.d.ts +11 -0
  76. package/lib/typescript/autocomplete-scripts/bash-autocomplete-script.d.ts.map +1 -0
  77. package/lib/typescript/autocomplete-scripts/powershell-autocomplete-script.d.ts +14 -0
  78. package/lib/typescript/autocomplete-scripts/powershell-autocomplete-script.d.ts.map +1 -0
  79. package/lib/typescript/autocomplete-scripts/zsh-autocomplete-script.d.ts +11 -0
  80. package/lib/typescript/autocomplete-scripts/zsh-autocomplete-script.d.ts.map +1 -0
  81. package/lib/typescript/help-message/colors.d.ts +17 -0
  82. package/lib/typescript/help-message/colors.d.ts.map +1 -0
  83. package/lib/typescript/help-message/print-arguments.d.ts +4 -0
  84. package/lib/typescript/help-message/print-arguments.d.ts.map +1 -0
  85. package/lib/typescript/help-message/print-help-message.d.ts +4 -0
  86. package/lib/typescript/help-message/print-help-message.d.ts.map +1 -0
  87. package/lib/typescript/help-message/print-options.d.ts +4 -0
  88. package/lib/typescript/help-message/print-options.d.ts.map +1 -0
  89. package/lib/typescript/help-message/print-subcommands.d.ts +4 -0
  90. package/lib/typescript/help-message/print-subcommands.d.ts.map +1 -0
  91. package/lib/typescript/help-message/utils.d.ts +13 -0
  92. package/lib/typescript/help-message/utils.d.ts.map +1 -0
  93. package/lib/typescript/index.d.ts +11 -4
  94. package/lib/typescript/index.d.ts.map +1 -1
  95. package/lib/typescript/markdown/generate-markdown.d.ts +3 -0
  96. package/lib/typescript/markdown/generate-markdown.d.ts.map +1 -0
  97. package/lib/typescript/metadata/get-arguments-metadata.d.ts +3 -0
  98. package/lib/typescript/metadata/get-arguments-metadata.d.ts.map +1 -0
  99. package/lib/typescript/metadata/get-cli-metadata.d.ts +3 -0
  100. package/lib/typescript/metadata/get-cli-metadata.d.ts.map +1 -0
  101. package/lib/typescript/metadata/get-options-metadata.d.ts +3 -0
  102. package/lib/typescript/metadata/get-options-metadata.d.ts.map +1 -0
  103. package/lib/typescript/metadata/get-subcommands-metadata.d.ts +3 -0
  104. package/lib/typescript/metadata/get-subcommands-metadata.d.ts.map +1 -0
  105. package/lib/typescript/parser/parse.d.ts +3 -0
  106. package/lib/typescript/parser/parse.d.ts.map +1 -0
  107. package/lib/typescript/parser/safe-parse.d.ts +3 -0
  108. package/lib/typescript/parser/safe-parse.d.ts.map +1 -0
  109. package/lib/typescript/types.d.ts +80 -0
  110. package/lib/typescript/types.d.ts.map +1 -1
  111. package/lib/typescript/utils.d.ts +0 -12
  112. package/lib/typescript/utils.d.ts.map +1 -1
  113. package/package.json +22 -22
  114. package/src/autocomplete-scripts/bash-autocomplete-script.ts +81 -0
  115. package/src/autocomplete-scripts/powershell-autocomplete-script.ts +78 -0
  116. package/src/autocomplete-scripts/zsh-autocomplete-script.ts +69 -0
  117. package/src/help-message/colors.ts +24 -0
  118. package/src/help-message/print-arguments.ts +35 -0
  119. package/src/help-message/print-help-message.ts +126 -0
  120. package/src/help-message/print-options.ts +40 -0
  121. package/src/help-message/print-subcommands.ts +28 -0
  122. package/src/help-message/utils.ts +31 -0
  123. package/src/index.ts +13 -10
  124. package/src/markdown/generate-markdown.ts +172 -0
  125. package/src/metadata/get-arguments-metadata.ts +23 -0
  126. package/src/metadata/get-cli-metadata.ts +23 -0
  127. package/src/metadata/get-options-metadata.ts +30 -0
  128. package/src/metadata/get-subcommands-metadata.ts +29 -0
  129. package/src/{parser.ts → parser/parse.ts} +4 -54
  130. package/src/parser/safe-parse.ts +45 -0
  131. package/src/types.ts +116 -0
  132. package/src/utils.ts +0 -32
  133. package/lib/commonjs/autocomplete.js +0 -107
  134. package/lib/commonjs/autocomplete.js.map +0 -1
  135. package/lib/commonjs/help.js +0 -1
  136. package/lib/commonjs/help.js.map +0 -1
  137. package/lib/commonjs/parser.js +0 -1
  138. package/lib/commonjs/parser.js.map +0 -1
  139. package/lib/module/autocomplete.js +0 -107
  140. package/lib/module/autocomplete.js.map +0 -1
  141. package/lib/module/help.js +0 -1
  142. package/lib/module/help.js.map +0 -1
  143. package/lib/module/parser.js +0 -1
  144. package/lib/module/parser.js.map +0 -1
  145. package/lib/typescript/autocomplete.d.ts +0 -32
  146. package/lib/typescript/autocomplete.d.ts.map +0 -1
  147. package/lib/typescript/help.d.ts +0 -9
  148. package/lib/typescript/help.d.ts.map +0 -1
  149. package/lib/typescript/parser.d.ts +0 -4
  150. package/lib/typescript/parser.d.ts.map +0 -1
  151. package/src/autocomplete.ts +0 -217
  152. package/src/help.ts +0 -341
package/src/help.ts DELETED
@@ -1,341 +0,0 @@
1
- import chalk from "chalk";
2
- import { concat, getDefaultValueFromSchema, indent, ln, print, println, transformOptionToArg } from "./utils.js";
3
-
4
- import type { Argument, Cli, Option, PrintHelpOpt, Subcommand } from "./types.js";
5
-
6
- const colors: NonNullable<Required<PrintHelpOpt["customColors"]>> = {
7
- title: chalk.bold.blue,
8
- description: chalk.white,
9
- default: chalk.dim.italic,
10
- optional: chalk.dim.italic,
11
- exampleTitle: chalk.yellow,
12
- example: chalk.dim,
13
- command: chalk.yellow,
14
- option: chalk.cyan,
15
- argument: chalk.green,
16
- placeholder: chalk.hex("#FF9800"),
17
- punctuation: chalk.white.dim,
18
- };
19
-
20
- type PreparedToPrint = {
21
- names: string;
22
- description: string;
23
- placeholder?: string;
24
- example?: string;
25
- default?: string;
26
- optional?: string;
27
- };
28
-
29
- function printCliHelp(params: [Cli, ...Subcommand[]], printOptions: PrintHelpOpt = {}) {
30
- printOptions.colors ??= true;
31
-
32
- const noColors = new Proxy(colors, {
33
- get: () => {
34
- return (...str: string[]) => str.join(" ");
35
- },
36
- });
37
-
38
- const c = printOptions.colors ? colors : noColors;
39
-
40
- if (printOptions.customColors) {
41
- Object.assign(c, printOptions.customColors);
42
- }
43
-
44
- const isFirstParamCli = "cliName" in params[0];
45
- const cliOptions = (isFirstParamCli ? params.shift() : {}) as Cli;
46
- const subcommands = params as Subcommand[];
47
-
48
- /** Print a styled title */
49
- const printTitle = (title: string) => {
50
- print(c.title(` ${title.toUpperCase()} `));
51
- };
52
-
53
- // Print CLI usage
54
- const cliName = cliOptions.cliName ?? "";
55
- const usage =
56
- cliOptions.usage ??
57
- concat(
58
- c.punctuation("$"),
59
- cliName,
60
- subcommands.length ? c.command("[command]") : "",
61
- cliOptions.options?.length ? c.option("[options]") : "",
62
- cliOptions.arguments?.length || cliOptions.allowPositional ? c.argument("<arguments>") : "",
63
- );
64
- printTitle("Usage");
65
- println();
66
- println(indent(2), usage, ln(1));
67
-
68
- // Print CLI description
69
- if (cliOptions.description) {
70
- printTitle("Description");
71
- println();
72
- println(indent(2), c.description(cliOptions.description), ln(1));
73
- }
74
-
75
- let longest = 0;
76
-
77
- // Prepare CLI options
78
- const [optionsToPrint, longestOptionIndent] = prepareOptionsToPrint(cliOptions.options);
79
- if (longestOptionIndent > longest) longest = longestOptionIndent;
80
-
81
- // Prepare CLI commands
82
- const [commandsToPrint, longestSubcommandIndent] = prepareCommandsToPrint(subcommands);
83
- if (longestSubcommandIndent > longest) longest = longestSubcommandIndent;
84
-
85
- // Prepare CLI arguments
86
- const [argsToPrint, longestArgIndent] = prepareArgumentsToPrint(cliOptions.arguments);
87
- if (longestArgIndent > longest) longest = longestArgIndent;
88
-
89
- // Print CLI options
90
- printPreparedOptions(optionsToPrint, c, longest);
91
-
92
- // Print CLI commands
93
- printPreparedCommands(commandsToPrint, c, longest);
94
-
95
- // Print CLI arguments
96
- printPreparedArguments(argsToPrint, c, longest);
97
-
98
- // Print CLI example
99
- if (cliOptions.example) {
100
- printTitle("Example");
101
- println();
102
- const normalizeExample = cliOptions.example.replace(/\n/g, "\n" + indent(3));
103
- println(indent(2), c.example(normalizeExample), ln(1));
104
- }
105
- }
106
-
107
- function printSubcommandHelp(subcommand: Subcommand, printOptions: PrintHelpOpt = {}, cliName = "") {
108
- printOptions.colors ??= true;
109
-
110
- const noColors = new Proxy(colors, {
111
- get: () => {
112
- return (...str: string[]) => str.join(" ");
113
- },
114
- });
115
-
116
- const c = printOptions.colors ? colors : noColors;
117
-
118
- if (printOptions.customColors) {
119
- Object.assign(c, printOptions.customColors);
120
- }
121
-
122
- /** Print a styled title */
123
- const printTitle = (title: string) => {
124
- print(c.title(` ${title.toUpperCase()} `));
125
- };
126
-
127
- // Print command usage
128
- const usage =
129
- subcommand.usage ??
130
- concat(
131
- c.punctuation("$"),
132
- cliName,
133
- c.command(subcommand.name),
134
- subcommand.options?.length ? c.option("[options]") : "",
135
- subcommand.arguments?.length || subcommand.allowPositional ? c.argument("<arguments>") : "",
136
- );
137
- printTitle("Usage");
138
- println();
139
- println(indent(2), usage, ln(1));
140
-
141
- // Print command description
142
- if (subcommand.description) {
143
- printTitle("Description");
144
- println();
145
- const normalizeDesc = subcommand.description.replace(/\n/g, "\n" + indent(3));
146
- println(indent(2), c.description(normalizeDesc), ln(1));
147
- }
148
-
149
- let longest = 0;
150
-
151
- // Prepare command options
152
- const [optionsToPrint, longestOptionIndent] = prepareOptionsToPrint(subcommand.options);
153
- if (longestOptionIndent > longest) longest = longestOptionIndent;
154
-
155
- // Prepare command arguments
156
- const [argsToPrint, longestArgIndent] = prepareArgumentsToPrint(subcommand.arguments);
157
- if (longestArgIndent > longest) longest = longestArgIndent;
158
-
159
- // Print command options
160
- printPreparedOptions(optionsToPrint, c, longest);
161
-
162
- // Print command arguments
163
- printPreparedArguments(argsToPrint, c, longest);
164
-
165
- // Print command example
166
- if (subcommand.example) {
167
- printTitle("Example");
168
- println();
169
- const normalizeExample = subcommand.example.replace(/\n/g, "\n" + indent(3));
170
- println(indent(2), c.example(normalizeExample), ln(1));
171
- }
172
- }
173
-
174
- // * Prepare
175
- function prepareOptionsToPrint(options: Option[] | undefined): [PreparedToPrint[], number] {
176
- if (!options || !options.length) return [[], 0];
177
-
178
- const optionsToPrint: PreparedToPrint[] = [];
179
- let longest = 0;
180
-
181
- for (const option of options) {
182
- const nameWithAliases = option.aliases ? [...option.aliases, option.name] : [option.name];
183
- const names = Array.from(new Set(nameWithAliases.map(name => transformOptionToArg(name)))).join(", ");
184
-
185
- const defaultValue = getDefaultValueFromSchema(option.type);
186
-
187
- const placeholder = option.placeholder ?? " ";
188
- optionsToPrint.push({
189
- names,
190
- placeholder,
191
- description: option.description ?? option.type.description ?? "",
192
- default: typeof defaultValue !== "undefined" ? `(default: ${JSON.stringify(defaultValue)})` : "",
193
- optional: option.type.isOptional() ? "[optional]" : "",
194
- example: option.example ?? "",
195
- });
196
-
197
- const optLength = names.length + placeholder.length;
198
-
199
- if (optLength > longest) longest = optLength;
200
- }
201
-
202
- return [optionsToPrint, longest];
203
- }
204
-
205
- function prepareCommandsToPrint(subcommands: Subcommand[] | undefined): [PreparedToPrint[], number] {
206
- if (!subcommands || !subcommands.length) return [[], 0];
207
-
208
- const commandsToPrint: PreparedToPrint[] = [];
209
- let longest = 0;
210
-
211
- for (const subcommand of subcommands) {
212
- const { name, aliases, description } = subcommand;
213
- const names = Array.from(new Set([...(aliases ?? []), name])).join(", ");
214
-
215
- const placeholder =
216
- subcommand.placeholder ?? (subcommand.options ? "[options]" : subcommand.allowPositional ? "<args>" : " ");
217
-
218
- commandsToPrint.push({ names, placeholder, description: description ?? "" });
219
-
220
- const cmdLength = names.length + placeholder.length;
221
- if (cmdLength > longest) longest = cmdLength;
222
- }
223
-
224
- return [commandsToPrint, longest];
225
- }
226
-
227
- function prepareArgumentsToPrint(args: Argument[] | undefined): [PreparedToPrint[], number] {
228
- if (!args || !args.length) return [[], 0];
229
-
230
- const argsToPrint: PreparedToPrint[] = [];
231
- let longest = 0;
232
-
233
- for (const arg of args) {
234
- const defaultValue = getDefaultValueFromSchema(arg.type);
235
-
236
- argsToPrint.push({
237
- names: arg.name,
238
- description: arg.description ?? "",
239
- default: typeof defaultValue !== "undefined" ? `(default: ${JSON.stringify(defaultValue)})` : "",
240
- optional: arg.type.isOptional() ? "[optional]" : "",
241
- example: arg.example ?? "",
242
- });
243
-
244
- const cmdLength = arg.name.length;
245
- if (cmdLength > longest) longest = cmdLength;
246
- }
247
-
248
- return [argsToPrint, longest];
249
- }
250
-
251
- // * Print
252
- function printPreparedOptions(optionsToPrint: PreparedToPrint[], c: typeof colors, longest: number) {
253
- if (!optionsToPrint.length) return;
254
-
255
- print(c.title(" OPTIONS "));
256
-
257
- println();
258
-
259
- for (const { names, placeholder, description, example, optional, default: def } of optionsToPrint) {
260
- const optLength = names.length + (placeholder?.length ?? 0);
261
- const spacing = longest + 1 - optLength;
262
- const normalizeDesc = description.replace(/\n/g, "\n" + indent(longest + 7) + c.punctuation("└"));
263
-
264
- const coloredNames = names
265
- .split(/(,)/)
266
- .map(name => (name === "," ? c.punctuation(name) : c.option(name)))
267
- .join("");
268
-
269
- println(
270
- indent(2),
271
- coloredNames,
272
- c.placeholder(placeholder),
273
- indent(spacing),
274
- c.description(normalizeDesc),
275
- def ? c.default(def) : c.optional(optional),
276
- );
277
-
278
- if (example) {
279
- const normalizeExample = example.replace(/\n/g, "\n" + indent(longest + 17));
280
- println(indent(longest + 6), c.punctuation("└") + c.exampleTitle("Example:"), c.example(normalizeExample));
281
- }
282
- }
283
-
284
- println();
285
- }
286
-
287
- function printPreparedCommands(commandsToPrint: PreparedToPrint[], c: typeof colors, longest: number) {
288
- if (!commandsToPrint.length) return;
289
-
290
- print(c.title(" COMMANDS "));
291
-
292
- println();
293
-
294
- for (const { names, placeholder, description } of commandsToPrint) {
295
- const optLength = names.length + (placeholder?.length ?? 0);
296
- const spacing = longest + 1 - optLength;
297
- const normalizeDesc = description.replace(/\n/g, "\n" + indent(longest + 7) + c.punctuation("└"));
298
-
299
- const coloredNames = names
300
- .split(/(,)/)
301
- .map(name => (name === "," ? c.punctuation(name) : c.command(name)))
302
- .join("");
303
-
304
- println(indent(2), coloredNames, c.placeholder(placeholder), indent(spacing), c.description(normalizeDesc));
305
- }
306
-
307
- println();
308
- }
309
-
310
- function printPreparedArguments(argsToPrint: PreparedToPrint[], c: typeof colors, longest: number) {
311
- if (!argsToPrint.length) return;
312
-
313
- print(c.title(" ARGUMENTS "));
314
-
315
- println();
316
-
317
- for (const { names, description, example, optional, default: def } of argsToPrint) {
318
- const spacing = longest + 2 - names.length;
319
- const normalizeDesc = description.replace(/\n/g, "\n" + indent(longest + 6) + c.punctuation("└"));
320
-
321
- println(
322
- indent(2),
323
- c.argument(names),
324
- indent(spacing),
325
- c.description(normalizeDesc),
326
- def ? c.default(def) : c.optional(optional),
327
- );
328
-
329
- if (example) {
330
- const normalizeExample = example.replace(/\n/g, "\n" + indent(longest + 16));
331
- println(indent(longest + 5), c.punctuation("└") + c.exampleTitle("Example:"), c.example(normalizeExample));
332
- }
333
- }
334
-
335
- println();
336
- }
337
-
338
- export const help = {
339
- printCliHelp,
340
- printSubcommandHelp,
341
- };