zod-args-parser 2.0.0-beta.0 → 2.0.0-beta.2

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 (475) hide show
  1. package/LICENSE +1 -0
  2. package/README.md +230 -289
  3. package/lib/esm/autocomplete-scripts/bash-autocomplete-script.js +74 -2
  4. package/lib/esm/autocomplete-scripts/bash-autocomplete-script.js.map +7 -1
  5. package/lib/esm/autocomplete-scripts/powershell-autocomplete-script.js +90 -2
  6. package/lib/esm/autocomplete-scripts/powershell-autocomplete-script.js.map +7 -1
  7. package/lib/esm/autocomplete-scripts/zsh-autocomplete-script.js +39 -13
  8. package/lib/esm/autocomplete-scripts/zsh-autocomplete-script.js.map +7 -1
  9. package/lib/esm/cli-error/cli-error.js +38 -0
  10. package/lib/esm/cli-error/cli-error.js.map +7 -0
  11. package/lib/esm/cli-error/error-cause.js +12 -0
  12. package/lib/esm/cli-error/error-cause.js.map +7 -0
  13. package/lib/esm/cli-error/error-code/definition-error-code.js +23 -0
  14. package/lib/esm/cli-error/error-code/definition-error-code.js.map +7 -0
  15. package/lib/esm/cli-error/error-code/internal-error-code.js +10 -0
  16. package/lib/esm/cli-error/error-code/internal-error-code.js.map +7 -0
  17. package/lib/esm/cli-error/error-code/parse-error-code.js +18 -0
  18. package/lib/esm/cli-error/error-code/parse-error-code.js.map +7 -0
  19. package/lib/esm/cli-error/error-code/validation-error-code.js +17 -0
  20. package/lib/esm/cli-error/error-code/validation-error-code.js.map +7 -0
  21. package/lib/esm/cli-error/error-message/definition-error-message.js +70 -0
  22. package/lib/esm/cli-error/error-message/definition-error-message.js.map +7 -0
  23. package/lib/esm/cli-error/error-message/internal-error-message.js +16 -0
  24. package/lib/esm/cli-error/error-message/internal-error-message.js.map +7 -0
  25. package/lib/esm/cli-error/error-message/parse-error-message.js +40 -0
  26. package/lib/esm/cli-error/error-message/parse-error-message.js.map +7 -0
  27. package/lib/esm/cli-error/error-message/validation-error-message.js +60 -0
  28. package/lib/esm/cli-error/error-message/validation-error-message.js.map +7 -0
  29. package/lib/esm/coerce/coerce-methods.js +197 -2
  30. package/lib/esm/coerce/coerce-methods.js.map +7 -1
  31. package/lib/esm/coerce/string-to-array.js +17 -2
  32. package/lib/esm/coerce/string-to-array.js.map +7 -1
  33. package/lib/esm/coerce/string-to-primitive.js +73 -2
  34. package/lib/esm/coerce/string-to-primitive.js.map +7 -1
  35. package/lib/esm/coerce/string-to-set.js +17 -2
  36. package/lib/esm/coerce/string-to-set.js.map +7 -1
  37. package/lib/esm/definitions/define-arguments.js +10 -2
  38. package/lib/esm/definitions/define-arguments.js.map +7 -1
  39. package/lib/esm/definitions/define-cli.js +89 -2
  40. package/lib/esm/definitions/define-cli.js.map +7 -1
  41. package/lib/esm/definitions/define-options.js +10 -2
  42. package/lib/esm/definitions/define-options.js.map +7 -1
  43. package/lib/esm/definitions/define-subcommand.js +56 -2
  44. package/lib/esm/definitions/define-subcommand.js.map +7 -1
  45. package/lib/esm/definitions/validate-cli-definition.js +360 -2
  46. package/lib/esm/definitions/validate-cli-definition.js.map +7 -1
  47. package/lib/esm/help-message/format-arguments.js +48 -2
  48. package/lib/esm/help-message/format-arguments.js.map +7 -1
  49. package/lib/esm/help-message/format-options.js +50 -2
  50. package/lib/esm/help-message/format-options.js.map +7 -1
  51. package/lib/esm/help-message/format-subcommands.js +37 -2
  52. package/lib/esm/help-message/format-subcommands.js.map +7 -1
  53. package/lib/esm/help-message/generate-for-cli.js +71 -2
  54. package/lib/esm/help-message/generate-for-cli.js.map +7 -1
  55. package/lib/esm/help-message/generate-for-subcommand.js +23 -2
  56. package/lib/esm/help-message/generate-for-subcommand.js.map +7 -1
  57. package/lib/esm/help-message/print-help.js +14 -2
  58. package/lib/esm/help-message/print-help.js.map +7 -1
  59. package/lib/esm/help-message/set-defaults.js +29 -2
  60. package/lib/esm/help-message/set-defaults.js.map +7 -1
  61. package/lib/esm/help-message/styles.js +154 -2
  62. package/lib/esm/help-message/styles.js.map +7 -1
  63. package/lib/esm/help-message/terminal-markdown.js +121 -2
  64. package/lib/esm/help-message/terminal-markdown.js.map +7 -1
  65. package/lib/esm/index.js +52 -2
  66. package/lib/esm/index.js.map +7 -1
  67. package/lib/esm/markdown/generate-markdown.js +153 -2
  68. package/lib/esm/markdown/generate-markdown.js.map +7 -1
  69. package/lib/esm/metadata/arguments-metadata.js +26 -2
  70. package/lib/esm/metadata/arguments-metadata.js.map +7 -1
  71. package/lib/esm/metadata/cli-metadata.js +24 -2
  72. package/lib/esm/metadata/cli-metadata.js.map +7 -1
  73. package/lib/esm/metadata/options-metadata.js +35 -2
  74. package/lib/esm/metadata/options-metadata.js.map +7 -1
  75. package/lib/esm/metadata/subcommands-metadata.js +32 -2
  76. package/lib/esm/metadata/subcommands-metadata.js.map +7 -1
  77. package/lib/esm/parse/context/cli-context-builder.js +283 -2
  78. package/lib/esm/parse/context/cli-context-builder.js.map +7 -1
  79. package/lib/esm/parse/context/object-context-builder.js +62 -2
  80. package/lib/esm/parse/context/object-context-builder.js.map +7 -1
  81. package/lib/esm/parse/parser-utilities.js +108 -2
  82. package/lib/esm/parse/parser-utilities.js.map +7 -1
  83. package/lib/esm/parse/safe-parse.js +57 -2
  84. package/lib/esm/parse/safe-parse.js.map +7 -1
  85. package/lib/esm/parse/validation/validate-context.js +17 -2
  86. package/lib/esm/parse/validation/validate-context.js.map +7 -1
  87. package/lib/esm/parse/validation/validators/arguments.js +67 -2
  88. package/lib/esm/parse/validation/validators/arguments.js.map +7 -1
  89. package/lib/esm/parse/validation/validators/conflict.js +38 -2
  90. package/lib/esm/parse/validation/validators/conflict.js.map +7 -1
  91. package/lib/esm/parse/validation/validators/exclusive.js +39 -2
  92. package/lib/esm/parse/validation/validators/exclusive.js.map +7 -1
  93. package/lib/esm/parse/validation/validators/explicitly-passed.js +18 -2
  94. package/lib/esm/parse/validation/validators/explicitly-passed.js.map +7 -1
  95. package/lib/esm/parse/validation/validators/options.js +67 -2
  96. package/lib/esm/parse/validation/validators/options.js.map +7 -1
  97. package/lib/esm/parse/validation/validators/requires.js +43 -2
  98. package/lib/esm/parse/validation/validators/requires.js.map +7 -1
  99. package/lib/esm/utilities/parse-argv.js +61 -0
  100. package/lib/esm/utilities/parse-argv.js.map +7 -0
  101. package/lib/esm/utilities/schema-utilities.js +57 -0
  102. package/lib/esm/utilities/schema-utilities.js.map +7 -0
  103. package/lib/esm/utilities/utilities.js +122 -0
  104. package/lib/esm/utilities/utilities.js.map +7 -0
  105. package/lib/iife/index.js +1 -1
  106. package/lib/iife/index.js.map +1 -1
  107. package/lib/mjs/autocomplete-scripts/bash-autocomplete-script.mjs +74 -2
  108. package/lib/mjs/autocomplete-scripts/bash-autocomplete-script.mjs.map +7 -1
  109. package/lib/mjs/autocomplete-scripts/powershell-autocomplete-script.mjs +90 -2
  110. package/lib/mjs/autocomplete-scripts/powershell-autocomplete-script.mjs.map +7 -1
  111. package/lib/mjs/autocomplete-scripts/zsh-autocomplete-script.mjs +39 -13
  112. package/lib/mjs/autocomplete-scripts/zsh-autocomplete-script.mjs.map +7 -1
  113. package/lib/mjs/cli-error/cli-error.mjs +38 -0
  114. package/lib/mjs/cli-error/cli-error.mjs.map +7 -0
  115. package/lib/mjs/cli-error/error-cause.mjs +12 -0
  116. package/lib/mjs/cli-error/error-cause.mjs.map +7 -0
  117. package/lib/mjs/cli-error/error-code/definition-error-code.mjs +23 -0
  118. package/lib/mjs/cli-error/error-code/definition-error-code.mjs.map +7 -0
  119. package/lib/mjs/cli-error/error-code/internal-error-code.mjs +10 -0
  120. package/lib/mjs/cli-error/error-code/internal-error-code.mjs.map +7 -0
  121. package/lib/mjs/cli-error/error-code/parse-error-code.mjs +18 -0
  122. package/lib/mjs/cli-error/error-code/parse-error-code.mjs.map +7 -0
  123. package/lib/mjs/cli-error/error-code/validation-error-code.mjs +17 -0
  124. package/lib/mjs/cli-error/error-code/validation-error-code.mjs.map +7 -0
  125. package/lib/mjs/cli-error/error-message/definition-error-message.mjs +70 -0
  126. package/lib/mjs/cli-error/error-message/definition-error-message.mjs.map +7 -0
  127. package/lib/mjs/cli-error/error-message/internal-error-message.mjs +16 -0
  128. package/lib/mjs/cli-error/error-message/internal-error-message.mjs.map +7 -0
  129. package/lib/mjs/cli-error/error-message/parse-error-message.mjs +40 -0
  130. package/lib/mjs/cli-error/error-message/parse-error-message.mjs.map +7 -0
  131. package/lib/mjs/cli-error/error-message/validation-error-message.mjs +60 -0
  132. package/lib/mjs/cli-error/error-message/validation-error-message.mjs.map +7 -0
  133. package/lib/mjs/coerce/coerce-methods.mjs +197 -2
  134. package/lib/mjs/coerce/coerce-methods.mjs.map +7 -1
  135. package/lib/mjs/coerce/string-to-array.mjs +17 -2
  136. package/lib/mjs/coerce/string-to-array.mjs.map +7 -1
  137. package/lib/mjs/coerce/string-to-primitive.mjs +73 -2
  138. package/lib/mjs/coerce/string-to-primitive.mjs.map +7 -1
  139. package/lib/mjs/coerce/string-to-set.mjs +17 -2
  140. package/lib/mjs/coerce/string-to-set.mjs.map +7 -1
  141. package/lib/mjs/definitions/define-arguments.mjs +10 -2
  142. package/lib/mjs/definitions/define-arguments.mjs.map +7 -1
  143. package/lib/mjs/definitions/define-cli.mjs +89 -2
  144. package/lib/mjs/definitions/define-cli.mjs.map +7 -1
  145. package/lib/mjs/definitions/define-options.mjs +10 -2
  146. package/lib/mjs/definitions/define-options.mjs.map +7 -1
  147. package/lib/mjs/definitions/define-subcommand.mjs +56 -2
  148. package/lib/mjs/definitions/define-subcommand.mjs.map +7 -1
  149. package/lib/mjs/definitions/validate-cli-definition.mjs +360 -2
  150. package/lib/mjs/definitions/validate-cli-definition.mjs.map +7 -1
  151. package/lib/mjs/help-message/format-arguments.mjs +48 -2
  152. package/lib/mjs/help-message/format-arguments.mjs.map +7 -1
  153. package/lib/mjs/help-message/format-options.mjs +50 -2
  154. package/lib/mjs/help-message/format-options.mjs.map +7 -1
  155. package/lib/mjs/help-message/format-subcommands.mjs +37 -2
  156. package/lib/mjs/help-message/format-subcommands.mjs.map +7 -1
  157. package/lib/mjs/help-message/generate-for-cli.mjs +71 -2
  158. package/lib/mjs/help-message/generate-for-cli.mjs.map +7 -1
  159. package/lib/mjs/help-message/generate-for-subcommand.mjs +23 -2
  160. package/lib/mjs/help-message/generate-for-subcommand.mjs.map +7 -1
  161. package/lib/mjs/help-message/print-help.mjs +14 -2
  162. package/lib/mjs/help-message/print-help.mjs.map +7 -1
  163. package/lib/mjs/help-message/set-defaults.mjs +29 -2
  164. package/lib/mjs/help-message/set-defaults.mjs.map +7 -1
  165. package/lib/mjs/help-message/styles.mjs +154 -2
  166. package/lib/mjs/help-message/styles.mjs.map +7 -1
  167. package/lib/mjs/help-message/terminal-markdown.mjs +121 -2
  168. package/lib/mjs/help-message/terminal-markdown.mjs.map +7 -1
  169. package/lib/mjs/index.mjs +52 -2
  170. package/lib/mjs/index.mjs.map +7 -1
  171. package/lib/mjs/markdown/generate-markdown.mjs +153 -2
  172. package/lib/mjs/markdown/generate-markdown.mjs.map +7 -1
  173. package/lib/mjs/metadata/arguments-metadata.mjs +26 -2
  174. package/lib/mjs/metadata/arguments-metadata.mjs.map +7 -1
  175. package/lib/mjs/metadata/cli-metadata.mjs +24 -2
  176. package/lib/mjs/metadata/cli-metadata.mjs.map +7 -1
  177. package/lib/mjs/metadata/options-metadata.mjs +35 -2
  178. package/lib/mjs/metadata/options-metadata.mjs.map +7 -1
  179. package/lib/mjs/metadata/subcommands-metadata.mjs +32 -2
  180. package/lib/mjs/metadata/subcommands-metadata.mjs.map +7 -1
  181. package/lib/mjs/parse/context/cli-context-builder.mjs +283 -2
  182. package/lib/mjs/parse/context/cli-context-builder.mjs.map +7 -1
  183. package/lib/mjs/parse/context/object-context-builder.mjs +62 -2
  184. package/lib/mjs/parse/context/object-context-builder.mjs.map +7 -1
  185. package/lib/mjs/parse/parser-utilities.mjs +108 -2
  186. package/lib/mjs/parse/parser-utilities.mjs.map +7 -1
  187. package/lib/mjs/parse/safe-parse.mjs +57 -2
  188. package/lib/mjs/parse/safe-parse.mjs.map +7 -1
  189. package/lib/mjs/parse/validation/validate-context.mjs +17 -2
  190. package/lib/mjs/parse/validation/validate-context.mjs.map +7 -1
  191. package/lib/mjs/parse/validation/validators/arguments.mjs +67 -2
  192. package/lib/mjs/parse/validation/validators/arguments.mjs.map +7 -1
  193. package/lib/mjs/parse/validation/validators/conflict.mjs +38 -2
  194. package/lib/mjs/parse/validation/validators/conflict.mjs.map +7 -1
  195. package/lib/mjs/parse/validation/validators/exclusive.mjs +39 -2
  196. package/lib/mjs/parse/validation/validators/exclusive.mjs.map +7 -1
  197. package/lib/mjs/parse/validation/validators/explicitly-passed.mjs +18 -2
  198. package/lib/mjs/parse/validation/validators/explicitly-passed.mjs.map +7 -1
  199. package/lib/mjs/parse/validation/validators/options.mjs +67 -2
  200. package/lib/mjs/parse/validation/validators/options.mjs.map +7 -1
  201. package/lib/mjs/parse/validation/validators/requires.mjs +43 -2
  202. package/lib/mjs/parse/validation/validators/requires.mjs.map +7 -1
  203. package/lib/mjs/utilities/parse-argv.mjs +61 -0
  204. package/lib/mjs/utilities/parse-argv.mjs.map +7 -0
  205. package/lib/mjs/utilities/schema-utilities.mjs +57 -0
  206. package/lib/mjs/utilities/schema-utilities.mjs.map +7 -0
  207. package/lib/mjs/utilities/utilities.mjs +122 -0
  208. package/lib/mjs/utilities/utilities.mjs.map +7 -0
  209. package/lib/{typescript → types}/autocomplete-scripts/bash-autocomplete-script.d.ts.map +1 -1
  210. package/lib/types/autocomplete-scripts/powershell-autocomplete-script.d.ts +15 -0
  211. package/lib/{typescript → types}/autocomplete-scripts/powershell-autocomplete-script.d.ts.map +1 -1
  212. package/lib/types/autocomplete-scripts/zsh-autocomplete-script.d.ts +4 -0
  213. package/lib/{typescript → types}/autocomplete-scripts/zsh-autocomplete-script.d.ts.map +1 -1
  214. package/lib/types/cli-error/cli-error.d.ts +152 -0
  215. package/lib/types/cli-error/cli-error.d.ts.map +1 -0
  216. package/lib/types/cli-error/error-cause.d.ts +5 -0
  217. package/lib/types/cli-error/error-cause.d.ts.map +1 -0
  218. package/lib/types/cli-error/error-code/definition-error-code.d.ts +5 -0
  219. package/lib/types/cli-error/error-code/definition-error-code.d.ts.map +1 -0
  220. package/lib/types/cli-error/error-code/internal-error-code.d.ts +5 -0
  221. package/lib/types/cli-error/error-code/internal-error-code.d.ts.map +1 -0
  222. package/lib/types/cli-error/error-code/parse-error-code.d.ts +5 -0
  223. package/lib/types/cli-error/error-code/parse-error-code.d.ts.map +1 -0
  224. package/lib/types/cli-error/error-code/validation-error-code.d.ts +5 -0
  225. package/lib/types/cli-error/error-code/validation-error-code.d.ts.map +1 -0
  226. package/lib/types/cli-error/error-message/definition-error-message.d.ts +3 -0
  227. package/lib/types/cli-error/error-message/definition-error-message.d.ts.map +1 -0
  228. package/lib/types/cli-error/error-message/internal-error-message.d.ts +3 -0
  229. package/lib/types/cli-error/error-message/internal-error-message.d.ts.map +1 -0
  230. package/lib/types/cli-error/error-message/parse-error-message.d.ts +3 -0
  231. package/lib/types/cli-error/error-message/parse-error-message.d.ts.map +1 -0
  232. package/lib/types/cli-error/error-message/validation-error-message.d.ts +3 -0
  233. package/lib/types/cli-error/error-message/validation-error-message.d.ts.map +1 -0
  234. package/lib/types/coerce/coerce-methods.d.ts +122 -0
  235. package/lib/types/coerce/coerce-methods.d.ts.map +1 -0
  236. package/lib/{typescript → types}/coerce/string-to-array.d.ts +2 -2
  237. package/lib/types/coerce/string-to-array.d.ts.map +1 -0
  238. package/lib/types/coerce/string-to-primitive.d.ts +21 -0
  239. package/lib/types/coerce/string-to-primitive.d.ts.map +1 -0
  240. package/lib/{typescript → types}/coerce/string-to-set.d.ts +2 -2
  241. package/lib/{typescript → types}/coerce/string-to-set.d.ts.map +1 -1
  242. package/lib/{typescript → types}/definitions/define-cli.d.ts +2 -6
  243. package/lib/types/definitions/define-cli.d.ts.map +1 -0
  244. package/lib/{typescript → types}/definitions/define-subcommand.d.ts +2 -4
  245. package/lib/types/definitions/define-subcommand.d.ts.map +1 -0
  246. package/lib/{typescript → types}/definitions/validate-cli-definition.d.ts +1 -1
  247. package/lib/types/definitions/validate-cli-definition.d.ts.map +1 -0
  248. package/lib/{typescript → types}/help-message/terminal-markdown.d.ts.map +1 -1
  249. package/lib/{typescript → types}/index.d.ts +7 -0
  250. package/lib/{typescript → types}/index.d.ts.map +1 -1
  251. package/lib/{typescript → types}/metadata/arguments-metadata.d.ts.map +1 -1
  252. package/lib/{typescript → types}/metadata/options-metadata.d.ts.map +1 -1
  253. package/lib/{typescript → types}/parse/context/cli-context-builder.d.ts +1 -1
  254. package/lib/{typescript → types}/parse/context/cli-context-builder.d.ts.map +1 -1
  255. package/lib/{typescript → types}/parse/context/object-context-builder.d.ts +1 -1
  256. package/lib/types/parse/context/object-context-builder.d.ts.map +1 -0
  257. package/lib/{typescript → types}/parse/parser-utilities.d.ts +9 -0
  258. package/lib/{typescript → types}/parse/parser-utilities.d.ts.map +1 -1
  259. package/lib/{typescript → types}/parse/safe-parse.d.ts.map +1 -1
  260. package/lib/{typescript → types}/parse/validation/validate-context.d.ts +1 -1
  261. package/lib/{typescript → types}/parse/validation/validate-context.d.ts.map +1 -1
  262. package/lib/{typescript → types}/parse/validation/validators/arguments.d.ts +1 -1
  263. package/lib/{typescript → types}/parse/validation/validators/arguments.d.ts.map +1 -1
  264. package/lib/{typescript → types}/parse/validation/validators/conflict.d.ts +3 -3
  265. package/lib/{typescript → types}/parse/validation/validators/conflict.d.ts.map +1 -1
  266. package/lib/{typescript → types}/parse/validation/validators/exclusive.d.ts +3 -3
  267. package/lib/{typescript → types}/parse/validation/validators/exclusive.d.ts.map +1 -1
  268. package/lib/{typescript → types}/parse/validation/validators/options.d.ts +1 -1
  269. package/lib/{typescript → types}/parse/validation/validators/options.d.ts.map +1 -1
  270. package/lib/{typescript → types}/parse/validation/validators/requires.d.ts +3 -3
  271. package/lib/{typescript → types}/parse/validation/validators/requires.d.ts.map +1 -1
  272. package/lib/{typescript → types}/types/definitions-types.d.ts +35 -4
  273. package/lib/types/types/definitions-types.d.ts.map +1 -0
  274. package/lib/types/types/error-types.d.ts +275 -0
  275. package/lib/types/types/error-types.d.ts.map +1 -0
  276. package/lib/{typescript → types}/types/io-types.d.ts +1 -1
  277. package/lib/types/types/types.d.ts +146 -0
  278. package/lib/types/types/types.d.ts.map +1 -0
  279. package/lib/{typescript → types}/types/utilities-types.d.ts +0 -2
  280. package/lib/{typescript → types}/types/utilities-types.d.ts.map +1 -1
  281. package/lib/types/utilities/parse-argv.d.ts +3 -0
  282. package/lib/types/utilities/parse-argv.d.ts.map +1 -0
  283. package/lib/types/utilities/schema-utilities.d.ts +15 -0
  284. package/lib/types/utilities/schema-utilities.d.ts.map +1 -0
  285. package/lib/{typescript → types/utilities}/utilities.d.ts +12 -15
  286. package/lib/types/utilities/utilities.d.ts.map +1 -0
  287. package/package.json +7 -7
  288. package/src/autocomplete-scripts/bash-autocomplete-script.ts +14 -12
  289. package/src/autocomplete-scripts/powershell-autocomplete-script.ts +67 -33
  290. package/src/autocomplete-scripts/zsh-autocomplete-script.ts +39 -31
  291. package/src/cli-error/cli-error.ts +44 -0
  292. package/src/cli-error/error-cause.ts +10 -0
  293. package/src/cli-error/error-code/definition-error-code.ts +21 -0
  294. package/src/cli-error/error-code/internal-error-code.ts +8 -0
  295. package/src/cli-error/error-code/parse-error-code.ts +16 -0
  296. package/src/cli-error/error-code/validation-error-code.ts +15 -0
  297. package/src/cli-error/error-message/definition-error-message.ts +144 -0
  298. package/src/cli-error/error-message/internal-error-message.ts +19 -0
  299. package/src/cli-error/error-message/parse-error-message.ts +76 -0
  300. package/src/cli-error/error-message/validation-error-message.ts +104 -0
  301. package/src/coerce/coerce-methods.ts +194 -39
  302. package/src/coerce/string-to-array.ts +2 -2
  303. package/src/coerce/string-to-primitive.ts +72 -13
  304. package/src/coerce/string-to-set.ts +2 -2
  305. package/src/definitions/define-arguments.ts +1 -1
  306. package/src/definitions/define-cli.ts +65 -30
  307. package/src/definitions/define-options.ts +1 -1
  308. package/src/definitions/define-subcommand.ts +47 -17
  309. package/src/definitions/validate-cli-definition.ts +287 -172
  310. package/src/help-message/format-arguments.ts +1 -1
  311. package/src/help-message/format-options.ts +1 -1
  312. package/src/help-message/format-subcommands.ts +1 -1
  313. package/src/help-message/generate-for-cli.ts +1 -1
  314. package/src/help-message/terminal-markdown.ts +3 -4
  315. package/src/index.ts +16 -0
  316. package/src/markdown/generate-markdown.ts +5 -4
  317. package/src/metadata/arguments-metadata.ts +2 -1
  318. package/src/metadata/options-metadata.ts +2 -1
  319. package/src/parse/context/cli-context-builder.ts +166 -39
  320. package/src/parse/context/object-context-builder.ts +37 -11
  321. package/src/parse/parser-utilities.ts +24 -10
  322. package/src/parse/safe-parse.ts +3 -6
  323. package/src/parse/validation/validate-context.ts +1 -1
  324. package/src/parse/validation/validators/arguments.ts +42 -14
  325. package/src/parse/validation/validators/conflict.ts +11 -20
  326. package/src/parse/validation/validators/exclusive.ts +16 -25
  327. package/src/parse/validation/validators/options.ts +40 -12
  328. package/src/parse/validation/validators/requires.ts +11 -20
  329. package/src/types/definitions-types.ts +36 -8
  330. package/src/types/error-types.ts +326 -0
  331. package/src/types/io-types.ts +1 -1
  332. package/src/types/types.ts +100 -7
  333. package/src/types/utilities-types.ts +0 -10
  334. package/src/utilities/parse-argv.ts +79 -0
  335. package/src/utilities/schema-utilities.ts +72 -0
  336. package/src/{utilities.ts → utilities/utilities.ts} +25 -138
  337. package/lib/cjs/autocomplete-scripts/bash-autocomplete-script.cjs +0 -2
  338. package/lib/cjs/autocomplete-scripts/bash-autocomplete-script.cjs.map +0 -1
  339. package/lib/cjs/autocomplete-scripts/powershell-autocomplete-script.cjs +0 -2
  340. package/lib/cjs/autocomplete-scripts/powershell-autocomplete-script.cjs.map +0 -1
  341. package/lib/cjs/autocomplete-scripts/zsh-autocomplete-script.cjs +0 -42
  342. package/lib/cjs/autocomplete-scripts/zsh-autocomplete-script.cjs.map +0 -1
  343. package/lib/cjs/coerce/coerce-methods.cjs +0 -2
  344. package/lib/cjs/coerce/coerce-methods.cjs.map +0 -1
  345. package/lib/cjs/coerce/string-to-array.cjs +0 -2
  346. package/lib/cjs/coerce/string-to-array.cjs.map +0 -1
  347. package/lib/cjs/coerce/string-to-primitive.cjs +0 -2
  348. package/lib/cjs/coerce/string-to-primitive.cjs.map +0 -1
  349. package/lib/cjs/coerce/string-to-set.cjs +0 -2
  350. package/lib/cjs/coerce/string-to-set.cjs.map +0 -1
  351. package/lib/cjs/definitions/define-arguments.cjs +0 -2
  352. package/lib/cjs/definitions/define-arguments.cjs.map +0 -1
  353. package/lib/cjs/definitions/define-cli.cjs +0 -2
  354. package/lib/cjs/definitions/define-cli.cjs.map +0 -1
  355. package/lib/cjs/definitions/define-options.cjs +0 -2
  356. package/lib/cjs/definitions/define-options.cjs.map +0 -1
  357. package/lib/cjs/definitions/define-subcommand.cjs +0 -2
  358. package/lib/cjs/definitions/define-subcommand.cjs.map +0 -1
  359. package/lib/cjs/definitions/validate-cli-definition.cjs +0 -2
  360. package/lib/cjs/definitions/validate-cli-definition.cjs.map +0 -1
  361. package/lib/cjs/help-message/format-arguments.cjs +0 -2
  362. package/lib/cjs/help-message/format-arguments.cjs.map +0 -1
  363. package/lib/cjs/help-message/format-options.cjs +0 -2
  364. package/lib/cjs/help-message/format-options.cjs.map +0 -1
  365. package/lib/cjs/help-message/format-subcommands.cjs +0 -2
  366. package/lib/cjs/help-message/format-subcommands.cjs.map +0 -1
  367. package/lib/cjs/help-message/generate-for-cli.cjs +0 -2
  368. package/lib/cjs/help-message/generate-for-cli.cjs.map +0 -1
  369. package/lib/cjs/help-message/generate-for-subcommand.cjs +0 -2
  370. package/lib/cjs/help-message/generate-for-subcommand.cjs.map +0 -1
  371. package/lib/cjs/help-message/print-help.cjs +0 -2
  372. package/lib/cjs/help-message/print-help.cjs.map +0 -1
  373. package/lib/cjs/help-message/set-defaults.cjs +0 -2
  374. package/lib/cjs/help-message/set-defaults.cjs.map +0 -1
  375. package/lib/cjs/help-message/styles.cjs +0 -2
  376. package/lib/cjs/help-message/styles.cjs.map +0 -1
  377. package/lib/cjs/help-message/terminal-markdown.cjs +0 -2
  378. package/lib/cjs/help-message/terminal-markdown.cjs.map +0 -1
  379. package/lib/cjs/index.cjs +0 -2
  380. package/lib/cjs/index.cjs.map +0 -1
  381. package/lib/cjs/markdown/generate-markdown.cjs +0 -2
  382. package/lib/cjs/markdown/generate-markdown.cjs.map +0 -1
  383. package/lib/cjs/metadata/arguments-metadata.cjs +0 -2
  384. package/lib/cjs/metadata/arguments-metadata.cjs.map +0 -1
  385. package/lib/cjs/metadata/cli-metadata.cjs +0 -2
  386. package/lib/cjs/metadata/cli-metadata.cjs.map +0 -1
  387. package/lib/cjs/metadata/options-metadata.cjs +0 -2
  388. package/lib/cjs/metadata/options-metadata.cjs.map +0 -1
  389. package/lib/cjs/metadata/subcommands-metadata.cjs +0 -2
  390. package/lib/cjs/metadata/subcommands-metadata.cjs.map +0 -1
  391. package/lib/cjs/parse/context/cli-context-builder.cjs +0 -2
  392. package/lib/cjs/parse/context/cli-context-builder.cjs.map +0 -1
  393. package/lib/cjs/parse/context/object-context-builder.cjs +0 -2
  394. package/lib/cjs/parse/context/object-context-builder.cjs.map +0 -1
  395. package/lib/cjs/parse/parser-utilities.cjs +0 -2
  396. package/lib/cjs/parse/parser-utilities.cjs.map +0 -1
  397. package/lib/cjs/parse/safe-parse.cjs +0 -2
  398. package/lib/cjs/parse/safe-parse.cjs.map +0 -1
  399. package/lib/cjs/parse/validation/validate-context.cjs +0 -2
  400. package/lib/cjs/parse/validation/validate-context.cjs.map +0 -1
  401. package/lib/cjs/parse/validation/validators/arguments.cjs +0 -2
  402. package/lib/cjs/parse/validation/validators/arguments.cjs.map +0 -1
  403. package/lib/cjs/parse/validation/validators/conflict.cjs +0 -2
  404. package/lib/cjs/parse/validation/validators/conflict.cjs.map +0 -1
  405. package/lib/cjs/parse/validation/validators/exclusive.cjs +0 -2
  406. package/lib/cjs/parse/validation/validators/exclusive.cjs.map +0 -1
  407. package/lib/cjs/parse/validation/validators/explicitly-passed.cjs +0 -2
  408. package/lib/cjs/parse/validation/validators/explicitly-passed.cjs.map +0 -1
  409. package/lib/cjs/parse/validation/validators/options.cjs +0 -2
  410. package/lib/cjs/parse/validation/validators/options.cjs.map +0 -1
  411. package/lib/cjs/parse/validation/validators/requires.cjs +0 -2
  412. package/lib/cjs/parse/validation/validators/requires.cjs.map +0 -1
  413. package/lib/cjs/utilities.cjs +0 -2
  414. package/lib/cjs/utilities.cjs.map +0 -1
  415. package/lib/esm/utilities.js +0 -2
  416. package/lib/esm/utilities.js.map +0 -1
  417. package/lib/mjs/utilities.mjs +0 -2
  418. package/lib/mjs/utilities.mjs.map +0 -1
  419. package/lib/typescript/autocomplete-scripts/powershell-autocomplete-script.d.ts +0 -14
  420. package/lib/typescript/autocomplete-scripts/zsh-autocomplete-script.d.ts +0 -11
  421. package/lib/typescript/coerce/coerce-methods.d.ts +0 -20
  422. package/lib/typescript/coerce/coerce-methods.d.ts.map +0 -1
  423. package/lib/typescript/coerce/string-to-array.d.ts.map +0 -1
  424. package/lib/typescript/coerce/string-to-primitive.d.ts +0 -5
  425. package/lib/typescript/coerce/string-to-primitive.d.ts.map +0 -1
  426. package/lib/typescript/definitions/define-cli.d.ts.map +0 -1
  427. package/lib/typescript/definitions/define-subcommand.d.ts.map +0 -1
  428. package/lib/typescript/definitions/validate-cli-definition.d.ts.map +0 -1
  429. package/lib/typescript/parse/context/object-context-builder.d.ts.map +0 -1
  430. package/lib/typescript/types/definitions-types.d.ts.map +0 -1
  431. package/lib/typescript/types/types.d.ts +0 -61
  432. package/lib/typescript/types/types.d.ts.map +0 -1
  433. package/lib/typescript/utilities.d.ts.map +0 -1
  434. /package/lib/{typescript → types}/autocomplete-scripts/bash-autocomplete-script.d.ts +0 -0
  435. /package/lib/{typescript → types}/definitions/define-arguments.d.ts +0 -0
  436. /package/lib/{typescript → types}/definitions/define-arguments.d.ts.map +0 -0
  437. /package/lib/{typescript → types}/definitions/define-options.d.ts +0 -0
  438. /package/lib/{typescript → types}/definitions/define-options.d.ts.map +0 -0
  439. /package/lib/{typescript → types}/help-message/format-arguments.d.ts +0 -0
  440. /package/lib/{typescript → types}/help-message/format-arguments.d.ts.map +0 -0
  441. /package/lib/{typescript → types}/help-message/format-options.d.ts +0 -0
  442. /package/lib/{typescript → types}/help-message/format-options.d.ts.map +0 -0
  443. /package/lib/{typescript → types}/help-message/format-subcommands.d.ts +0 -0
  444. /package/lib/{typescript → types}/help-message/format-subcommands.d.ts.map +0 -0
  445. /package/lib/{typescript → types}/help-message/generate-for-cli.d.ts +0 -0
  446. /package/lib/{typescript → types}/help-message/generate-for-cli.d.ts.map +0 -0
  447. /package/lib/{typescript → types}/help-message/generate-for-subcommand.d.ts +0 -0
  448. /package/lib/{typescript → types}/help-message/generate-for-subcommand.d.ts.map +0 -0
  449. /package/lib/{typescript → types}/help-message/print-help.d.ts +0 -0
  450. /package/lib/{typescript → types}/help-message/print-help.d.ts.map +0 -0
  451. /package/lib/{typescript → types}/help-message/set-defaults.d.ts +0 -0
  452. /package/lib/{typescript → types}/help-message/set-defaults.d.ts.map +0 -0
  453. /package/lib/{typescript → types}/help-message/styles.d.ts +0 -0
  454. /package/lib/{typescript → types}/help-message/styles.d.ts.map +0 -0
  455. /package/lib/{typescript → types}/help-message/terminal-markdown.d.ts +0 -0
  456. /package/lib/{typescript → types}/markdown/generate-markdown.d.ts +0 -0
  457. /package/lib/{typescript → types}/markdown/generate-markdown.d.ts.map +0 -0
  458. /package/lib/{typescript → types}/metadata/arguments-metadata.d.ts +0 -0
  459. /package/lib/{typescript → types}/metadata/cli-metadata.d.ts +0 -0
  460. /package/lib/{typescript → types}/metadata/cli-metadata.d.ts.map +0 -0
  461. /package/lib/{typescript → types}/metadata/options-metadata.d.ts +0 -0
  462. /package/lib/{typescript → types}/metadata/subcommands-metadata.d.ts +0 -0
  463. /package/lib/{typescript → types}/metadata/subcommands-metadata.d.ts.map +0 -0
  464. /package/lib/{typescript → types}/parse/safe-parse.d.ts +0 -0
  465. /package/lib/{typescript → types}/parse/validation/validators/explicitly-passed.d.ts +0 -0
  466. /package/lib/{typescript → types}/parse/validation/validators/explicitly-passed.d.ts.map +0 -0
  467. /package/lib/{typescript → types}/types/context-types.d.ts +0 -0
  468. /package/lib/{typescript → types}/types/context-types.d.ts.map +0 -0
  469. /package/lib/{typescript → types}/types/help-message-types.d.ts +0 -0
  470. /package/lib/{typescript → types}/types/help-message-types.d.ts.map +0 -0
  471. /package/lib/{typescript → types}/types/io-types.d.ts.map +0 -0
  472. /package/lib/{typescript → types}/types/metadata-types.d.ts +0 -0
  473. /package/lib/{typescript → types}/types/metadata-types.d.ts.map +0 -0
  474. /package/lib/{typescript → types}/types/schema-types.d.ts +0 -0
  475. /package/lib/{typescript → types}/types/schema-types.d.ts.map +0 -0
