zod-args-parser 1.2.3 → 1.2.5

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 (50) hide show
  1. package/README.md +13 -7
  2. package/lib/commonjs/help-message/format-arguments.js +1 -1
  3. package/lib/commonjs/help-message/format-arguments.js.map +1 -1
  4. package/lib/commonjs/help-message/format-cli.js +1 -1
  5. package/lib/commonjs/help-message/format-cli.js.map +1 -1
  6. package/lib/commonjs/help-message/format-options.js +1 -1
  7. package/lib/commonjs/help-message/format-options.js.map +1 -1
  8. package/lib/commonjs/help-message/format-subcommands.js +1 -1
  9. package/lib/commonjs/help-message/format-subcommands.js.map +1 -1
  10. package/lib/commonjs/index.js +1 -1
  11. package/lib/commonjs/index.js.map +1 -1
  12. package/lib/commonjs/markdown/generate-markdown.js +1 -1
  13. package/lib/commonjs/markdown/generate-markdown.js.map +1 -1
  14. package/lib/commonjs/parser/validate/validate-type.js.map +1 -1
  15. package/lib/commonjs/utils.js +1 -1
  16. package/lib/commonjs/utils.js.map +1 -1
  17. package/lib/module/help-message/format-arguments.js +1 -1
  18. package/lib/module/help-message/format-arguments.js.map +1 -1
  19. package/lib/module/help-message/format-cli.js +1 -1
  20. package/lib/module/help-message/format-cli.js.map +1 -1
  21. package/lib/module/help-message/format-options.js +1 -1
  22. package/lib/module/help-message/format-options.js.map +1 -1
  23. package/lib/module/help-message/format-subcommands.js +1 -1
  24. package/lib/module/help-message/format-subcommands.js.map +1 -1
  25. package/lib/module/index.js +1 -1
  26. package/lib/module/index.js.map +1 -1
  27. package/lib/module/markdown/generate-markdown.js +1 -1
  28. package/lib/module/markdown/generate-markdown.js.map +1 -1
  29. package/lib/module/parser/validate/validate-type.js.map +1 -1
  30. package/lib/module/utils.js +1 -1
  31. package/lib/module/utils.js.map +1 -1
  32. package/lib/typescript/help-message/format-subcommands.d.ts.map +1 -1
  33. package/lib/typescript/index.d.ts +3 -3
  34. package/lib/typescript/index.d.ts.map +1 -1
  35. package/lib/typescript/parser/validate/validate-type.d.ts +3 -3
  36. package/lib/typescript/parser/validate/validate-type.d.ts.map +1 -1
  37. package/lib/typescript/types.d.ts +51 -14
  38. package/lib/typescript/types.d.ts.map +1 -1
  39. package/lib/typescript/utils.d.ts +3 -0
  40. package/lib/typescript/utils.d.ts.map +1 -1
  41. package/package.json +1 -1
  42. package/src/help-message/format-arguments.ts +2 -2
  43. package/src/help-message/format-cli.ts +5 -5
  44. package/src/help-message/format-options.ts +2 -2
  45. package/src/help-message/format-subcommands.ts +3 -4
  46. package/src/index.ts +3 -3
  47. package/src/markdown/generate-markdown.ts +2 -2
  48. package/src/parser/validate/validate-type.ts +3 -3
  49. package/src/types.ts +61 -15
  50. package/src/utils.ts +26 -0
package/src/types.ts CHANGED
@@ -7,9 +7,19 @@ export type SchemaV3 = Z3.ZodTypeAny;
7
7
  export type SchemaV4 = Z4.$ZodType;
8
8
  export type Schema = SchemaV3 | SchemaV4;
9
9
 
10
- export type ZodInfer<T extends Schema> = T extends SchemaV4 ? Z4.infer<T> : T extends SchemaV3 ? Z3.infer<T> : never;
11
-
12
- export type Subcommand = {
10
+ export type ZodInferOutput<T extends Schema> = T extends SchemaV4
11
+ ? Z4.infer<T>
12
+ : T extends SchemaV3
13
+ ? Z3.infer<T>
14
+ : never;
15
+
16
+ export type ZodInferInput<T extends Schema> = T extends SchemaV4
17
+ ? Z4.input<T>
18
+ : T extends SchemaV3
19
+ ? Z3.input<T>
20
+ : never;
21
+
22
+ export interface Subcommand {
13
23
  /**
14
24
  * - The subcommand name
15
25
  * - Make sure to not duplicate commands and aliases.
@@ -86,7 +96,7 @@ export type Subcommand = {
86
96
  * helpCommand.setPreValidationHook(ctx => console.log(ctx));
87
97
  */
88
98
  preValidation?: (ctx?: any) => any;
89
- };
99
+ }
90
100
 
91
101
  export type Cli = Prettify<
92
102
  Omit<Subcommand, "name" | "aliases" | "placeholder"> & {
@@ -95,7 +105,7 @@ export type Cli = Prettify<
95
105
  }
96
106
  >;
97
107
 
98
- export type Option = {
108
+ export interface Option {
99
109
  /**
100
110
  * The name of the option, use a valid **JavaScript** variable name.\
101
111
  * **Supports:** `camelCase`, `PascalCase`, `snake_case`, and `SCREAMING_SNAKE_CASE`.\
@@ -135,9 +145,9 @@ export type Option = {
135
145
  * - Make sure to not duplicate aliases.
136
146
  */
137
147
  aliases?: [string, ...string[]];
138
- };
148
+ }
139
149
 
