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.
Files changed (239) hide show
  1. package/README.md +19 -19
  2. package/lib/commonjs/autocomplete-scripts/{bash-autocomplete-script.js → bash-autocomplete-script.cjs} +2 -2
  3. package/lib/commonjs/autocomplete-scripts/bash-autocomplete-script.cjs.map +1 -0
  4. package/lib/commonjs/autocomplete-scripts/{powershell-autocomplete-script.js → powershell-autocomplete-script.cjs} +3 -3
  5. package/lib/commonjs/autocomplete-scripts/powershell-autocomplete-script.cjs.map +1 -0
  6. package/lib/commonjs/autocomplete-scripts/{zsh-autocomplete-script.js → zsh-autocomplete-script.cjs} +4 -4
  7. package/lib/commonjs/autocomplete-scripts/zsh-autocomplete-script.cjs.map +1 -0
  8. package/lib/commonjs/help-message/format-arguments.cjs +1 -0
  9. package/lib/commonjs/help-message/format-arguments.cjs.map +1 -0
  10. package/lib/commonjs/help-message/format-cli.cjs +1 -0
  11. package/lib/commonjs/help-message/format-cli.cjs.map +1 -0
  12. package/lib/commonjs/help-message/format-options.cjs +1 -0
  13. package/lib/commonjs/help-message/format-options.cjs.map +1 -0
  14. package/lib/commonjs/help-message/format-subcommands.cjs +1 -0
  15. package/lib/commonjs/help-message/format-subcommands.cjs.map +1 -0
  16. package/lib/commonjs/help-message/styles.cjs +1 -0
  17. package/lib/commonjs/help-message/styles.cjs.map +1 -0
  18. package/lib/commonjs/index.cjs +1 -0
  19. package/lib/commonjs/index.cjs.map +1 -0
  20. package/lib/commonjs/markdown/generate-markdown.cjs +1 -0
  21. package/lib/commonjs/markdown/generate-markdown.cjs.map +1 -0
  22. package/lib/commonjs/metadata/get-arguments-metadata.cjs +1 -0
  23. package/lib/commonjs/metadata/get-arguments-metadata.cjs.map +1 -0
  24. package/lib/commonjs/metadata/get-cli-metadata.cjs +1 -0
  25. package/lib/commonjs/metadata/{get-cli-metadata.js.map → get-cli-metadata.cjs.map} +1 -1
  26. package/lib/commonjs/metadata/get-options-metadata.cjs +1 -0
  27. package/lib/commonjs/metadata/get-options-metadata.cjs.map +1 -0
  28. package/lib/commonjs/metadata/get-subcommands-metadata.cjs +1 -0
  29. package/lib/commonjs/metadata/get-subcommands-metadata.cjs.map +1 -0
  30. package/lib/commonjs/parser/parse/parse.cjs +1 -0
  31. package/lib/commonjs/parser/parse/parse.cjs.map +1 -0
  32. package/lib/commonjs/parser/parse/parser-helpers.cjs +1 -0
  33. package/lib/commonjs/parser/parse/parser-helpers.cjs.map +1 -0
  34. package/lib/commonjs/parser/safe-parse.cjs +1 -0
  35. package/lib/commonjs/parser/safe-parse.cjs.map +1 -0
  36. package/lib/commonjs/parser/unsafe-parse.cjs +1 -0
  37. package/lib/commonjs/parser/unsafe-parse.cjs.map +1 -0
  38. package/lib/commonjs/parser/validate/validate-type.cjs.map +1 -0
  39. package/lib/commonjs/parser/validate/validate.cjs +1 -0
  40. package/lib/commonjs/parser/validate/validate.cjs.map +1 -0
  41. package/lib/commonjs/utilities.cjs +1 -0
  42. package/lib/commonjs/utilities.cjs.map +1 -0
  43. package/lib/commonjs/zod-utilities.cjs +1 -0
  44. package/lib/commonjs/zod-utilities.cjs.map +1 -0
  45. package/lib/module/autocomplete-scripts/{bash-autocomplete-script.js → bash-autocomplete-script.mjs} +2 -2
  46. package/lib/module/autocomplete-scripts/bash-autocomplete-script.mjs.map +1 -0
  47. package/lib/module/autocomplete-scripts/powershell-autocomplete-script.mjs +31 -0
  48. package/lib/module/autocomplete-scripts/powershell-autocomplete-script.mjs.map +1 -0
  49. package/lib/module/autocomplete-scripts/{zsh-autocomplete-script.js → zsh-autocomplete-script.mjs} +4 -4
  50. package/lib/module/autocomplete-scripts/zsh-autocomplete-script.mjs.map +1 -0
  51. package/lib/module/help-message/format-arguments.mjs +1 -0
  52. package/lib/module/help-message/format-arguments.mjs.map +1 -0
  53. package/lib/module/help-message/format-cli.mjs +1 -0
  54. package/lib/module/help-message/format-cli.mjs.map +1 -0
  55. package/lib/module/help-message/format-options.mjs +1 -0
  56. package/lib/module/help-message/format-options.mjs.map +1 -0
  57. package/lib/module/help-message/format-subcommands.mjs +1 -0
  58. package/lib/module/help-message/format-subcommands.mjs.map +1 -0
  59. package/lib/module/help-message/styles.mjs +1 -0
  60. package/lib/module/help-message/styles.mjs.map +1 -0
  61. package/lib/module/index.mjs +1 -0
  62. package/lib/module/index.mjs.map +1 -0
  63. package/lib/module/markdown/generate-markdown.mjs +1 -0
  64. package/lib/module/markdown/generate-markdown.mjs.map +1 -0
  65. package/lib/module/metadata/get-arguments-metadata.mjs +1 -0
  66. package/lib/module/metadata/get-arguments-metadata.mjs.map +1 -0
  67. package/lib/module/metadata/get-cli-metadata.mjs +1 -0
  68. package/lib/module/metadata/{get-cli-metadata.js.map → get-cli-metadata.mjs.map} +1 -1
  69. package/lib/module/metadata/get-options-metadata.mjs +1 -0
  70. package/lib/module/metadata/get-options-metadata.mjs.map +1 -0
  71. package/lib/module/metadata/get-subcommands-metadata.mjs +1 -0
  72. package/lib/module/metadata/get-subcommands-metadata.mjs.map +1 -0
  73. package/lib/module/parser/parse/parse.mjs +1 -0
  74. package/lib/module/parser/parse/parse.mjs.map +1 -0
  75. package/lib/module/parser/parse/parser-helpers.mjs +1 -0
  76. package/lib/module/parser/parse/parser-helpers.mjs.map +1 -0
  77. package/lib/module/parser/safe-parse.mjs +1 -0
  78. package/lib/module/parser/safe-parse.mjs.map +1 -0
  79. package/lib/module/parser/unsafe-parse.mjs +1 -0
  80. package/lib/module/parser/unsafe-parse.mjs.map +1 -0
  81. package/lib/module/parser/validate/validate-type.mjs.map +1 -0
  82. package/lib/module/parser/validate/validate.mjs +1 -0
  83. package/lib/module/parser/validate/validate.mjs.map +1 -0
  84. package/lib/module/utilities.mjs +1 -0
  85. package/lib/module/utilities.mjs.map +1 -0
  86. package/lib/module/zod-utilities.mjs +1 -0
  87. package/lib/module/zod-utilities.mjs.map +1 -0
  88. package/lib/typescript/autocomplete-scripts/bash-autocomplete-script.d.ts +1 -1
  89. package/lib/typescript/autocomplete-scripts/bash-autocomplete-script.d.ts.map +1 -1
  90. package/lib/typescript/autocomplete-scripts/powershell-autocomplete-script.d.ts +1 -1
  91. package/lib/typescript/autocomplete-scripts/powershell-autocomplete-script.d.ts.map +1 -1
  92. package/lib/typescript/autocomplete-scripts/zsh-autocomplete-script.d.ts +1 -1
  93. package/lib/typescript/autocomplete-scripts/zsh-autocomplete-script.d.ts.map +1 -1
  94. package/lib/typescript/help-message/format-arguments.d.ts +2 -2
  95. package/lib/typescript/help-message/format-arguments.d.ts.map +1 -1
  96. package/lib/typescript/help-message/format-cli.d.ts +5 -5
  97. package/lib/typescript/help-message/format-cli.d.ts.map +1 -1
  98. package/lib/typescript/help-message/format-options.d.ts +2 -2
  99. package/lib/typescript/help-message/format-options.d.ts.map +1 -1
  100. package/lib/typescript/help-message/format-subcommands.d.ts +2 -2
  101. package/lib/typescript/help-message/format-subcommands.d.ts.map +1 -1
  102. package/lib/typescript/help-message/styles.d.ts +14 -13
  103. package/lib/typescript/help-message/styles.d.ts.map +1 -1
  104. package/lib/typescript/index.d.ts +10 -10
  105. package/lib/typescript/index.d.ts.map +1 -1
  106. package/lib/typescript/markdown/generate-markdown.d.ts +1 -1
  107. package/lib/typescript/markdown/generate-markdown.d.ts.map +1 -1
  108. package/lib/typescript/metadata/get-arguments-metadata.d.ts +1 -1
  109. package/lib/typescript/metadata/get-arguments-metadata.d.ts.map +1 -1
  110. package/lib/typescript/metadata/get-options-metadata.d.ts.map +1 -1
  111. package/lib/typescript/metadata/get-subcommands-metadata.d.ts.map +1 -1
  112. package/lib/typescript/parser/parse/parse-types.d.ts +5 -5
  113. package/lib/typescript/parser/parse/parse-types.d.ts.map +1 -1
  114. package/lib/typescript/parser/parse/parse.d.ts +2 -2
  115. package/lib/typescript/parser/parse/parse.d.ts.map +1 -1
  116. package/lib/typescript/parser/parse/parser-helpers.d.ts +7 -7
  117. package/lib/typescript/parser/parse/parser-helpers.d.ts.map +1 -1
  118. package/lib/typescript/parser/safe-parse.d.ts +2 -2
  119. package/lib/typescript/parser/safe-parse.d.ts.map +1 -1
  120. package/lib/typescript/parser/unsafe-parse.d.ts +2 -2
  121. package/lib/typescript/parser/unsafe-parse.d.ts.map +1 -1
  122. package/lib/typescript/parser/validate/validate-type.d.ts +4 -4
  123. package/lib/typescript/parser/validate/validate-type.d.ts.map +1 -1
  124. package/lib/typescript/parser/validate/validate.d.ts +4 -4
  125. package/lib/typescript/parser/validate/validate.d.ts.map +1 -1
  126. package/lib/typescript/types.d.ts +19 -15
  127. package/lib/typescript/types.d.ts.map +1 -1
  128. package/lib/typescript/{utils.d.ts → utilities.d.ts} +9 -9
  129. package/lib/typescript/utilities.d.ts.map +1 -0
  130. package/lib/typescript/{zod-utils.d.ts → zod-utilities.d.ts} +11 -10
  131. package/lib/typescript/zod-utilities.d.ts.map +1 -0
  132. package/package.json +17 -18
  133. package/src/autocomplete-scripts/bash-autocomplete-script.ts +10 -10
  134. package/src/autocomplete-scripts/powershell-autocomplete-script.ts +17 -15
  135. package/src/autocomplete-scripts/zsh-autocomplete-script.ts +8 -5
  136. package/src/help-message/format-arguments.ts +20 -13
  137. package/src/help-message/format-cli.ts +65 -45
  138. package/src/help-message/format-options.ts +19 -12
  139. package/src/help-message/format-subcommands.ts +9 -9
  140. package/src/help-message/styles.ts +18 -18
  141. package/src/index.ts +21 -21
  142. package/src/markdown/generate-markdown.ts +55 -55
  143. package/src/metadata/get-arguments-metadata.ts +10 -10
  144. package/src/metadata/get-options-metadata.ts +8 -6
  145. package/src/metadata/get-subcommands-metadata.ts +3 -1
  146. package/src/parser/parse/parse-types.ts +5 -5
  147. package/src/parser/parse/parse.ts +67 -64
  148. package/src/parser/parse/parser-helpers.ts +24 -25
  149. package/src/parser/safe-parse.ts +24 -18
  150. package/src/parser/unsafe-parse.ts +34 -34
  151. package/src/parser/validate/validate-type.ts +4 -4
  152. package/src/parser/validate/validate.ts +17 -17
  153. package/src/types.ts +21 -17
  154. package/src/{utils.ts → utilities.ts} +44 -28
  155. package/src/zod-utilities.ts +214 -0
  156. package/lib/commonjs/autocomplete-scripts/bash-autocomplete-script.js.map +0 -1
  157. package/lib/commonjs/autocomplete-scripts/powershell-autocomplete-script.js.map +0 -1
  158. package/lib/commonjs/autocomplete-scripts/zsh-autocomplete-script.js.map +0 -1
  159. package/lib/commonjs/help-message/format-arguments.js +0 -1
  160. package/lib/commonjs/help-message/format-arguments.js.map +0 -1
  161. package/lib/commonjs/help-message/format-cli.js +0 -1
  162. package/lib/commonjs/help-message/format-cli.js.map +0 -1
  163. package/lib/commonjs/help-message/format-options.js +0 -1
  164. package/lib/commonjs/help-message/format-options.js.map +0 -1
  165. package/lib/commonjs/help-message/format-subcommands.js +0 -1
  166. package/lib/commonjs/help-message/format-subcommands.js.map +0 -1
  167. package/lib/commonjs/help-message/styles.js +0 -1
  168. package/lib/commonjs/help-message/styles.js.map +0 -1
  169. package/lib/commonjs/index.js +0 -1
  170. package/lib/commonjs/index.js.map +0 -1
  171. package/lib/commonjs/markdown/generate-markdown.js +0 -1
  172. package/lib/commonjs/markdown/generate-markdown.js.map +0 -1
  173. package/lib/commonjs/metadata/get-arguments-metadata.js +0 -1
  174. package/lib/commonjs/metadata/get-arguments-metadata.js.map +0 -1
  175. package/lib/commonjs/metadata/get-cli-metadata.js +0 -1
  176. package/lib/commonjs/metadata/get-options-metadata.js +0 -1
  177. package/lib/commonjs/metadata/get-options-metadata.js.map +0 -1
  178. package/lib/commonjs/metadata/get-subcommands-metadata.js +0 -1
  179. package/lib/commonjs/metadata/get-subcommands-metadata.js.map +0 -1
  180. package/lib/commonjs/parser/parse/parse.js +0 -1
  181. package/lib/commonjs/parser/parse/parse.js.map +0 -1
  182. package/lib/commonjs/parser/parse/parser-helpers.js +0 -1
  183. package/lib/commonjs/parser/parse/parser-helpers.js.map +0 -1
  184. package/lib/commonjs/parser/safe-parse.js +0 -1
  185. package/lib/commonjs/parser/safe-parse.js.map +0 -1
  186. package/lib/commonjs/parser/unsafe-parse.js +0 -1
  187. package/lib/commonjs/parser/unsafe-parse.js.map +0 -1
  188. package/lib/commonjs/parser/validate/validate-type.js.map +0 -1
  189. package/lib/commonjs/parser/validate/validate.js +0 -1
  190. package/lib/commonjs/parser/validate/validate.js.map +0 -1
  191. package/lib/commonjs/utils.js +0 -1
  192. package/lib/commonjs/utils.js.map +0 -1
  193. package/lib/commonjs/zod-utils.js +0 -1
  194. package/lib/commonjs/zod-utils.js.map +0 -1
  195. package/lib/module/autocomplete-scripts/bash-autocomplete-script.js.map +0 -1
  196. package/lib/module/autocomplete-scripts/powershell-autocomplete-script.js +0 -31
  197. package/lib/module/autocomplete-scripts/powershell-autocomplete-script.js.map +0 -1
  198. package/lib/module/autocomplete-scripts/zsh-autocomplete-script.js.map +0 -1
  199. package/lib/module/help-message/format-arguments.js +0 -1
  200. package/lib/module/help-message/format-arguments.js.map +0 -1
  201. package/lib/module/help-message/format-cli.js +0 -1
  202. package/lib/module/help-message/format-cli.js.map +0 -1
  203. package/lib/module/help-message/format-options.js +0 -1
  204. package/lib/module/help-message/format-options.js.map +0 -1
  205. package/lib/module/help-message/format-subcommands.js +0 -1
  206. package/lib/module/help-message/format-subcommands.js.map +0 -1
  207. package/lib/module/help-message/styles.js +0 -1
  208. package/lib/module/help-message/styles.js.map +0 -1
  209. package/lib/module/index.js +0 -1
  210. package/lib/module/index.js.map +0 -1
  211. package/lib/module/markdown/generate-markdown.js +0 -1
  212. package/lib/module/markdown/generate-markdown.js.map +0 -1
  213. package/lib/module/metadata/get-arguments-metadata.js +0 -1
  214. package/lib/module/metadata/get-arguments-metadata.js.map +0 -1
  215. package/lib/module/metadata/get-cli-metadata.js +0 -1
  216. package/lib/module/metadata/get-options-metadata.js +0 -1
  217. package/lib/module/metadata/get-options-metadata.js.map +0 -1
  218. package/lib/module/metadata/get-subcommands-metadata.js +0 -1
  219. package/lib/module/metadata/get-subcommands-metadata.js.map +0 -1
  220. package/lib/module/parser/parse/parse.js +0 -1
  221. package/lib/module/parser/parse/parse.js.map +0 -1
  222. package/lib/module/parser/parse/parser-helpers.js +0 -1
  223. package/lib/module/parser/parse/parser-helpers.js.map +0 -1
  224. package/lib/module/parser/safe-parse.js +0 -1
  225. package/lib/module/parser/safe-parse.js.map +0 -1
  226. package/lib/module/parser/unsafe-parse.js +0 -1
  227. package/lib/module/parser/unsafe-parse.js.map +0 -1
  228. package/lib/module/parser/validate/validate-type.js.map +0 -1
  229. package/lib/module/parser/validate/validate.js +0 -1
  230. package/lib/module/parser/validate/validate.js.map +0 -1
  231. package/lib/module/utils.js +0 -1
  232. package/lib/module/utils.js.map +0 -1
  233. package/lib/module/zod-utils.js +0 -1
  234. package/lib/module/zod-utils.js.map +0 -1
  235. package/lib/typescript/utils.d.ts.map +0 -1
  236. package/lib/typescript/zod-utils.d.ts.map +0 -1
  237. package/src/zod-utils.ts +0 -199
  238. /package/lib/commonjs/parser/validate/{validate-type.js → validate-type.cjs} +0 -0
  239. /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, subcommandArr: Subcommand[]): Subcommand | undefined {
12
- return subcommandArr.find(c => {
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(optionArg: string, options: [Option, ...Option[]]): Option | undefined {
33
- const validVarNames = optionArgToVarNames(optionArg);
34
- const isNegative = optionArg.startsWith("--no-");
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 (validVarNames.has(o.name)) {
37
+ if (validVariableNames.has(o.name)) {
38
38
  return true;
39
39
  }
40
40
 
41
- if (isNegative && validVarNames.has(negateOption(o.name))) {
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 => validVarNames.has(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 => validVarNames.has(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(args: string[]): string[] {
64
+ export function decoupleFlags(arguments_: string[]): string[] {
65
65
  const flagsRe = /^-[a-z0-9]{2,}$/i;
66
66
 
67
67
  const result = [];
68
- for (let i = 0; i < args.length; i++) {
69
- const arg = args[i];
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(arg);
72
+ result.push(argument);
74
73
  continue;
75
74
  }
76
75
 
77
- const decoupledArr = arg
78
- .substring(1)
76
+ const decoupledArray = argument
77
+ .slice(1)
79
78
  .split("")
80
79
  .map(c => "-" + c);
81
80
 
82
- result.push(...decoupledArr);
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 optionArgToVarNames(name: string): Set<string> {
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.substring(2) : name.substring(1); // remove prefix
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 isFlagArg(name: string): boolean {
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 isLongArg(name: string): boolean {
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 isOptionArg(name: string | boolean): boolean {
130
+ export function isOptionArgument(name: string | boolean): boolean {
132
131
  if (typeof name !== "string") {
133
132
  return false;
134
133
  }
135
134
 
136
- return isFlagArg(name) || isLongArg(name);
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 transformOptionToArg(name: string): string {
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, (m, i) => (i ? "-" + m : m)); // add "-" before camel case letters except for the first letter
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
  }
@@ -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
- ...params: [U, ...T]
9
+ ...parameters: [U, ...T]
10
10
  ): SafeParseResult<[...T, NoSubcommand & U]> {
11
- const cliOptions = ("cliName" in params[0] ? params[0] : {}) as U;
12
- const subcommandArr = params as Subcommand[];
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(params, style);
18
+ const printCliHelp: PrintCli = style => help.printCliHelp(parameters, style);
19
19
 
20
20
  const printSubcommandHelp: PrintSubcommand = (subCmdName, style) => {
21
- const subcommand = findSubcommand(subCmdName, subcommandArr);
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, ...params);
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 (e) {
43
- if (!(e instanceof Error) || e.cause !== "zod-args-parser") throw e;
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: e as 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
- ...params: [U, ...T]
58
+ ...parameters: [U, ...T]
56
59
  ): Promise<SafeParseResult<[...T, NoSubcommand & U]>> {
57
- const cliOptions = ("cliName" in params[0] ? params[0] : {}) as U;
58
- const subcommandArr = params as Subcommand[];
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(params, style);
67
+ const printCliHelp: PrintCli = style => help.printCliHelp(parameters, style);
65
68
 
66
69
  const printSubcommandHelp: PrintSubcommand = (subCmdName, style) => {
67
- const subcommand = findSubcommand(subCmdName, subcommandArr);
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, ...params);
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 (e) {
89
- if (!(e instanceof Error) || e.cause !== "zod-args-parser") throw e;
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: e as 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, NoSubcommand, HelpMsgStyle, Subcommand, UnsafeParseResult } from "../types.js";
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
- ...params: [U, ...T]
10
+ ...parameters: [U, ...T]
11
11
  ): UnsafeParseResult<[...T, NoSubcommand & U]> {
12
- const cliOptions = ("cliName" in params[0] ? params[0] : {}) as U;
13
- const subcommandArr = params as unknown as T;
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, ...params);
16
+ const parsedData = parse(argv, ...parameters);
17
17
 
18
- const subcommandObj = findSubcommand(parsedData.subcommand, subcommandArr);
19
- if (!subcommandObj) {
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 (subcommandObj.preValidation) {
25
- subcommandObj.preValidation(parsedData);
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<HelpMsgStyle>) {
33
- help.printCliHelp(params, style);
32
+ printCliHelp(style?: Partial<HelpMessageStyle>) {
33
+ help.printCliHelp(parameters, style);
34
34
  },
35
- printSubcommandHelp(subCmdName: string, style?: Partial<HelpMsgStyle>) {
36
- const subcommandObj = findSubcommand(subCmdName, subcommandArr);
37
- if (!subcommandObj) {
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(subcommandObj, style, cliOptions.cliName);
42
+ help.printSubcommandHelp(subcommandObject, style, cliOptions.cliName);
43
43
  },
44
44
  });
45
45
 
46
46
  // Fire action
47
- if (subcommandObj.action) {
48
- subcommandObj.action(validateResult);
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
- ...params: [U, ...T]
56
+ ...parameters: [U, ...T]
57
57
  ): Promise<UnsafeParseResult<[...T, NoSubcommand & U]>> {
58
- const cliOptions = ("cliName" in params[0] ? params[0] : {}) as U;
59
- const subcommandArr = params as unknown as T;
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, ...params);
62
+ const parsedData = parse(argv, ...parameters);
63
63
 
64
- const subcommandObj = findSubcommand(parsedData.subcommand, subcommandArr);
65
- if (!subcommandObj) {
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 (subcommandObj.preValidation) {
71
- await subcommandObj.preValidation(parsedData);
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<HelpMsgStyle>) {
79
- help.printCliHelp(params, style);
78
+ printCliHelp(style?: Partial<HelpMessageStyle>) {
79
+ help.printCliHelp(parameters, style);
80
80
  },
81
- printSubcommandHelp(subCmdName: string, style?: Partial<HelpMsgStyle>) {
82
- const subcommandObj = findSubcommand(subCmdName, subcommandArr);
83
- if (!subcommandObj) {
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(subcommandObj, style, cliOptions.cliName);
88
+ help.printSubcommandHelp(subcommandObject, style, cliOptions.cliName);
89
89
  },
90
90
  });
91
91
 
92
92
  // Fire action
93
- if (subcommandObj.action) {
94
- await subcommandObj.action(validateResult);
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 OptionsArr2RecordType<T extends Option[] | undefined> = T extends Option[]
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 ArgumentsArr2ArrType<T extends Argument[] | undefined> = T extends Argument[]
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: ArgumentsArr2ArrType<S[K]["arguments"]>;
16
+ arguments: ArgumentsArray2ArrayType<S[K]["arguments"]>;
17
17
  positional: S[K]["allowPositional"] extends true ? string[] : never;
18
18
  ctx: ParseResult<S>;
19
- } & OptionsArr2RecordType<S[K]["options"]>
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 "../../utils.js";
4
- import { isBooleanSchema, safeParseSchema } from "../../zod-utils.js";
3
+ import { generateOrdinalSuffix, stringToBoolean } from "../../utilities.js";
4
+ import { isBooleanSchema, safeParseSchema } from "../../zod-utilities.js";
5
5
 
6
- import type { ParseCtx } from "../parse/parse-types.js";
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 ResultsTempType = Record<string, unknown> & {
9
+ type ResultsTemporaryType = Record<string, unknown> & {
10
10
  subcommand: string | undefined;
11
11
  positional?: string[];
12
12
  arguments?: unknown[];
13
- ctx: ParseCtx;
13
+ ctx: ParsedContext;
14
14
  };
15
15
 
16
- export function validate(parsedData: ParseCtx) {
17
- const results: ResultsTempType = {
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 res = safeParseSchema(schema, optionsValue);
37
- if (!res.success) {
38
- throw new Error(`Invalid value "${rawValue}" for "${flag}": ${prettifyError(res.error)}`, {
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] = res.data;
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 argValue = rawValue && isBooleanSchema(schema) ? stringToBoolean(rawValue) : rawValue;
51
+ const argumentValue = rawValue && isBooleanSchema(schema) ? stringToBoolean(rawValue) : rawValue;
52
52
 
53
- const res = safeParseSchema(schema, argValue);
54
- if (!res.success) {
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(res.error)}`,
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(res.data);
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?: (results?: any) => any;
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?: (ctx?: any) => any;
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 ColorFnType = (...text: unknown[]) => string;
174
+ export type ColorFunctionType = (...text: unknown[]) => string;
175
175
 
176
176
  /** - The colors to use for the help message. */
177
- export type HelpMsgStyle = Record<
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
- ColorFnType
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<HelpMsgStyle>) => void;
268
- printSubcommandHelp: (subcommand: LiteralUnion<NonNullable<N>>, style?: Partial<HelpMsgStyle>) => void;
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 ActionsFn<T extends Subcommand | Cli> = {
285
- setAction: (actions: (res: UnsafeParseResult<[T]>) => void) => void;
286
- setPreValidationHook: (hookFn: (ctx: ParseResult<[T]>) => void) => void;
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 MapNameAndAliases2StrArr<T extends { name?: string; aliases?: string[] }[]> = T extends [
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
- ? [First["name"], ...(First["aliases"] extends string[] ? First["aliases"] : []), ...MapNameAndAliases2StrArr<Rest>]
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 IsDuplicatesInArr<Input extends any[]> = Input extends [infer Item, ...infer Rest]
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
- : IsDuplicatesInArr<Rest>
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
- ? IsDuplicatesInArr<MapNameAndAliases2StrArr<O>> extends infer Name extends string
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
- IsDuplicatesInArr<MapNameAndAliases2StrArr<T>> extends infer Name extends string
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
- ? IsDuplicatesInArr<MapNameAndAliases2StrArr<A>> extends infer Name extends string
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;