@@ -1,4 +1,8 @@
1
- /** @throws {TypeError} */
1
+ import { CliError } from "../cli-error/cli-error.ts";
2
+ import { ErrorCause } from "../cli-error/error-cause.ts";
3
+ import { ValidationErrorCode } from "../cli-error/error-code/validation-error-code.ts";
4
+
5
+ /** @throws {CliError} */
2
6
  export function stringToBoolean(string: string): boolean {
3
7
  if (string.toLowerCase() === "true") {
4
8
  return true;
@@ -8,24 +12,79 @@ export function stringToBoolean(string: string): boolean {
8
12
  return false;
9
13
  }
10
14
 
11
- throw new TypeError(`Invalid boolean value: ${string}`);
15
+ throw new CliError({
16
+ cause: ErrorCause.Validation,
17
+ code: ValidationErrorCode.CoercionFailed,
18
+ context: { coerceToType: "boolean", providedValue: string },
19
+ });
12
20
  }
13
21
 
14
- /** @throws {TypeError} */
15
- export function stringToNumber(string: string): number {
16
- const trimmed = string.trim();
22
+ /**
23
+ * Converts a string to a finite number.
24
+ *
25
+ * @throws {CliError} If input is empty, invalid, or not a finite number.
26
+ */
27
+ export function stringToNumber(value: string): number {
28
+ const input = value.trim();
29
+ if (!/^[-+]?[0-9]+(?:\.[0-9]+)?$/.test(input)) {
30
+ throw new CliError({
31
+ cause: ErrorCause.Validation,
32
+ code: ValidationErrorCode.CoercionFailed,
33
+ context: { coerceToType: "number", providedValue: value },
34
+ });
35
+ }
17
36
 
18
- // Reject empty, whitespace-only, or signs without digits
19
- if (trimmed === "" || trimmed === "+" || trimmed === "-") {
20
- throw new TypeError(`Invalid number: "${string}"`);
37
+ const number = Number(input);
38
+ if (!Number.isFinite(number)) {
39
+ throw new CliError({
40
+ cause: ErrorCause.Validation,
41
+ code: ValidationErrorCode.CoercionFailed,
42
+ context: { coerceToType: "number", providedValue: value },
43
+ });
21
44
  }
22
45
 
23
- const result = Number(trimmed);
46
+ return number;
47
+ }
24
48
 
25
- // Reject NaN, Infinity, and -Infinity
26
- if (!Number.isFinite(result)) {
27
- throw new TypeError(`Invalid number: "${string}"`);
49
+ /**
50
+ * Converts a string to a bigint.
51
+ *
52
+ * @throws {CliError} If input is empty or not a valid bigint.
53
+ */
54
+ export function stringToBigint(value: string): bigint {
55
+ const input = value.trim();
56
+ if (!/^[+-]?[0-9]+$/.test(input)) {
57
+ throw new CliError({
58
+ cause: ErrorCause.Validation,
59
+ code: ValidationErrorCode.CoercionFailed,
60
+ context: { coerceToType: "bigint", providedValue: value },
61
+ });
28
62
  }
29
63
 
30
- return result;
64
+ try {
65
+ return BigInt(input);
66
+ } catch {
67
+ throw new CliError({
68
+ cause: ErrorCause.Validation,
69
+ code: ValidationErrorCode.CoercionFailed,
70
+ context: { coerceToType: "bigint", providedValue: value },
71
+ });
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Converts a string to a valid Date object.
77
+ *
78
+ * @throws {CliError} If input is not a valid date string.
79
+ */
80
+ export function stringToDate(value: string): Date {
81
+ const date = new Date(value);
82
+ if (Number.isNaN(date.getTime())) {
83
+ throw new CliError({
84
+ cause: ErrorCause.Validation,
85
+ code: ValidationErrorCode.CoercionFailed,
86
+ context: { coerceToType: "date", providedValue: value },
87
+ });
88
+ }
89
+ return date;
31
90
  }
@@ -4,12 +4,12 @@ export function stringToStringSet(stringValue: string, separator: string = ","):
4
4
  return new Set(stringToStringArray(stringValue, separator));
5
5
  }
6
6
 
7
- /** @throws {TypeError} - Because of `stringToNumber` */
7
+ /** @throws {CliError} - Because of `stringToNumber` */
8
8
  export function stringToNumberSet(stringValue: string, separator: string = ","): Set<number> {
9
9
  return new Set(stringToNumberArray(stringValue, separator));
10
10
  }
11
11
 
12
- /** @throws {TypeError} - Because of `stringToBoolean` */
12
+ /** @throws {CliError} - Because of `stringToBoolean` */
13
13
  export function stringToBooleanSet(stringValue: string, separator: string = ","): Set<boolean> {
14
14
  return new Set(stringToBooleanArray(stringValue, separator));
15
15
  }
@@ -1,4 +1,4 @@
1
- import { prepareDefinitionTypes } from "../utilities.ts";
1
+ import { prepareDefinitionTypes } from "../utilities/schema-utilities.ts";
2
2
 
3
3
  import type { Argument } from "../types/definitions-types.ts";
4
4
 
@@ -1,9 +1,12 @@
1
+ import { CliError } from "../cli-error/cli-error.ts";
2
+ import { ErrorCause } from "../cli-error/error-cause.ts";
3
+ import { DefinitionErrorCode } from "../cli-error/error-code/definition-error-code.ts";
1
4
  import { generateCliHelpMessage } from "../help-message/generate-for-cli.ts";
2
5
  import { generateSubcommandHelpMessage } from "../help-message/generate-for-subcommand.ts";
3
6
  import { buildObjectContext } from "../parse/context/object-context-builder.ts";
4
7
  import { safeParse, safeParseAsync } from "../parse/safe-parse.ts";
5
8
  import { validate } from "../parse/validation/validate-context.ts";
6
- import { prepareDefinitionTypes } from "../utilities.ts";
9
+ import { prepareDefinitionTypes } from "../utilities/schema-utilities.ts";
7
10
 
8
11
  import type { Argument, Cli, Option, Subcommand } from "../types/definitions-types.ts";
9
12
  import type { PrintHelpOptions } from "../types/help-message-types.ts";
@@ -40,66 +43,98 @@ type CliInput<T extends Cli> = {
40
43
  };
41
44
 
42
45
  export function defineCLI<T extends Cli>(input: CliInput<T> & Cli) {
43
- const cliSchema = input as Prettify<T & AttachedMethods<T> & ValidateMethods<T>>;
46
+ const cliDefinition = input as T;
44
47
 
45
- prepareDefinitionTypes(cliSchema.options);
46
- prepareDefinitionTypes(cliSchema.arguments);
48
+ prepareDefinitionTypes(cliDefinition.options);
49
+ prepareDefinitionTypes(cliDefinition.arguments);
47
50
 
48
- if (cliSchema.subcommands) {
49
- for (const subcommand of Object.values(cliSchema.subcommands)) {
51
+ if (cliDefinition.subcommands) {
52
+ for (const subcommand of Object.values(cliDefinition.subcommands)) {
50
53
  prepareDefinitionTypes(subcommand.options);
51
54
  prepareDefinitionTypes(subcommand.arguments);
52
55
  }
53
56
  }
54
57
 
55
58
  const onExecute = (handler: (Cli["_onExecute"] & {})[number]) => {
56
- cliSchema._onExecute ??= [];
57
- cliSchema._onExecute.push(handler);
59
+ cliDefinition._onExecute ??= [];
60
+ cliDefinition._onExecute.push(handler);
58
61
 
59
62
  return () => {
60
- const handlerIndex = cliSchema._onExecute?.indexOf(handler);
63
+ const handlerIndex = cliDefinition._onExecute?.indexOf(handler);
61
64
  if (!handlerIndex || handlerIndex < 0) return;
62
- cliSchema._onExecute?.splice(handlerIndex, 1);
65
+ cliDefinition._onExecute?.splice(handlerIndex, 1);
63
66
  };
64
67
  };
65
68
 
66
69
  const execute: AttachedMethodsWide["execute"] = inputValues => {
67
70
  inputValues ??= {};
68
- if (!cliSchema._onExecute) throw new Error("Action is not defined");
69
- const context = buildObjectContext(inputValues, cliSchema);
70
- const validateResult = validate(context, cliSchema);
71
71
 
72
- if (cliSchema._onExecute) {
73
- for (const handler of cliSchema._onExecute) {
74
- handler(validateResult);
75
- }
72
+ const handlers = cliDefinition._onExecute;
73
+ if (!handlers) {
74
+ throw new CliError({
75
+ cause: ErrorCause.Definition,
76
+ code: DefinitionErrorCode.MissingOnExecute,
77
+ context: { commandKind: "command", commandName: cliDefinition.cliName },
78
+ });
79
+ }
80
+
81
+ const context = buildObjectContext(inputValues, cliDefinition);
82
+ const validateResult = validate(context, cliDefinition);
83
+
84
+ for (const handler of handlers) {
85
+ void handler(validateResult);
86
+ }
87
+ };
88
+
89
+ const executeAsync: AttachedMethodsWide["executeAsync"] = async inputValues => {
90
+ inputValues ??= {};
91
+
92
+ const handlers = cliDefinition._onExecute;
93
+
94
+ if (!handlers) {
95
+ throw new CliError({
96
+ cause: ErrorCause.Definition,
97
+ code: DefinitionErrorCode.MissingOnExecute,
98
+ context: { commandKind: "command", commandName: cliDefinition.cliName },
99
+ });
76
100
  }
101
+
102
+ const context = buildObjectContext(inputValues, cliDefinition);
103
+ const validateResult = validate(context, cliDefinition);
104
+
105
+ await Promise.all(handlers.map(async handler => await handler(validateResult)));
77
106
  };
78
107
 
79
108
  // Add print methods for CLI schema and its subcommands
80
109
  const generateHelpMethods: Pick<AttachedMethodsWide, "generateCliHelpMessage" | "generateSubcommandHelpMessage"> = {
81
110
  generateCliHelpMessage(options?: PrintHelpOptions) {
82
- return generateCliHelpMessage(cliSchema, options);
111
+ return generateCliHelpMessage(cliDefinition, options);
83
112
  },
84
113
  generateSubcommandHelpMessage(subcommandName: string, options?: PrintHelpOptions) {
85
- const foundSubcommand = cliSchema.subcommands?.find(s => s.name === subcommandName);
86
- if (!foundSubcommand) throw new Error(`Subcommand ${subcommandName} not found`);
87
- return generateSubcommandHelpMessage(foundSubcommand, options, cliSchema.cliName);
114
+ const foundSubcommand = cliDefinition.subcommands?.find(s => s.name === subcommandName);
115
+ if (!foundSubcommand) {
116
+ throw new CliError({
117
+ cause: ErrorCause.Definition,
118
+ code: DefinitionErrorCode.SubcommandHelpNotFound,
119
+ context: { cliName: cliDefinition.cliName, subcommandName },
120
+ });
121
+ }
122
+ return generateSubcommandHelpMessage(foundSubcommand, options, cliDefinition.cliName);
88
123
  },
89
124
  };
90
125
 
91
- Object.assign(cliSchema, generateHelpMethods);
126
+ Object.assign(cliDefinition, generateHelpMethods);
92
127
 
93
- if (cliSchema.subcommands) {
94
- for (const subcommandSchema of cliSchema.subcommands) {
128
+ if (cliDefinition.subcommands) {
129
+ for (const subcommandSchema of cliDefinition.subcommands) {
95
130
  Object.assign(subcommandSchema, generateHelpMethods);
96
131
  }
97
132
  }
98
133
 
99
- return Object.assign(cliSchema, {
100
- execute,
101
- onExecute,
102
- run: (stringOrArgv: string | string[]) => safeParse(stringOrArgv, cliSchema),
103
- runAsync: (stringOrArgv: string | string[]) => safeParseAsync(stringOrArgv, cliSchema),
104
- });
134
+ const run = (stringOrArgv: string | string[]) => safeParse(stringOrArgv, cliDefinition);
135
+ const runAsync = (stringOrArgv: string | string[]) => safeParseAsync(stringOrArgv, cliDefinition);
136
+
137
+ return Object.assign(cliDefinition, { onExecute, execute, executeAsync, run, runAsync }) as Prettify<
138
+ T & AttachedMethods<T> & ValidateMethods<T>
139
+ >;
105
140
  }
@@ -1,4 +1,4 @@
1
- import { prepareDefinitionTypes } from "../utilities.ts";
1
+ import { prepareDefinitionTypes } from "../utilities/schema-utilities.ts";
2
2
 
3
3
  import type { Option } from "../types/definitions-types.ts";
4
4
 
@@ -1,6 +1,9 @@
1
+ import { CliError } from "../cli-error/cli-error.ts";
2
+ import { ErrorCause } from "../cli-error/error-cause.ts";
3
+ import { DefinitionErrorCode } from "../cli-error/error-code/definition-error-code.ts";
1
4
  import { buildObjectContext } from "../parse/context/object-context-builder.ts";
2
5
  import { validate } from "../parse/validation/validate-context.ts";
3
- import { prepareDefinitionTypes } from "../utilities.ts";
6
+ import { prepareDefinitionTypes } from "../utilities/schema-utilities.ts";
4
7
 
5
8
  import type { Argument, Option, Subcommand } from "../types/definitions-types.ts";
6
9
  import type { AttachedMethods, AttachedMethodsWide } from "../types/types.ts";
@@ -24,34 +27,61 @@ type SubcommandInput<T extends Subcommand> = {
24
27
  };
25
28
 
26
29
  export function defineSubcommand<T extends Subcommand>(input: SubcommandInput<T> & Subcommand) {
27
- const subcommandSchema = input as Prettify<T & AttachedMethods<T>>;
30
+ const subcommandDefinition = input as T;
28
31
 
29
- prepareDefinitionTypes(subcommandSchema.options);
30
- prepareDefinitionTypes(subcommandSchema.arguments);
32
+ prepareDefinitionTypes(subcommandDefinition.options);
33
+ prepareDefinitionTypes(subcommandDefinition.arguments);
31
34
 
32
35
  const onExecute = (handler: (Subcommand["_onExecute"] & {})[number]) => {
33
- subcommandSchema._onExecute ??= [];
34
- subcommandSchema._onExecute.push(handler);
36
+ subcommandDefinition._onExecute ??= [];
37
+ subcommandDefinition._onExecute.push(handler);
35
38
 
36
39
  return () => {
37
- const handlerIndex = subcommandSchema._onExecute?.indexOf(handler);
40
+ const handlerIndex = subcommandDefinition._onExecute?.indexOf(handler);
38
41
  if (!handlerIndex || handlerIndex < 0) return;
39
- subcommandSchema._onExecute?.splice(handlerIndex, 1);
42
+ subcommandDefinition._onExecute?.splice(handlerIndex, 1);
40
43
  };
41
44
  };
42
45
 
43
46
  const execute: AttachedMethodsWide["execute"] = inputValues => {
44
47
  inputValues ??= {};
45
- if (!subcommandSchema._onExecute) throw new Error("Action is not defined");
46
- const context = buildObjectContext(inputValues, subcommandSchema);
47
- const validateResult = validate(context, subcommandSchema);
48
-
49
- if (subcommandSchema._onExecute) {
50
- for (const handler of subcommandSchema._onExecute) {
51
- handler(validateResult);
52
- }
48
+
49
+ const handlers = subcommandDefinition._onExecute;
50
+
51
+ if (!handlers) {
52
+ throw new CliError({
53
+ cause: ErrorCause.Definition,
54
+ code: DefinitionErrorCode.MissingOnExecute,
55
+ context: { commandKind: "subcommand", commandName: subcommandDefinition.name },
56
+ });
53
57
  }
58
+
59
+ const context = buildObjectContext(inputValues, subcommandDefinition);
60
+ const validateResult = validate(context, subcommandDefinition);
61
+
62
+ for (const handler of handlers) {
63
+ void handler(validateResult);
64
+ }
65
+ };
66
+
67
+ const executeAsync: AttachedMethodsWide["executeAsync"] = async inputValues => {
68
+ inputValues ??= {};
69
+
70
+ const handlers = subcommandDefinition._onExecute;
71
+
72
+ if (!handlers) {
73
+ throw new CliError({
74
+ cause: ErrorCause.Definition,
75
+ code: DefinitionErrorCode.MissingOnExecute,
76
+ context: { commandKind: "subcommand", commandName: subcommandDefinition.name },
77
+ });
78
+ }
79
+
80
+ const context = buildObjectContext(inputValues, subcommandDefinition);
81
+ const validateResult = validate(context, subcommandDefinition);
82
+
83
+ await Promise.all(handlers.map(async handler => await handler(validateResult)));
54
84
  };
55
85
 
56
- return Object.assign(subcommandSchema, { onExecute, execute });
86
+ return Object.assign(subcommandDefinition, { onExecute, execute, executeAsync }) as Prettify<T & AttachedMethods<T>>;
57
87
  }