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.
- package/LICENSE +1 -0
- package/README.md +230 -289
- package/lib/esm/autocomplete-scripts/bash-autocomplete-script.js +74 -2
- package/lib/esm/autocomplete-scripts/bash-autocomplete-script.js.map +7 -1
- package/lib/esm/autocomplete-scripts/powershell-autocomplete-script.js +90 -2
- package/lib/esm/autocomplete-scripts/powershell-autocomplete-script.js.map +7 -1
- package/lib/esm/autocomplete-scripts/zsh-autocomplete-script.js +39 -13
- package/lib/esm/autocomplete-scripts/zsh-autocomplete-script.js.map +7 -1
- package/lib/esm/cli-error/cli-error.js +38 -0
- package/lib/esm/cli-error/cli-error.js.map +7 -0
- package/lib/esm/cli-error/error-cause.js +12 -0
- package/lib/esm/cli-error/error-cause.js.map +7 -0
- package/lib/esm/cli-error/error-code/definition-error-code.js +23 -0
- package/lib/esm/cli-error/error-code/definition-error-code.js.map +7 -0
- package/lib/esm/cli-error/error-code/internal-error-code.js +10 -0
- package/lib/esm/cli-error/error-code/internal-error-code.js.map +7 -0
- package/lib/esm/cli-error/error-code/parse-error-code.js +18 -0
- package/lib/esm/cli-error/error-code/parse-error-code.js.map +7 -0
- package/lib/esm/cli-error/error-code/validation-error-code.js +17 -0
- package/lib/esm/cli-error/error-code/validation-error-code.js.map +7 -0
- package/lib/esm/cli-error/error-message/definition-error-message.js +70 -0
- package/lib/esm/cli-error/error-message/definition-error-message.js.map +7 -0
- package/lib/esm/cli-error/error-message/internal-error-message.js +16 -0
- package/lib/esm/cli-error/error-message/internal-error-message.js.map +7 -0
- package/lib/esm/cli-error/error-message/parse-error-message.js +40 -0
- package/lib/esm/cli-error/error-message/parse-error-message.js.map +7 -0
- package/lib/esm/cli-error/error-message/validation-error-message.js +60 -0
- package/lib/esm/cli-error/error-message/validation-error-message.js.map +7 -0
- package/lib/esm/coerce/coerce-methods.js +197 -2
- package/lib/esm/coerce/coerce-methods.js.map +7 -1
- package/lib/esm/coerce/string-to-array.js +17 -2
- package/lib/esm/coerce/string-to-array.js.map +7 -1
- package/lib/esm/coerce/string-to-primitive.js +73 -2
- package/lib/esm/coerce/string-to-primitive.js.map +7 -1
- package/lib/esm/coerce/string-to-set.js +17 -2
- package/lib/esm/coerce/string-to-set.js.map +7 -1
- package/lib/esm/definitions/define-arguments.js +10 -2
- package/lib/esm/definitions/define-arguments.js.map +7 -1
- package/lib/esm/definitions/define-cli.js +89 -2
- package/lib/esm/definitions/define-cli.js.map +7 -1
- package/lib/esm/definitions/define-options.js +10 -2
- package/lib/esm/definitions/define-options.js.map +7 -1
- package/lib/esm/definitions/define-subcommand.js +56 -2
- package/lib/esm/definitions/define-subcommand.js.map +7 -1
- package/lib/esm/definitions/validate-cli-definition.js +360 -2
- package/lib/esm/definitions/validate-cli-definition.js.map +7 -1
- package/lib/esm/help-message/format-arguments.js +48 -2
- package/lib/esm/help-message/format-arguments.js.map +7 -1
- package/lib/esm/help-message/format-options.js +50 -2
- package/lib/esm/help-message/format-options.js.map +7 -1
- package/lib/esm/help-message/format-subcommands.js +37 -2
- package/lib/esm/help-message/format-subcommands.js.map +7 -1
- package/lib/esm/help-message/generate-for-cli.js +71 -2
- package/lib/esm/help-message/generate-for-cli.js.map +7 -1
- package/lib/esm/help-message/generate-for-subcommand.js +23 -2
- package/lib/esm/help-message/generate-for-subcommand.js.map +7 -1
- package/lib/esm/help-message/print-help.js +14 -2
- package/lib/esm/help-message/print-help.js.map +7 -1
- package/lib/esm/help-message/set-defaults.js +29 -2
- package/lib/esm/help-message/set-defaults.js.map +7 -1
- package/lib/esm/help-message/styles.js +154 -2
- package/lib/esm/help-message/styles.js.map +7 -1
- package/lib/esm/help-message/terminal-markdown.js +121 -2
- package/lib/esm/help-message/terminal-markdown.js.map +7 -1
- package/lib/esm/index.js +52 -2
- package/lib/esm/index.js.map +7 -1
- package/lib/esm/markdown/generate-markdown.js +153 -2
- package/lib/esm/markdown/generate-markdown.js.map +7 -1
- package/lib/esm/metadata/arguments-metadata.js +26 -2
- package/lib/esm/metadata/arguments-metadata.js.map +7 -1
- package/lib/esm/metadata/cli-metadata.js +24 -2
- package/lib/esm/metadata/cli-metadata.js.map +7 -1
- package/lib/esm/metadata/options-metadata.js +35 -2
- package/lib/esm/metadata/options-metadata.js.map +7 -1
- package/lib/esm/metadata/subcommands-metadata.js +32 -2
- package/lib/esm/metadata/subcommands-metadata.js.map +7 -1
- package/lib/esm/parse/context/cli-context-builder.js +283 -2
- package/lib/esm/parse/context/cli-context-builder.js.map +7 -1
- package/lib/esm/parse/context/object-context-builder.js +62 -2
- package/lib/esm/parse/context/object-context-builder.js.map +7 -1
- package/lib/esm/parse/parser-utilities.js +108 -2
- package/lib/esm/parse/parser-utilities.js.map +7 -1
- package/lib/esm/parse/safe-parse.js +57 -2
- package/lib/esm/parse/safe-parse.js.map +7 -1
- package/lib/esm/parse/validation/validate-context.js +17 -2
- package/lib/esm/parse/validation/validate-context.js.map +7 -1
- package/lib/esm/parse/validation/validators/arguments.js +67 -2
- package/lib/esm/parse/validation/validators/arguments.js.map +7 -1
- package/lib/esm/parse/validation/validators/conflict.js +38 -2
- package/lib/esm/parse/validation/validators/conflict.js.map +7 -1
- package/lib/esm/parse/validation/validators/exclusive.js +39 -2
- package/lib/esm/parse/validation/validators/exclusive.js.map +7 -1
- package/lib/esm/parse/validation/validators/explicitly-passed.js +18 -2
- package/lib/esm/parse/validation/validators/explicitly-passed.js.map +7 -1
- package/lib/esm/parse/validation/validators/options.js +67 -2
- package/lib/esm/parse/validation/validators/options.js.map +7 -1
- package/lib/esm/parse/validation/validators/requires.js +43 -2
- package/lib/esm/parse/validation/validators/requires.js.map +7 -1
- package/lib/esm/utilities/parse-argv.js +61 -0
- package/lib/esm/utilities/parse-argv.js.map +7 -0
- package/lib/esm/utilities/schema-utilities.js +57 -0
- package/lib/esm/utilities/schema-utilities.js.map +7 -0
- package/lib/esm/utilities/utilities.js +122 -0
- package/lib/esm/utilities/utilities.js.map +7 -0
- package/lib/iife/index.js +1 -1
- package/lib/iife/index.js.map +1 -1
- package/lib/mjs/autocomplete-scripts/bash-autocomplete-script.mjs +74 -2
- package/lib/mjs/autocomplete-scripts/bash-autocomplete-script.mjs.map +7 -1
- package/lib/mjs/autocomplete-scripts/powershell-autocomplete-script.mjs +90 -2
- package/lib/mjs/autocomplete-scripts/powershell-autocomplete-script.mjs.map +7 -1
- package/lib/mjs/autocomplete-scripts/zsh-autocomplete-script.mjs +39 -13
- package/lib/mjs/autocomplete-scripts/zsh-autocomplete-script.mjs.map +7 -1
- package/lib/mjs/cli-error/cli-error.mjs +38 -0
- package/lib/mjs/cli-error/cli-error.mjs.map +7 -0
- package/lib/mjs/cli-error/error-cause.mjs +12 -0
- package/lib/mjs/cli-error/error-cause.mjs.map +7 -0
- package/lib/mjs/cli-error/error-code/definition-error-code.mjs +23 -0
- package/lib/mjs/cli-error/error-code/definition-error-code.mjs.map +7 -0
- package/lib/mjs/cli-error/error-code/internal-error-code.mjs +10 -0
- package/lib/mjs/cli-error/error-code/internal-error-code.mjs.map +7 -0
- package/lib/mjs/cli-error/error-code/parse-error-code.mjs +18 -0
- package/lib/mjs/cli-error/error-code/parse-error-code.mjs.map +7 -0
- package/lib/mjs/cli-error/error-code/validation-error-code.mjs +17 -0
- package/lib/mjs/cli-error/error-code/validation-error-code.mjs.map +7 -0
- package/lib/mjs/cli-error/error-message/definition-error-message.mjs +70 -0
- package/lib/mjs/cli-error/error-message/definition-error-message.mjs.map +7 -0
- package/lib/mjs/cli-error/error-message/internal-error-message.mjs +16 -0
- package/lib/mjs/cli-error/error-message/internal-error-message.mjs.map +7 -0
- package/lib/mjs/cli-error/error-message/parse-error-message.mjs +40 -0
- package/lib/mjs/cli-error/error-message/parse-error-message.mjs.map +7 -0
- package/lib/mjs/cli-error/error-message/validation-error-message.mjs +60 -0
- package/lib/mjs/cli-error/error-message/validation-error-message.mjs.map +7 -0
- package/lib/mjs/coerce/coerce-methods.mjs +197 -2
- package/lib/mjs/coerce/coerce-methods.mjs.map +7 -1
- package/lib/mjs/coerce/string-to-array.mjs +17 -2
- package/lib/mjs/coerce/string-to-array.mjs.map +7 -1
- package/lib/mjs/coerce/string-to-primitive.mjs +73 -2
- package/lib/mjs/coerce/string-to-primitive.mjs.map +7 -1
- package/lib/mjs/coerce/string-to-set.mjs +17 -2
- package/lib/mjs/coerce/string-to-set.mjs.map +7 -1
- package/lib/mjs/definitions/define-arguments.mjs +10 -2
- package/lib/mjs/definitions/define-arguments.mjs.map +7 -1
- package/lib/mjs/definitions/define-cli.mjs +89 -2
- package/lib/mjs/definitions/define-cli.mjs.map +7 -1
- package/lib/mjs/definitions/define-options.mjs +10 -2
- package/lib/mjs/definitions/define-options.mjs.map +7 -1
- package/lib/mjs/definitions/define-subcommand.mjs +56 -2
- package/lib/mjs/definitions/define-subcommand.mjs.map +7 -1
- package/lib/mjs/definitions/validate-cli-definition.mjs +360 -2
- package/lib/mjs/definitions/validate-cli-definition.mjs.map +7 -1
- package/lib/mjs/help-message/format-arguments.mjs +48 -2
- package/lib/mjs/help-message/format-arguments.mjs.map +7 -1
- package/lib/mjs/help-message/format-options.mjs +50 -2
- package/lib/mjs/help-message/format-options.mjs.map +7 -1
- package/lib/mjs/help-message/format-subcommands.mjs +37 -2
- package/lib/mjs/help-message/format-subcommands.mjs.map +7 -1
- package/lib/mjs/help-message/generate-for-cli.mjs +71 -2
- package/lib/mjs/help-message/generate-for-cli.mjs.map +7 -1
- package/lib/mjs/help-message/generate-for-subcommand.mjs +23 -2
- package/lib/mjs/help-message/generate-for-subcommand.mjs.map +7 -1
- package/lib/mjs/help-message/print-help.mjs +14 -2
- package/lib/mjs/help-message/print-help.mjs.map +7 -1
- package/lib/mjs/help-message/set-defaults.mjs +29 -2
- package/lib/mjs/help-message/set-defaults.mjs.map +7 -1
- package/lib/mjs/help-message/styles.mjs +154 -2
- package/lib/mjs/help-message/styles.mjs.map +7 -1
- package/lib/mjs/help-message/terminal-markdown.mjs +121 -2
- package/lib/mjs/help-message/terminal-markdown.mjs.map +7 -1
- package/lib/mjs/index.mjs +52 -2
- package/lib/mjs/index.mjs.map +7 -1
- package/lib/mjs/markdown/generate-markdown.mjs +153 -2
- package/lib/mjs/markdown/generate-markdown.mjs.map +7 -1
- package/lib/mjs/metadata/arguments-metadata.mjs +26 -2
- package/lib/mjs/metadata/arguments-metadata.mjs.map +7 -1
- package/lib/mjs/metadata/cli-metadata.mjs +24 -2
- package/lib/mjs/metadata/cli-metadata.mjs.map +7 -1
- package/lib/mjs/metadata/options-metadata.mjs +35 -2
- package/lib/mjs/metadata/options-metadata.mjs.map +7 -1
- package/lib/mjs/metadata/subcommands-metadata.mjs +32 -2
- package/lib/mjs/metadata/subcommands-metadata.mjs.map +7 -1
- package/lib/mjs/parse/context/cli-context-builder.mjs +283 -2
- package/lib/mjs/parse/context/cli-context-builder.mjs.map +7 -1
- package/lib/mjs/parse/context/object-context-builder.mjs +62 -2
- package/lib/mjs/parse/context/object-context-builder.mjs.map +7 -1
- package/lib/mjs/parse/parser-utilities.mjs +108 -2
- package/lib/mjs/parse/parser-utilities.mjs.map +7 -1
- package/lib/mjs/parse/safe-parse.mjs +57 -2
- package/lib/mjs/parse/safe-parse.mjs.map +7 -1
- package/lib/mjs/parse/validation/validate-context.mjs +17 -2
- package/lib/mjs/parse/validation/validate-context.mjs.map +7 -1
- package/lib/mjs/parse/validation/validators/arguments.mjs +67 -2
- package/lib/mjs/parse/validation/validators/arguments.mjs.map +7 -1
- package/lib/mjs/parse/validation/validators/conflict.mjs +38 -2
- package/lib/mjs/parse/validation/validators/conflict.mjs.map +7 -1
- package/lib/mjs/parse/validation/validators/exclusive.mjs +39 -2
- package/lib/mjs/parse/validation/validators/exclusive.mjs.map +7 -1
- package/lib/mjs/parse/validation/validators/explicitly-passed.mjs +18 -2
- package/lib/mjs/parse/validation/validators/explicitly-passed.mjs.map +7 -1
- package/lib/mjs/parse/validation/validators/options.mjs +67 -2
- package/lib/mjs/parse/validation/validators/options.mjs.map +7 -1
- package/lib/mjs/parse/validation/validators/requires.mjs +43 -2
- package/lib/mjs/parse/validation/validators/requires.mjs.map +7 -1
- package/lib/mjs/utilities/parse-argv.mjs +61 -0
- package/lib/mjs/utilities/parse-argv.mjs.map +7 -0
- package/lib/mjs/utilities/schema-utilities.mjs +57 -0
- package/lib/mjs/utilities/schema-utilities.mjs.map +7 -0
- package/lib/mjs/utilities/utilities.mjs +122 -0
- package/lib/mjs/utilities/utilities.mjs.map +7 -0
- package/lib/{typescript → types}/autocomplete-scripts/bash-autocomplete-script.d.ts.map +1 -1
- package/lib/types/autocomplete-scripts/powershell-autocomplete-script.d.ts +15 -0
- package/lib/{typescript → types}/autocomplete-scripts/powershell-autocomplete-script.d.ts.map +1 -1
- package/lib/types/autocomplete-scripts/zsh-autocomplete-script.d.ts +4 -0
- package/lib/{typescript → types}/autocomplete-scripts/zsh-autocomplete-script.d.ts.map +1 -1
- package/lib/types/cli-error/cli-error.d.ts +152 -0
- package/lib/types/cli-error/cli-error.d.ts.map +1 -0
- package/lib/types/cli-error/error-cause.d.ts +5 -0
- package/lib/types/cli-error/error-cause.d.ts.map +1 -0
- package/lib/types/cli-error/error-code/definition-error-code.d.ts +5 -0
- package/lib/types/cli-error/error-code/definition-error-code.d.ts.map +1 -0
- package/lib/types/cli-error/error-code/internal-error-code.d.ts +5 -0
- package/lib/types/cli-error/error-code/internal-error-code.d.ts.map +1 -0
- package/lib/types/cli-error/error-code/parse-error-code.d.ts +5 -0
- package/lib/types/cli-error/error-code/parse-error-code.d.ts.map +1 -0
- package/lib/types/cli-error/error-code/validation-error-code.d.ts +5 -0
- package/lib/types/cli-error/error-code/validation-error-code.d.ts.map +1 -0
- package/lib/types/cli-error/error-message/definition-error-message.d.ts +3 -0
- package/lib/types/cli-error/error-message/definition-error-message.d.ts.map +1 -0
- package/lib/types/cli-error/error-message/internal-error-message.d.ts +3 -0
- package/lib/types/cli-error/error-message/internal-error-message.d.ts.map +1 -0
- package/lib/types/cli-error/error-message/parse-error-message.d.ts +3 -0
- package/lib/types/cli-error/error-message/parse-error-message.d.ts.map +1 -0
- package/lib/types/cli-error/error-message/validation-error-message.d.ts +3 -0
- package/lib/types/cli-error/error-message/validation-error-message.d.ts.map +1 -0
- package/lib/types/coerce/coerce-methods.d.ts +122 -0
- package/lib/types/coerce/coerce-methods.d.ts.map +1 -0
- package/lib/{typescript → types}/coerce/string-to-array.d.ts +2 -2
- package/lib/types/coerce/string-to-array.d.ts.map +1 -0
- package/lib/types/coerce/string-to-primitive.d.ts +21 -0
- package/lib/types/coerce/string-to-primitive.d.ts.map +1 -0
- package/lib/{typescript → types}/coerce/string-to-set.d.ts +2 -2
- package/lib/{typescript → types}/coerce/string-to-set.d.ts.map +1 -1
- package/lib/{typescript → types}/definitions/define-cli.d.ts +2 -6
- package/lib/types/definitions/define-cli.d.ts.map +1 -0
- package/lib/{typescript → types}/definitions/define-subcommand.d.ts +2 -4
- package/lib/types/definitions/define-subcommand.d.ts.map +1 -0
- package/lib/{typescript → types}/definitions/validate-cli-definition.d.ts +1 -1
- package/lib/types/definitions/validate-cli-definition.d.ts.map +1 -0
- package/lib/{typescript → types}/help-message/terminal-markdown.d.ts.map +1 -1
- package/lib/{typescript → types}/index.d.ts +7 -0
- package/lib/{typescript → types}/index.d.ts.map +1 -1
- package/lib/{typescript → types}/metadata/arguments-metadata.d.ts.map +1 -1
- package/lib/{typescript → types}/metadata/options-metadata.d.ts.map +1 -1
- package/lib/{typescript → types}/parse/context/cli-context-builder.d.ts +1 -1
- package/lib/{typescript → types}/parse/context/cli-context-builder.d.ts.map +1 -1
- package/lib/{typescript → types}/parse/context/object-context-builder.d.ts +1 -1
- package/lib/types/parse/context/object-context-builder.d.ts.map +1 -0
- package/lib/{typescript → types}/parse/parser-utilities.d.ts +9 -0
- package/lib/{typescript → types}/parse/parser-utilities.d.ts.map +1 -1
- package/lib/{typescript → types}/parse/safe-parse.d.ts.map +1 -1
- package/lib/{typescript → types}/parse/validation/validate-context.d.ts +1 -1
- package/lib/{typescript → types}/parse/validation/validate-context.d.ts.map +1 -1
- package/lib/{typescript → types}/parse/validation/validators/arguments.d.ts +1 -1
- package/lib/{typescript → types}/parse/validation/validators/arguments.d.ts.map +1 -1
- package/lib/{typescript → types}/parse/validation/validators/conflict.d.ts +3 -3
- package/lib/{typescript → types}/parse/validation/validators/conflict.d.ts.map +1 -1
- package/lib/{typescript → types}/parse/validation/validators/exclusive.d.ts +3 -3
- package/lib/{typescript → types}/parse/validation/validators/exclusive.d.ts.map +1 -1
- package/lib/{typescript → types}/parse/validation/validators/options.d.ts +1 -1
- package/lib/{typescript → types}/parse/validation/validators/options.d.ts.map +1 -1
- package/lib/{typescript → types}/parse/validation/validators/requires.d.ts +3 -3
- package/lib/{typescript → types}/parse/validation/validators/requires.d.ts.map +1 -1
- package/lib/{typescript → types}/types/definitions-types.d.ts +35 -4
- package/lib/types/types/definitions-types.d.ts.map +1 -0
- package/lib/types/types/error-types.d.ts +275 -0
- package/lib/types/types/error-types.d.ts.map +1 -0
- package/lib/{typescript → types}/types/io-types.d.ts +1 -1
- package/lib/types/types/types.d.ts +146 -0
- package/lib/types/types/types.d.ts.map +1 -0
- package/lib/{typescript → types}/types/utilities-types.d.ts +0 -2
- package/lib/{typescript → types}/types/utilities-types.d.ts.map +1 -1
- package/lib/types/utilities/parse-argv.d.ts +3 -0
- package/lib/types/utilities/parse-argv.d.ts.map +1 -0
- package/lib/types/utilities/schema-utilities.d.ts +15 -0
- package/lib/types/utilities/schema-utilities.d.ts.map +1 -0
- package/lib/{typescript → types/utilities}/utilities.d.ts +12 -15
- package/lib/types/utilities/utilities.d.ts.map +1 -0
- package/package.json +7 -7
- package/src/autocomplete-scripts/bash-autocomplete-script.ts +14 -12
- package/src/autocomplete-scripts/powershell-autocomplete-script.ts +67 -33
- package/src/autocomplete-scripts/zsh-autocomplete-script.ts +39 -31
- package/src/cli-error/cli-error.ts +44 -0
- package/src/cli-error/error-cause.ts +10 -0
- package/src/cli-error/error-code/definition-error-code.ts +21 -0
- package/src/cli-error/error-code/internal-error-code.ts +8 -0
- package/src/cli-error/error-code/parse-error-code.ts +16 -0
- package/src/cli-error/error-code/validation-error-code.ts +15 -0
- package/src/cli-error/error-message/definition-error-message.ts +144 -0
- package/src/cli-error/error-message/internal-error-message.ts +19 -0
- package/src/cli-error/error-message/parse-error-message.ts +76 -0
- package/src/cli-error/error-message/validation-error-message.ts +104 -0
- package/src/coerce/coerce-methods.ts +194 -39
- package/src/coerce/string-to-array.ts +2 -2
- package/src/coerce/string-to-primitive.ts +72 -13
- package/src/coerce/string-to-set.ts +2 -2
- package/src/definitions/define-arguments.ts +1 -1
- package/src/definitions/define-cli.ts +65 -30
- package/src/definitions/define-options.ts +1 -1
- package/src/definitions/define-subcommand.ts +47 -17
- package/src/definitions/validate-cli-definition.ts +287 -172
- package/src/help-message/format-arguments.ts +1 -1
- package/src/help-message/format-options.ts +1 -1
- package/src/help-message/format-subcommands.ts +1 -1
- package/src/help-message/generate-for-cli.ts +1 -1
- package/src/help-message/terminal-markdown.ts +3 -4
- package/src/index.ts +16 -0
- package/src/markdown/generate-markdown.ts +5 -4
- package/src/metadata/arguments-metadata.ts +2 -1
- package/src/metadata/options-metadata.ts +2 -1
- package/src/parse/context/cli-context-builder.ts +166 -39
- package/src/parse/context/object-context-builder.ts +37 -11
- package/src/parse/parser-utilities.ts +24 -10
- package/src/parse/safe-parse.ts +3 -6
- package/src/parse/validation/validate-context.ts +1 -1
- package/src/parse/validation/validators/arguments.ts +42 -14
- package/src/parse/validation/validators/conflict.ts +11 -20
- package/src/parse/validation/validators/exclusive.ts +16 -25
- package/src/parse/validation/validators/options.ts +40 -12
- package/src/parse/validation/validators/requires.ts +11 -20
- package/src/types/definitions-types.ts +36 -8
- package/src/types/error-types.ts +326 -0
- package/src/types/io-types.ts +1 -1
- package/src/types/types.ts +100 -7
- package/src/types/utilities-types.ts +0 -10
- package/src/utilities/parse-argv.ts +79 -0
- package/src/utilities/schema-utilities.ts +72 -0
- package/src/{utilities.ts → utilities/utilities.ts} +25 -138
- package/lib/cjs/autocomplete-scripts/bash-autocomplete-script.cjs +0 -2
- package/lib/cjs/autocomplete-scripts/bash-autocomplete-script.cjs.map +0 -1
- package/lib/cjs/autocomplete-scripts/powershell-autocomplete-script.cjs +0 -2
- package/lib/cjs/autocomplete-scripts/powershell-autocomplete-script.cjs.map +0 -1
- package/lib/cjs/autocomplete-scripts/zsh-autocomplete-script.cjs +0 -42
- package/lib/cjs/autocomplete-scripts/zsh-autocomplete-script.cjs.map +0 -1
- package/lib/cjs/coerce/coerce-methods.cjs +0 -2
- package/lib/cjs/coerce/coerce-methods.cjs.map +0 -1
- package/lib/cjs/coerce/string-to-array.cjs +0 -2
- package/lib/cjs/coerce/string-to-array.cjs.map +0 -1
- package/lib/cjs/coerce/string-to-primitive.cjs +0 -2
- package/lib/cjs/coerce/string-to-primitive.cjs.map +0 -1
- package/lib/cjs/coerce/string-to-set.cjs +0 -2
- package/lib/cjs/coerce/string-to-set.cjs.map +0 -1
- package/lib/cjs/definitions/define-arguments.cjs +0 -2
- package/lib/cjs/definitions/define-arguments.cjs.map +0 -1
- package/lib/cjs/definitions/define-cli.cjs +0 -2
- package/lib/cjs/definitions/define-cli.cjs.map +0 -1
- package/lib/cjs/definitions/define-options.cjs +0 -2
- package/lib/cjs/definitions/define-options.cjs.map +0 -1
- package/lib/cjs/definitions/define-subcommand.cjs +0 -2
- package/lib/cjs/definitions/define-subcommand.cjs.map +0 -1
- package/lib/cjs/definitions/validate-cli-definition.cjs +0 -2
- package/lib/cjs/definitions/validate-cli-definition.cjs.map +0 -1
- package/lib/cjs/help-message/format-arguments.cjs +0 -2
- package/lib/cjs/help-message/format-arguments.cjs.map +0 -1
- package/lib/cjs/help-message/format-options.cjs +0 -2
- package/lib/cjs/help-message/format-options.cjs.map +0 -1
- package/lib/cjs/help-message/format-subcommands.cjs +0 -2
- package/lib/cjs/help-message/format-subcommands.cjs.map +0 -1
- package/lib/cjs/help-message/generate-for-cli.cjs +0 -2
- package/lib/cjs/help-message/generate-for-cli.cjs.map +0 -1
- package/lib/cjs/help-message/generate-for-subcommand.cjs +0 -2
- package/lib/cjs/help-message/generate-for-subcommand.cjs.map +0 -1
- package/lib/cjs/help-message/print-help.cjs +0 -2
- package/lib/cjs/help-message/print-help.cjs.map +0 -1
- package/lib/cjs/help-message/set-defaults.cjs +0 -2
- package/lib/cjs/help-message/set-defaults.cjs.map +0 -1
- package/lib/cjs/help-message/styles.cjs +0 -2
- package/lib/cjs/help-message/styles.cjs.map +0 -1
- package/lib/cjs/help-message/terminal-markdown.cjs +0 -2
- package/lib/cjs/help-message/terminal-markdown.cjs.map +0 -1
- package/lib/cjs/index.cjs +0 -2
- package/lib/cjs/index.cjs.map +0 -1
- package/lib/cjs/markdown/generate-markdown.cjs +0 -2
- package/lib/cjs/markdown/generate-markdown.cjs.map +0 -1
- package/lib/cjs/metadata/arguments-metadata.cjs +0 -2
- package/lib/cjs/metadata/arguments-metadata.cjs.map +0 -1
- package/lib/cjs/metadata/cli-metadata.cjs +0 -2
- package/lib/cjs/metadata/cli-metadata.cjs.map +0 -1
- package/lib/cjs/metadata/options-metadata.cjs +0 -2
- package/lib/cjs/metadata/options-metadata.cjs.map +0 -1
- package/lib/cjs/metadata/subcommands-metadata.cjs +0 -2
- package/lib/cjs/metadata/subcommands-metadata.cjs.map +0 -1
- package/lib/cjs/parse/context/cli-context-builder.cjs +0 -2
- package/lib/cjs/parse/context/cli-context-builder.cjs.map +0 -1
- package/lib/cjs/parse/context/object-context-builder.cjs +0 -2
- package/lib/cjs/parse/context/object-context-builder.cjs.map +0 -1
- package/lib/cjs/parse/parser-utilities.cjs +0 -2
- package/lib/cjs/parse/parser-utilities.cjs.map +0 -1
- package/lib/cjs/parse/safe-parse.cjs +0 -2
- package/lib/cjs/parse/safe-parse.cjs.map +0 -1
- package/lib/cjs/parse/validation/validate-context.cjs +0 -2
- package/lib/cjs/parse/validation/validate-context.cjs.map +0 -1
- package/lib/cjs/parse/validation/validators/arguments.cjs +0 -2
- package/lib/cjs/parse/validation/validators/arguments.cjs.map +0 -1
- package/lib/cjs/parse/validation/validators/conflict.cjs +0 -2
- package/lib/cjs/parse/validation/validators/conflict.cjs.map +0 -1
- package/lib/cjs/parse/validation/validators/exclusive.cjs +0 -2
- package/lib/cjs/parse/validation/validators/exclusive.cjs.map +0 -1
- package/lib/cjs/parse/validation/validators/explicitly-passed.cjs +0 -2
- package/lib/cjs/parse/validation/validators/explicitly-passed.cjs.map +0 -1
- package/lib/cjs/parse/validation/validators/options.cjs +0 -2
- package/lib/cjs/parse/validation/validators/options.cjs.map +0 -1
- package/lib/cjs/parse/validation/validators/requires.cjs +0 -2
- package/lib/cjs/parse/validation/validators/requires.cjs.map +0 -1
- package/lib/cjs/utilities.cjs +0 -2
- package/lib/cjs/utilities.cjs.map +0 -1
- package/lib/esm/utilities.js +0 -2
- package/lib/esm/utilities.js.map +0 -1
- package/lib/mjs/utilities.mjs +0 -2
- package/lib/mjs/utilities.mjs.map +0 -1
- package/lib/typescript/autocomplete-scripts/powershell-autocomplete-script.d.ts +0 -14
- package/lib/typescript/autocomplete-scripts/zsh-autocomplete-script.d.ts +0 -11
- package/lib/typescript/coerce/coerce-methods.d.ts +0 -20
- package/lib/typescript/coerce/coerce-methods.d.ts.map +0 -1
- package/lib/typescript/coerce/string-to-array.d.ts.map +0 -1
- package/lib/typescript/coerce/string-to-primitive.d.ts +0 -5
- package/lib/typescript/coerce/string-to-primitive.d.ts.map +0 -1
- package/lib/typescript/definitions/define-cli.d.ts.map +0 -1
- package/lib/typescript/definitions/define-subcommand.d.ts.map +0 -1
- package/lib/typescript/definitions/validate-cli-definition.d.ts.map +0 -1
- package/lib/typescript/parse/context/object-context-builder.d.ts.map +0 -1
- package/lib/typescript/types/definitions-types.d.ts.map +0 -1
- package/lib/typescript/types/types.d.ts +0 -61
- package/lib/typescript/types/types.d.ts.map +0 -1
- package/lib/typescript/utilities.d.ts.map +0 -1
- /package/lib/{typescript → types}/autocomplete-scripts/bash-autocomplete-script.d.ts +0 -0
- /package/lib/{typescript → types}/definitions/define-arguments.d.ts +0 -0
- /package/lib/{typescript → types}/definitions/define-arguments.d.ts.map +0 -0
- /package/lib/{typescript → types}/definitions/define-options.d.ts +0 -0
- /package/lib/{typescript → types}/definitions/define-options.d.ts.map +0 -0
- /package/lib/{typescript → types}/help-message/format-arguments.d.ts +0 -0
- /package/lib/{typescript → types}/help-message/format-arguments.d.ts.map +0 -0
- /package/lib/{typescript → types}/help-message/format-options.d.ts +0 -0
- /package/lib/{typescript → types}/help-message/format-options.d.ts.map +0 -0
- /package/lib/{typescript → types}/help-message/format-subcommands.d.ts +0 -0
- /package/lib/{typescript → types}/help-message/format-subcommands.d.ts.map +0 -0
- /package/lib/{typescript → types}/help-message/generate-for-cli.d.ts +0 -0
- /package/lib/{typescript → types}/help-message/generate-for-cli.d.ts.map +0 -0
- /package/lib/{typescript → types}/help-message/generate-for-subcommand.d.ts +0 -0
- /package/lib/{typescript → types}/help-message/generate-for-subcommand.d.ts.map +0 -0
- /package/lib/{typescript → types}/help-message/print-help.d.ts +0 -0
- /package/lib/{typescript → types}/help-message/print-help.d.ts.map +0 -0
- /package/lib/{typescript → types}/help-message/set-defaults.d.ts +0 -0
- /package/lib/{typescript → types}/help-message/set-defaults.d.ts.map +0 -0
- /package/lib/{typescript → types}/help-message/styles.d.ts +0 -0
- /package/lib/{typescript → types}/help-message/styles.d.ts.map +0 -0
- /package/lib/{typescript → types}/help-message/terminal-markdown.d.ts +0 -0
- /package/lib/{typescript → types}/markdown/generate-markdown.d.ts +0 -0
- /package/lib/{typescript → types}/markdown/generate-markdown.d.ts.map +0 -0
- /package/lib/{typescript → types}/metadata/arguments-metadata.d.ts +0 -0
- /package/lib/{typescript → types}/metadata/cli-metadata.d.ts +0 -0
- /package/lib/{typescript → types}/metadata/cli-metadata.d.ts.map +0 -0
- /package/lib/{typescript → types}/metadata/options-metadata.d.ts +0 -0
- /package/lib/{typescript → types}/metadata/subcommands-metadata.d.ts +0 -0
- /package/lib/{typescript → types}/metadata/subcommands-metadata.d.ts.map +0 -0
- /package/lib/{typescript → types}/parse/safe-parse.d.ts +0 -0
- /package/lib/{typescript → types}/parse/validation/validators/explicitly-passed.d.ts +0 -0
- /package/lib/{typescript → types}/parse/validation/validators/explicitly-passed.d.ts.map +0 -0
- /package/lib/{typescript → types}/types/context-types.d.ts +0 -0
- /package/lib/{typescript → types}/types/context-types.d.ts.map +0 -0
- /package/lib/{typescript → types}/types/help-message-types.d.ts +0 -0
- /package/lib/{typescript → types}/types/help-message-types.d.ts.map +0 -0
- /package/lib/{typescript → types}/types/io-types.d.ts.map +0 -0
- /package/lib/{typescript → types}/types/metadata-types.d.ts +0 -0
- /package/lib/{typescript → types}/types/metadata-types.d.ts.map +0 -0
- /package/lib/{typescript → types}/types/schema-types.d.ts +0 -0
- /package/lib/{typescript → types}/types/schema-types.d.ts.map +0 -0
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
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
|
|
15
|
+
throw new CliError({
|
|
16
|
+
cause: ErrorCause.Validation,
|
|
17
|
+
code: ValidationErrorCode.CoercionFailed,
|
|
18
|
+
context: { coerceToType: "boolean", providedValue: string },
|
|
19
|
+
});
|
|
12
20
|
}
|
|
13
21
|
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
19
|
-
if (
|
|
20
|
-
throw new
|
|
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
|
-
|
|
46
|
+
return number;
|
|
47
|
+
}
|
|
24
48
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
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 {
|
|
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 {
|
|
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,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
|
|
46
|
+
const cliDefinition = input as T;
|
|
44
47
|
|
|
45
|
-
prepareDefinitionTypes(
|
|
46
|
-
prepareDefinitionTypes(
|
|
48
|
+
prepareDefinitionTypes(cliDefinition.options);
|
|
49
|
+
prepareDefinitionTypes(cliDefinition.arguments);
|
|
47
50
|
|
|
48
|
-
if (
|
|
49
|
-
for (const subcommand of Object.values(
|
|
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
|
-
|
|
57
|
-
|
|
59
|
+
cliDefinition._onExecute ??= [];
|
|
60
|
+
cliDefinition._onExecute.push(handler);
|
|
58
61
|
|
|
59
62
|
return () => {
|
|
60
|
-
const handlerIndex =
|
|
63
|
+
const handlerIndex = cliDefinition._onExecute?.indexOf(handler);
|
|
61
64
|
if (!handlerIndex || handlerIndex < 0) return;
|
|
62
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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(
|
|
111
|
+
return generateCliHelpMessage(cliDefinition, options);
|
|
83
112
|
},
|
|
84
113
|
generateSubcommandHelpMessage(subcommandName: string, options?: PrintHelpOptions) {
|
|
85
|
-
const foundSubcommand =
|
|
86
|
-
if (!foundSubcommand)
|
|
87
|
-
|
|
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(
|
|
126
|
+
Object.assign(cliDefinition, generateHelpMethods);
|
|
92
127
|
|
|
93
|
-
if (
|
|
94
|
-
for (const subcommandSchema of
|
|
128
|
+
if (cliDefinition.subcommands) {
|
|
129
|
+
for (const subcommandSchema of cliDefinition.subcommands) {
|
|
95
130
|
Object.assign(subcommandSchema, generateHelpMethods);
|
|
96
131
|
}
|
|
97
132
|
}
|
|
98
133
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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,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
|
|
30
|
+
const subcommandDefinition = input as T;
|
|
28
31
|
|
|
29
|
-
prepareDefinitionTypes(
|
|
30
|
-
prepareDefinitionTypes(
|
|
32
|
+
prepareDefinitionTypes(subcommandDefinition.options);
|
|
33
|
+
prepareDefinitionTypes(subcommandDefinition.arguments);
|
|
31
34
|
|
|
32
35
|
const onExecute = (handler: (Subcommand["_onExecute"] & {})[number]) => {
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
subcommandDefinition._onExecute ??= [];
|
|
37
|
+
subcommandDefinition._onExecute.push(handler);
|
|
35
38
|
|
|
36
39
|
return () => {
|
|
37
|
-
const handlerIndex =
|
|
40
|
+
const handlerIndex = subcommandDefinition._onExecute?.indexOf(handler);
|
|
38
41
|
if (!handlerIndex || handlerIndex < 0) return;
|
|
39
|
-
|
|
42
|
+
subcommandDefinition._onExecute?.splice(handlerIndex, 1);
|
|
40
43
|
};
|
|
41
44
|
};
|
|
42
45
|
|
|
43
46
|
const execute: AttachedMethodsWide["execute"] = inputValues => {
|
|
44
47
|
inputValues ??= {};
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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(
|
|
86
|
+
return Object.assign(subcommandDefinition, { onExecute, execute, executeAsync }) as Prettify<T & AttachedMethods<T>>;
|
|
57
87
|
}
|