140
- export type Argument = {
150
+ export interface Argument {
141
151
  /** - The name of the argument. */
142
152
  name: string;
143
153
 
@@ -159,7 +169,7 @@ export type Argument = {
159
169
  * - Used for generating the help message.
160
170
  */
161
171
  example?: string;
162
- };
172
+ }
163
173
 
164
174
  export type ColorFnType = (...text: unknown[]) => string;
165
175
 
@@ -182,23 +192,59 @@ export type HelpMsgStyle = Record<
182
192
  /**
183
193
  * - Infer the options type from a subcommand.
184
194
  *
195
+ * @deprecated Use `InferOptionsOutput` instead.
196
+ */
197
+ export type InferOptionsType<T extends Partial<Subcommand>> = InferOptionsOutput<T>;
198
+
199
+ /**
200
+ * - Infer the options output type (after zod validation) from a subcommand.
201
+ *
202
+ * @example
203
+ * const subcommand = createSubcommand({ name: "build", options: [...] });
204
+ * type OptionsType = InferOptionsOutput<typeof subcommand>;
205
+ */
206
+ export type InferOptionsOutput<T extends Partial<Subcommand>> = T["options"] extends infer U extends Option[]
207
+ ? ToOptional<{ [K in U[number]["name"]]: ZodInferOutput<Extract<U[number], { name: K }>["type"]> }>
208
+ : undefined;
209
+
210
+ /**
211
+ * - Infer the options input type (before zod validation) from a subcommand.
212
+ *
185
213
  * @example
186
214
  * const subcommand = createSubcommand({ name: "build", options: [...] });
187
- * type OptionsType = InferOptionsType<typeof subcommand>;
215
+ * type OptionsType = InferOptionsInput<typeof subcommand>;
216
+ */
217
+ export type InferOptionsInput<T extends Partial<Subcommand>> = T["options"] extends infer U extends Option[]
218
+ ? ToOptional<{ [K in U[number]["name"]]: ZodInferInput<Extract<U[number], { name: K }>["type"]> }>
219
+ : undefined;
220
+
221
+ /**
222
+ * - Infer the arguments output type (after zod validation) from a subcommand.
223
+ *
224
+ * @deprecated Use `InferArgumentsOutput` instead.
225
+ */
226
+ export type InferArgumentsType<T extends Partial<Subcommand>> = InferArgumentsOutput<T>;
227
+
228
+ /**
229
+ * - Infer the arguments output type (after zod validation) from a subcommand.
230
+ *
231
+ * @example
232
+ * const subcommand = createSubcommand({ name: "build", arguments: [...] });
233
+ * type ArgumentsType = InferArgumentsOutput<typeof subcommand>;
188
234
  */
189
- export type InferOptionsType<T extends Partial<Subcommand>> = T["options"] extends infer U extends Option[]
190
- ? ToOptional<{ [K in U[number]["name"]]: ZodInfer<Extract<U[number], { name: K }>["type"]> }>
235
+ export type InferArgumentsOutput<T extends Partial<Subcommand>> = T["arguments"] extends infer U extends Argument[]
236
+ ? { [K in keyof U]: U[K] extends { type: Schema } ? ZodInferOutput<U[K]["type"]> : never }
191
237
  : undefined;
192
238
 
193
239
  /**
194
- * - Infer the arguments type from a subcommand.
240
+ * - Infer the arguments Input type (before zod validation) from a subcommand.
195
241
  *
196
242
  * @example
197
243
  * const subcommand = createSubcommand({ name: "build", arguments: [...] });
198
- * type ArgumentsType = InferArgumentsType<typeof subcommand>;
244
+ * type ArgumentsType = InferArgumentsInput<typeof subcommand>;
199
245
  */
200
- export type InferArgumentsType<T extends Partial<Subcommand>> = T["arguments"] extends infer U extends Argument[]
201
- ? { [K in keyof U]: U[K] extends { type: Schema } ? ZodInfer<U[K]["type"]> : never }
246
+ export type InferArgumentsInput<T extends Partial<Subcommand>> = T["arguments"] extends infer U extends Argument[]
247
+ ? { [K in keyof U]: U[K] extends { type: Schema } ? ZodInferInput<U[K]["type"]> : never }
202
248
  : undefined;
203
249
 
204
250
  /** `{ some props } & { other props }` => `{ some props, other props }` */
package/src/utils.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { SubcommandMetadata } from "./metadata/metadata-types.js";
2
+
1
3
  /**
2
4
  * Converts a string to its corresponding boolean value if the string is "true" or "false" (case-insensitive).
3
5
  *
@@ -52,6 +54,7 @@ export function ln(count: number) {
52
54
 
53
55
  /** Space */
54
56
  export function indent(count: number) {
57
+ if (count <= 0) return "";
55
58
  return " ".repeat(count);
56
59
  }
57
60
 
@@ -77,3 +80,26 @@ export function insertAtEndOfFirstLine(str: string, insert: string) {
77
80
  lines[0] += " " + insert;
78
81
  return lines.join("\n");
79
82
  }
83
+
84
+ /** Get the placeholder for a subcommand */
85
+ export function subcommandPlaceholder(metadata: SubcommandMetadata): string {
86
+ let placeholder = metadata.placeholder;
87
+
88
+ if (!placeholder && metadata.options.length) {
89
+ placeholder = "[options]";
90
+ }
91
+
92
+ if (!placeholder && metadata.arguments.length) {
93
+ placeholder = "<arguments>";
94
+ }
95
+
96
+ if (metadata.allowPositional) {
97
+ placeholder += (placeholder ? " " : "") + "<positionals>";
98
+ }
99
+
100
+ if (!placeholder) {
101
+ placeholder = " ";
102
+ }
103
+
104
+ return placeholder;
105
+ }