@villedemontreal/caporal 3.1.7
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 +21 -0
- package/README.md +29 -0
- package/dist/src/__tests__/issue-163.spec.d.ts +2 -0
- package/dist/src/__tests__/issue-163.spec.d.ts.map +1 -0
- package/dist/src/__tests__/issue-163.spec.js +17 -0
- package/dist/src/__tests__/issue-163.spec.js.map +1 -0
- package/dist/src/argument/__tests__/argument.spec.d.ts +2 -0
- package/dist/src/argument/__tests__/argument.spec.d.ts.map +1 -0
- package/dist/src/argument/__tests__/argument.spec.js +85 -0
- package/dist/src/argument/__tests__/argument.spec.js.map +1 -0
- package/dist/src/argument/find.d.ts +8 -0
- package/dist/src/argument/find.d.ts.map +1 -0
- package/dist/src/argument/find.js +11 -0
- package/dist/src/argument/find.js.map +1 -0
- package/dist/src/argument/index.d.ts +13 -0
- package/dist/src/argument/index.d.ts.map +1 -0
- package/dist/src/argument/index.js +27 -0
- package/dist/src/argument/index.js.map +1 -0
- package/dist/src/argument/synopsis.d.ts +19 -0
- package/dist/src/argument/synopsis.d.ts.map +1 -0
- package/dist/src/argument/synopsis.js +39 -0
- package/dist/src/argument/synopsis.js.map +1 -0
- package/dist/src/argument/validate.d.ts +43 -0
- package/dist/src/argument/validate.d.ts.map +1 -0
- package/dist/src/argument/validate.js +127 -0
- package/dist/src/argument/validate.js.map +1 -0
- package/dist/src/autocomplete/__fixtures__/prog-autocomplete.d.ts +3 -0
- package/dist/src/autocomplete/__fixtures__/prog-autocomplete.d.ts.map +1 -0
- package/dist/src/autocomplete/__fixtures__/prog-autocomplete.js +24 -0
- package/dist/src/autocomplete/__fixtures__/prog-autocomplete.js.map +1 -0
- package/dist/src/autocomplete/__tests__/autocomplete.spec.d.ts +2 -0
- package/dist/src/autocomplete/__tests__/autocomplete.spec.d.ts.map +1 -0
- package/dist/src/autocomplete/__tests__/autocomplete.spec.js +119 -0
- package/dist/src/autocomplete/__tests__/autocomplete.spec.js.map +1 -0
- package/dist/src/autocomplete/index.d.ts +20 -0
- package/dist/src/autocomplete/index.d.ts.map +1 -0
- package/dist/src/autocomplete/index.js +172 -0
- package/dist/src/autocomplete/index.js.map +1 -0
- package/dist/src/autocomplete/types.d.ts +26 -0
- package/dist/src/autocomplete/types.d.ts.map +1 -0
- package/dist/src/autocomplete/types.js +3 -0
- package/dist/src/autocomplete/types.js.map +1 -0
- package/dist/src/command/__fixtures__/example-cmd.d.ts +8 -0
- package/dist/src/command/__fixtures__/example-cmd.d.ts.map +1 -0
- package/dist/src/command/__fixtures__/example-cmd.js +10 -0
- package/dist/src/command/__fixtures__/example-cmd.js.map +1 -0
- package/dist/src/command/__tests__/command.spec.d.ts +2 -0
- package/dist/src/command/__tests__/command.spec.d.ts.map +1 -0
- package/dist/src/command/__tests__/command.spec.js +510 -0
- package/dist/src/command/__tests__/command.spec.js.map +1 -0
- package/dist/src/command/__tests__/find.spec.d.ts +2 -0
- package/dist/src/command/__tests__/find.spec.d.ts.map +1 -0
- package/dist/src/command/__tests__/find.spec.js +31 -0
- package/dist/src/command/__tests__/find.spec.js.map +1 -0
- package/dist/src/command/__tests__/import.spec.d.ts +2 -0
- package/dist/src/command/__tests__/import.spec.d.ts.map +1 -0
- package/dist/src/command/__tests__/import.spec.js +17 -0
- package/dist/src/command/__tests__/import.spec.js.map +1 -0
- package/dist/src/command/__tests__/scan.spec.d.ts +2 -0
- package/dist/src/command/__tests__/scan.spec.d.ts.map +1 -0
- package/dist/src/command/__tests__/scan.spec.js +20 -0
- package/dist/src/command/__tests__/scan.spec.js.map +1 -0
- package/dist/src/command/find.d.ts +8 -0
- package/dist/src/command/find.d.ts.map +1 -0
- package/dist/src/command/find.js +47 -0
- package/dist/src/command/find.js.map +1 -0
- package/dist/src/command/import.d.ts +7 -0
- package/dist/src/command/import.d.ts.map +1 -0
- package/dist/src/command/import.js +47 -0
- package/dist/src/command/import.js.map +1 -0
- package/dist/src/command/index.d.ts +211 -0
- package/dist/src/command/index.d.ts.map +1 -0
- package/dist/src/command/index.js +374 -0
- package/dist/src/command/index.js.map +1 -0
- package/dist/src/command/scan.d.ts +4 -0
- package/dist/src/command/scan.d.ts.map +1 -0
- package/dist/src/command/scan.js +31 -0
- package/dist/src/command/scan.js.map +1 -0
- package/dist/src/command/validate-call.d.ts +8 -0
- package/dist/src/command/validate-call.d.ts.map +1 -0
- package/dist/src/command/validate-call.js +13 -0
- package/dist/src/command/validate-call.js.map +1 -0
- package/dist/src/config/index.d.ts +7 -0
- package/dist/src/config/index.d.ts.map +1 -0
- package/dist/src/config/index.js +24 -0
- package/dist/src/config/index.js.map +1 -0
- package/dist/src/error/__tests__/fatal.spec.d.ts +2 -0
- package/dist/src/error/__tests__/fatal.spec.d.ts.map +1 -0
- package/dist/src/error/__tests__/fatal.spec.js +34 -0
- package/dist/src/error/__tests__/fatal.spec.js.map +1 -0
- package/dist/src/error/action.d.ts +9 -0
- package/dist/src/error/action.d.ts.map +1 -0
- package/dist/src/error/action.js +16 -0
- package/dist/src/error/action.js.map +1 -0
- package/dist/src/error/base.d.ts +11 -0
- package/dist/src/error/base.d.ts.map +1 -0
- package/dist/src/error/base.js +18 -0
- package/dist/src/error/base.js.map +1 -0
- package/dist/src/error/fatal.d.ts +10 -0
- package/dist/src/error/fatal.d.ts.map +1 -0
- package/dist/src/error/fatal.js +27 -0
- package/dist/src/error/fatal.js.map +1 -0
- package/dist/src/error/index.d.ts +18 -0
- package/dist/src/error/index.d.ts.map +1 -0
- package/dist/src/error/index.js +34 -0
- package/dist/src/error/index.js.map +1 -0
- package/dist/src/error/invalid-validator.d.ts +10 -0
- package/dist/src/error/invalid-validator.d.ts.map +1 -0
- package/dist/src/error/invalid-validator.js +15 -0
- package/dist/src/error/invalid-validator.js.map +1 -0
- package/dist/src/error/missing-argument.d.ts +11 -0
- package/dist/src/error/missing-argument.d.ts.map +1 -0
- package/dist/src/error/missing-argument.js +20 -0
- package/dist/src/error/missing-argument.js.map +1 -0
- package/dist/src/error/missing-flag.d.ts +11 -0
- package/dist/src/error/missing-flag.d.ts.map +1 -0
- package/dist/src/error/missing-flag.js +20 -0
- package/dist/src/error/missing-flag.js.map +1 -0
- package/dist/src/error/multi-validation.d.ts +10 -0
- package/dist/src/error/multi-validation.d.ts.map +1 -0
- package/dist/src/error/multi-validation.js +26 -0
- package/dist/src/error/multi-validation.js.map +1 -0
- package/dist/src/error/no-action.d.ts +10 -0
- package/dist/src/error/no-action.d.ts.map +1 -0
- package/dist/src/error/no-action.js +22 -0
- package/dist/src/error/no-action.js.map +1 -0
- package/dist/src/error/option-synopsis-syntax.d.ts +9 -0
- package/dist/src/error/option-synopsis-syntax.d.ts.map +1 -0
- package/dist/src/error/option-synopsis-syntax.js +15 -0
- package/dist/src/error/option-synopsis-syntax.js.map +1 -0
- package/dist/src/error/too-many-arguments.d.ts +11 -0
- package/dist/src/error/too-many-arguments.d.ts.map +1 -0
- package/dist/src/error/too-many-arguments.js +25 -0
- package/dist/src/error/too-many-arguments.js.map +1 -0
- package/dist/src/error/unknown-command.d.ts +13 -0
- package/dist/src/error/unknown-command.d.ts.map +1 -0
- package/dist/src/error/unknown-command.js +44 -0
- package/dist/src/error/unknown-command.js.map +1 -0
- package/dist/src/error/unknown-option.d.ts +13 -0
- package/dist/src/error/unknown-option.d.ts.map +1 -0
- package/dist/src/error/unknown-option.js +40 -0
- package/dist/src/error/unknown-option.js.map +1 -0
- package/dist/src/error/validation.d.ts +17 -0
- package/dist/src/error/validation.d.ts.map +1 -0
- package/dist/src/error/validation.js +49 -0
- package/dist/src/error/validation.js.map +1 -0
- package/dist/src/help/__tests__/help.spec.d.ts +2 -0
- package/dist/src/help/__tests__/help.spec.d.ts.map +1 -0
- package/dist/src/help/__tests__/help.spec.js +130 -0
- package/dist/src/help/__tests__/help.spec.js.map +1 -0
- package/dist/src/help/__tests__/utils.spec.d.ts +2 -0
- package/dist/src/help/__tests__/utils.spec.d.ts.map +1 -0
- package/dist/src/help/__tests__/utils.spec.js +16 -0
- package/dist/src/help/__tests__/utils.spec.js.map +1 -0
- package/dist/src/help/index.d.ts +47 -0
- package/dist/src/help/index.d.ts.map +1 -0
- package/dist/src/help/index.js +131 -0
- package/dist/src/help/index.js.map +1 -0
- package/dist/src/help/templates/command.d.ts +7 -0
- package/dist/src/help/templates/command.d.ts.map +1 -0
- package/dist/src/help/templates/command.js +23 -0
- package/dist/src/help/templates/command.js.map +1 -0
- package/dist/src/help/templates/custom.d.ts +7 -0
- package/dist/src/help/templates/custom.d.ts.map +1 -0
- package/dist/src/help/templates/custom.js +24 -0
- package/dist/src/help/templates/custom.js.map +1 -0
- package/dist/src/help/templates/header.d.ts +7 -0
- package/dist/src/help/templates/header.d.ts.map +1 -0
- package/dist/src/help/templates/header.js +16 -0
- package/dist/src/help/templates/header.js.map +1 -0
- package/dist/src/help/templates/index.d.ts +10 -0
- package/dist/src/help/templates/index.d.ts.map +1 -0
- package/dist/src/help/templates/index.js +26 -0
- package/dist/src/help/templates/index.js.map +1 -0
- package/dist/src/help/templates/program.d.ts +7 -0
- package/dist/src/help/templates/program.d.ts.map +1 -0
- package/dist/src/help/templates/program.js +18 -0
- package/dist/src/help/templates/program.js.map +1 -0
- package/dist/src/help/templates/usage.d.ts +7 -0
- package/dist/src/help/templates/usage.d.ts.map +1 -0
- package/dist/src/help/templates/usage.js +19 -0
- package/dist/src/help/templates/usage.js.map +1 -0
- package/dist/src/help/types.d.ts +53 -0
- package/dist/src/help/types.d.ts.map +1 -0
- package/dist/src/help/types.js +3 -0
- package/dist/src/help/types.js.map +1 -0
- package/dist/src/help/utils.d.ts +9 -0
- package/dist/src/help/utils.d.ts.map +1 -0
- package/dist/src/help/utils.js +92 -0
- package/dist/src/help/utils.js.map +1 -0
- package/dist/src/index.d.ts +90 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +111 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/logger/__tests__/logger.spec.d.ts +2 -0
- package/dist/src/logger/__tests__/logger.spec.d.ts.map +1 -0
- package/dist/src/logger/__tests__/logger.spec.js +56 -0
- package/dist/src/logger/__tests__/logger.spec.js.map +1 -0
- package/dist/src/logger/index.d.ts +6 -0
- package/dist/src/logger/index.d.ts.map +1 -0
- package/dist/src/logger/index.js +128 -0
- package/dist/src/logger/index.js.map +1 -0
- package/dist/src/option/__tests__/global.spec.d.ts +2 -0
- package/dist/src/option/__tests__/global.spec.d.ts.map +1 -0
- package/dist/src/option/__tests__/global.spec.js +121 -0
- package/dist/src/option/__tests__/global.spec.js.map +1 -0
- package/dist/src/option/__tests__/option.spec.d.ts +2 -0
- package/dist/src/option/__tests__/option.spec.d.ts.map +1 -0
- package/dist/src/option/__tests__/option.spec.js +111 -0
- package/dist/src/option/__tests__/option.spec.js.map +1 -0
- package/dist/src/option/find.d.ts +14 -0
- package/dist/src/option/find.d.ts.map +1 -0
- package/dist/src/option/find.js +17 -0
- package/dist/src/option/find.js.map +1 -0
- package/dist/src/option/index.d.ts +65 -0
- package/dist/src/option/index.d.ts.map +1 -0
- package/dist/src/option/index.js +230 -0
- package/dist/src/option/index.js.map +1 -0
- package/dist/src/option/mapping.d.ts +7 -0
- package/dist/src/option/mapping.d.ts.map +1 -0
- package/dist/src/option/mapping.js +21 -0
- package/dist/src/option/mapping.js.map +1 -0
- package/dist/src/option/utils.d.ts +35 -0
- package/dist/src/option/utils.d.ts.map +1 -0
- package/dist/src/option/utils.js +139 -0
- package/dist/src/option/utils.js.map +1 -0
- package/dist/src/option/validate.d.ts +15 -0
- package/dist/src/option/validate.d.ts.map +1 -0
- package/dist/src/option/validate.js +63 -0
- package/dist/src/option/validate.js.map +1 -0
- package/dist/src/parser/__tests__/parser.spec.d.ts +2 -0
- package/dist/src/parser/__tests__/parser.spec.d.ts.map +1 -0
- package/dist/src/parser/__tests__/parser.spec.js +343 -0
- package/dist/src/parser/__tests__/parser.spec.js.map +1 -0
- package/dist/src/parser/index.d.ts +17 -0
- package/dist/src/parser/index.d.ts.map +1 -0
- package/dist/src/parser/index.js +299 -0
- package/dist/src/parser/index.js.map +1 -0
- package/dist/src/program/__tests__/program.spec.d.ts +2 -0
- package/dist/src/program/__tests__/program.spec.d.ts.map +1 -0
- package/dist/src/program/__tests__/program.spec.js +261 -0
- package/dist/src/program/__tests__/program.spec.js.map +1 -0
- package/dist/src/program/index.d.ts +349 -0
- package/dist/src/program/index.d.ts.map +1 -0
- package/dist/src/program/index.js +584 -0
- package/dist/src/program/index.js.map +1 -0
- package/dist/src/types.d.ts +396 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +51 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils/__tests__/fs.spec.d.ts +2 -0
- package/dist/src/utils/__tests__/fs.spec.d.ts.map +1 -0
- package/dist/src/utils/__tests__/fs.spec.js +14 -0
- package/dist/src/utils/__tests__/fs.spec.js.map +1 -0
- package/dist/src/utils/__tests__/levenshtein.spec.d.ts +2 -0
- package/dist/src/utils/__tests__/levenshtein.spec.d.ts.map +1 -0
- package/dist/src/utils/__tests__/levenshtein.spec.js +18 -0
- package/dist/src/utils/__tests__/levenshtein.spec.js.map +1 -0
- package/dist/src/utils/__tests__/suggest.spec.d.ts +2 -0
- package/dist/src/utils/__tests__/suggest.spec.d.ts.map +1 -0
- package/dist/src/utils/__tests__/suggest.spec.js +31 -0
- package/dist/src/utils/__tests__/suggest.spec.js.map +1 -0
- package/dist/src/utils/colorize.d.ts +2 -0
- package/dist/src/utils/colorize.d.ts.map +1 -0
- package/dist/src/utils/colorize.js +27 -0
- package/dist/src/utils/colorize.js.map +1 -0
- package/dist/src/utils/fs.d.ts +2 -0
- package/dist/src/utils/fs.d.ts.map +1 -0
- package/dist/src/utils/fs.js +19 -0
- package/dist/src/utils/fs.js.map +1 -0
- package/dist/src/utils/levenshtein.d.ts +6 -0
- package/dist/src/utils/levenshtein.d.ts.map +1 -0
- package/dist/src/utils/levenshtein.js +32 -0
- package/dist/src/utils/levenshtein.js.map +1 -0
- package/dist/src/utils/suggest.d.ts +15 -0
- package/dist/src/utils/suggest.d.ts.map +1 -0
- package/dist/src/utils/suggest.js +49 -0
- package/dist/src/utils/suggest.js.map +1 -0
- package/dist/src/utils/version.d.ts +2 -0
- package/dist/src/utils/version.d.ts.map +1 -0
- package/dist/src/utils/version.js +21 -0
- package/dist/src/utils/version.js.map +1 -0
- package/dist/src/utils/web/autocomplete.d.ts +12 -0
- package/dist/src/utils/web/autocomplete.d.ts.map +1 -0
- package/dist/src/utils/web/autocomplete.js +12 -0
- package/dist/src/utils/web/autocomplete.js.map +1 -0
- package/dist/src/utils/web/process.d.ts +15 -0
- package/dist/src/utils/web/process.d.ts.map +1 -0
- package/dist/src/utils/web/process.js +29 -0
- package/dist/src/utils/web/process.js.map +1 -0
- package/dist/src/validator/__tests__/array.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/array.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/array.spec.js +34 -0
- package/dist/src/validator/__tests__/array.spec.js.map +1 -0
- package/dist/src/validator/__tests__/caporal.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/caporal.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/caporal.spec.js +86 -0
- package/dist/src/validator/__tests__/caporal.spec.js.map +1 -0
- package/dist/src/validator/__tests__/function.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/function.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/function.spec.js +42 -0
- package/dist/src/validator/__tests__/function.spec.js.map +1 -0
- package/dist/src/validator/__tests__/regexp.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/regexp.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/regexp.spec.js +34 -0
- package/dist/src/validator/__tests__/regexp.spec.js.map +1 -0
- package/dist/src/validator/__tests__/utils.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/utils.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/utils.spec.js +66 -0
- package/dist/src/validator/__tests__/utils.spec.js.map +1 -0
- package/dist/src/validator/__tests__/validate.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/validate.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/validate.spec.js +25 -0
- package/dist/src/validator/__tests__/validate.spec.js.map +1 -0
- package/dist/src/validator/array.d.ts +14 -0
- package/dist/src/validator/array.d.ts.map +1 -0
- package/dist/src/validator/array.js +29 -0
- package/dist/src/validator/array.js.map +1 -0
- package/dist/src/validator/caporal.d.ts +25 -0
- package/dist/src/validator/caporal.d.ts.map +1 -0
- package/dist/src/validator/caporal.js +89 -0
- package/dist/src/validator/caporal.js.map +1 -0
- package/dist/src/validator/function.d.ts +7 -0
- package/dist/src/validator/function.d.ts.map +1 -0
- package/dist/src/validator/function.js +27 -0
- package/dist/src/validator/function.js.map +1 -0
- package/dist/src/validator/regexp.d.ts +14 -0
- package/dist/src/validator/regexp.d.ts.map +1 -0
- package/dist/src/validator/regexp.js +31 -0
- package/dist/src/validator/regexp.js.map +1 -0
- package/dist/src/validator/utils.d.ts +14 -0
- package/dist/src/validator/utils.d.ts.map +1 -0
- package/dist/src/validator/utils.js +81 -0
- package/dist/src/validator/utils.js.map +1 -0
- package/dist/src/validator/validate.d.ts +7 -0
- package/dist/src/validator/validate.d.ts.map +1 -0
- package/dist/src/validator/validate.js +29 -0
- package/dist/src/validator/validate.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +75 -0
- package/src/__tests__/issue-163.spec.ts +18 -0
- package/src/argument/__tests__/argument.spec.ts +84 -0
- package/src/argument/find.ts +11 -0
- package/src/argument/index.ts +35 -0
- package/src/argument/synopsis.ts +41 -0
- package/src/argument/validate.ts +176 -0
- package/src/autocomplete/__fixtures__/prog-autocomplete.ts +24 -0
- package/src/autocomplete/__tests__/autocomplete.spec.ts +129 -0
- package/src/autocomplete/index.ts +225 -0
- package/src/autocomplete/types.ts +29 -0
- package/src/command/__fixtures__/example-cmd.ts +14 -0
- package/src/command/__tests__/command.spec.ts +621 -0
- package/src/command/__tests__/find.spec.ts +32 -0
- package/src/command/__tests__/import.spec.ts +14 -0
- package/src/command/__tests__/scan.spec.ts +16 -0
- package/src/command/find.ts +57 -0
- package/src/command/import.ts +13 -0
- package/src/command/index.ts +458 -0
- package/src/command/scan.ts +30 -0
- package/src/command/validate-call.ts +19 -0
- package/src/config/index.ts +26 -0
- package/src/error/__tests__/fatal.spec.ts +38 -0
- package/src/error/action.ts +13 -0
- package/src/error/base.ts +20 -0
- package/src/error/fatal.ts +25 -0
- package/src/error/index.ts +17 -0
- package/src/error/invalid-validator.ts +12 -0
- package/src/error/missing-argument.ts +16 -0
- package/src/error/missing-flag.ts +16 -0
- package/src/error/multi-validation.ts +22 -0
- package/src/error/no-action.ts +19 -0
- package/src/error/option-synopsis-syntax.ts +12 -0
- package/src/error/too-many-arguments.ts +28 -0
- package/src/error/unknown-command.ts +41 -0
- package/src/error/unknown-option.ts +40 -0
- package/src/error/validation.ts +80 -0
- package/src/help/__tests__/__snapshots__/help.spec.ts.snap +334 -0
- package/src/help/__tests__/help.spec.ts +144 -0
- package/src/help/__tests__/utils.spec.ts +14 -0
- package/src/help/index.ts +107 -0
- package/src/help/templates/command.ts +27 -0
- package/src/help/templates/custom.ts +25 -0
- package/src/help/templates/header.ts +19 -0
- package/src/help/templates/index.ts +9 -0
- package/src/help/templates/program.ts +21 -0
- package/src/help/templates/usage.ts +24 -0
- package/src/help/types.ts +57 -0
- package/src/help/utils.ts +109 -0
- package/src/index.ts +93 -0
- package/src/logger/__tests__/logger.spec.ts +65 -0
- package/src/logger/index.ts +97 -0
- package/src/option/__tests__/global.spec.ts +141 -0
- package/src/option/__tests__/option.spec.ts +115 -0
- package/src/option/find.ts +17 -0
- package/src/option/index.ts +280 -0
- package/src/option/mapping.ts +17 -0
- package/src/option/utils.ts +141 -0
- package/src/option/validate.ts +80 -0
- package/src/parser/__tests__/parser.spec.ts +371 -0
- package/src/parser/index.ts +383 -0
- package/src/program/__tests__/program.spec.ts +316 -0
- package/src/program/index.ts +676 -0
- package/src/types.ts +432 -0
- package/src/utils/__tests__/fs.spec.ts +14 -0
- package/src/utils/__tests__/levenshtein.spec.ts +16 -0
- package/src/utils/__tests__/suggest.spec.ts +30 -0
- package/src/utils/colorize.ts +21 -0
- package/src/utils/fs.ts +13 -0
- package/src/utils/levenshtein.ts +28 -0
- package/src/utils/suggest.ts +52 -0
- package/src/utils/version.ts +14 -0
- package/src/utils/web/autocomplete.ts +19 -0
- package/src/utils/web/process.ts +24 -0
- package/src/validator/__tests__/array.spec.ts +41 -0
- package/src/validator/__tests__/caporal.spec.ts +132 -0
- package/src/validator/__tests__/function.spec.ts +52 -0
- package/src/validator/__tests__/regexp.spec.ts +38 -0
- package/src/validator/__tests__/utils.spec.ts +67 -0
- package/src/validator/__tests__/validate.spec.ts +24 -0
- package/src/validator/array.ts +31 -0
- package/src/validator/caporal.ts +104 -0
- package/src/validator/function.ts +31 -0
- package/src/validator/regexp.ts +34 -0
- package/src/validator/utils.ts +84 -0
- package/src/validator/validate.ts +38 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @module caporal/option
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
Option,
|
|
8
|
+
OptionValueType,
|
|
9
|
+
CreateOptionProgramOpts,
|
|
10
|
+
CreateOptionCommandOpts,
|
|
11
|
+
Action,
|
|
12
|
+
ActionParameters,
|
|
13
|
+
GlobalOptions,
|
|
14
|
+
ParserProcessedResult,
|
|
15
|
+
} from "../types"
|
|
16
|
+
import { checkValidator, isBoolValidator, getTypeHint } from "../validator/utils"
|
|
17
|
+
import { parseOptionSynopsis } from "./utils"
|
|
18
|
+
import { logger } from "../logger"
|
|
19
|
+
import type { Command } from "../command"
|
|
20
|
+
import type { Program } from "../program"
|
|
21
|
+
import { getHelp } from "../help"
|
|
22
|
+
import { installCompletion, uninstallCompletion } from "../autocomplete"
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Create an Option object
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
* @param synopsis
|
|
29
|
+
* @param description
|
|
30
|
+
* @param options
|
|
31
|
+
*/
|
|
32
|
+
export function createOption(
|
|
33
|
+
synopsis: string,
|
|
34
|
+
description: string,
|
|
35
|
+
options: CreateOptionProgramOpts | CreateOptionCommandOpts = {},
|
|
36
|
+
): Option {
|
|
37
|
+
// eslint-disable-next-line prefer-const
|
|
38
|
+
let { validator, required, hidden } = options
|
|
39
|
+
|
|
40
|
+
// force casting
|
|
41
|
+
required = Boolean(required)
|
|
42
|
+
|
|
43
|
+
checkValidator(validator)
|
|
44
|
+
const syno = parseOptionSynopsis(synopsis)
|
|
45
|
+
let boolean = syno.valueType === OptionValueType.None || isBoolValidator(validator)
|
|
46
|
+
if (validator && !isBoolValidator(validator)) {
|
|
47
|
+
boolean = false
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const opt: Option = {
|
|
51
|
+
kind: "option",
|
|
52
|
+
default: boolean == true ? Boolean(options.default) : options.default,
|
|
53
|
+
description,
|
|
54
|
+
choices: Array.isArray(validator) ? validator : [],
|
|
55
|
+
...syno,
|
|
56
|
+
required,
|
|
57
|
+
visible: !hidden,
|
|
58
|
+
boolean,
|
|
59
|
+
validator,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
opt.typeHint = getTypeHint(opt)
|
|
63
|
+
|
|
64
|
+
return opt
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { showHelp }
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Display help. Return false to prevent further processing.
|
|
71
|
+
*
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
const showHelp: Action = async ({ program, command }: ActionParameters) => {
|
|
75
|
+
const help = await getHelp(program, command)
|
|
76
|
+
// eslint-disable-next-line no-console
|
|
77
|
+
console.log(help)
|
|
78
|
+
program.emit("help", help)
|
|
79
|
+
return false
|
|
80
|
+
},
|
|
81
|
+
/**
|
|
82
|
+
* Display program version. Return false to prevent further processing.
|
|
83
|
+
*
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
86
|
+
showVersion: Action = ({ program }: ActionParameters) => {
|
|
87
|
+
// eslint-disable-next-line no-console
|
|
88
|
+
console.log(program.getVersion())
|
|
89
|
+
program.emit("version", program.getVersion())
|
|
90
|
+
return false
|
|
91
|
+
},
|
|
92
|
+
/**
|
|
93
|
+
* Disable colors in output
|
|
94
|
+
*
|
|
95
|
+
* @internal
|
|
96
|
+
*/
|
|
97
|
+
disableColors: Action = ({ logger }: ActionParameters) => {
|
|
98
|
+
logger.disableColors()
|
|
99
|
+
},
|
|
100
|
+
/**
|
|
101
|
+
* Set verbosity to the maximum
|
|
102
|
+
*
|
|
103
|
+
* @internal
|
|
104
|
+
*/
|
|
105
|
+
setVerbose: Action = ({ logger }: ActionParameters) => {
|
|
106
|
+
logger.level = "silly"
|
|
107
|
+
},
|
|
108
|
+
/**
|
|
109
|
+
* Makes the program quiet, eg displaying logs with level >= warning
|
|
110
|
+
*/
|
|
111
|
+
setQuiet: Action = ({ logger }: ActionParameters) => {
|
|
112
|
+
logger.level = "warn"
|
|
113
|
+
},
|
|
114
|
+
/**
|
|
115
|
+
* Makes the program totally silent
|
|
116
|
+
*/
|
|
117
|
+
setSilent: Action = ({ logger }: ActionParameters) => {
|
|
118
|
+
logger.silent = true
|
|
119
|
+
},
|
|
120
|
+
/**
|
|
121
|
+
* Install completion
|
|
122
|
+
*/
|
|
123
|
+
installComp: Action = ({ program }: ActionParameters) => {
|
|
124
|
+
return installCompletion(program)
|
|
125
|
+
},
|
|
126
|
+
/**
|
|
127
|
+
* Uninstall completion
|
|
128
|
+
*/
|
|
129
|
+
uninstallComp: Action = ({ program }: ActionParameters) => {
|
|
130
|
+
return uninstallCompletion(program)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Global options container
|
|
135
|
+
*
|
|
136
|
+
* @internal
|
|
137
|
+
*/
|
|
138
|
+
let globalOptions: undefined | GlobalOptions
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Get the list of registered global flags
|
|
142
|
+
*
|
|
143
|
+
* @internal
|
|
144
|
+
*/
|
|
145
|
+
export function getGlobalOptions(): GlobalOptions {
|
|
146
|
+
if (globalOptions === undefined) {
|
|
147
|
+
globalOptions = setupGlobalOptions()
|
|
148
|
+
}
|
|
149
|
+
return globalOptions
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Set up the global flags
|
|
154
|
+
*
|
|
155
|
+
* @internal
|
|
156
|
+
*/
|
|
157
|
+
function setupGlobalOptions(): GlobalOptions {
|
|
158
|
+
const help = createOption("-h, --help", "Display global help or command-related help."),
|
|
159
|
+
verbose = createOption(
|
|
160
|
+
"-v, --verbose",
|
|
161
|
+
"Verbose mode: will also output debug messages.",
|
|
162
|
+
),
|
|
163
|
+
quiet = createOption(
|
|
164
|
+
"--quiet",
|
|
165
|
+
"Quiet mode - only displays warn and error messages.",
|
|
166
|
+
),
|
|
167
|
+
silent = createOption(
|
|
168
|
+
"--silent",
|
|
169
|
+
"Silent mode: does not output anything, giving no indication of success or failure other than the exit code.",
|
|
170
|
+
),
|
|
171
|
+
version = createOption("-V, --version", "Display version."),
|
|
172
|
+
color = createOption("--no-color", "Disable use of colors in output."),
|
|
173
|
+
installCompOpt = createOption(
|
|
174
|
+
"--install-completion",
|
|
175
|
+
"Install completion for your shell.",
|
|
176
|
+
{ hidden: true },
|
|
177
|
+
),
|
|
178
|
+
uninstallCompOpt = createOption(
|
|
179
|
+
"--uninstall-completion",
|
|
180
|
+
"Uninstall completion for your shell.",
|
|
181
|
+
{ hidden: true },
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
return new Map([
|
|
185
|
+
[help, showHelp],
|
|
186
|
+
[version, showVersion],
|
|
187
|
+
[color, disableColors],
|
|
188
|
+
[verbose, setVerbose],
|
|
189
|
+
[quiet, setQuiet],
|
|
190
|
+
[silent, setSilent],
|
|
191
|
+
[installCompOpt, installComp],
|
|
192
|
+
[uninstallCompOpt, uninstallComp],
|
|
193
|
+
])
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function resetGlobalOptions(): GlobalOptions {
|
|
197
|
+
return (globalOptions = setupGlobalOptions())
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Disable a global option
|
|
202
|
+
*
|
|
203
|
+
* @param name Can be the option short/long name or notation
|
|
204
|
+
*/
|
|
205
|
+
export function disableGlobalOption(name: string): boolean {
|
|
206
|
+
const opts = getGlobalOptions()
|
|
207
|
+
for (const [opt] of opts) {
|
|
208
|
+
if (opt.allNames.includes(name) || opt.allNotations.includes(name)) {
|
|
209
|
+
return opts.delete(opt)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return false
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Add a global option to the program.
|
|
217
|
+
* A global option is available at the program level,
|
|
218
|
+
* and associated with one given {@link Action}.
|
|
219
|
+
*
|
|
220
|
+
* @param a {@link Option} instance, for example created using {@link createOption()}
|
|
221
|
+
*/
|
|
222
|
+
export function addGlobalOption(opt: Option, action?: Action): GlobalOptions {
|
|
223
|
+
return getGlobalOptions().set(opt, action)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Process global options, if any
|
|
228
|
+
* @internal
|
|
229
|
+
*/
|
|
230
|
+
export async function processGlobalOptions(
|
|
231
|
+
parsed: ParserProcessedResult,
|
|
232
|
+
program: Program,
|
|
233
|
+
command?: Command,
|
|
234
|
+
): Promise<boolean> {
|
|
235
|
+
const { options } = parsed
|
|
236
|
+
const actionsParams = { ...parsed, logger, program, command }
|
|
237
|
+
const promises = Object.entries(options).map(([opt]) => {
|
|
238
|
+
const action = findGlobalOptAction(opt)
|
|
239
|
+
if (action) {
|
|
240
|
+
return action(actionsParams)
|
|
241
|
+
}
|
|
242
|
+
return undefined
|
|
243
|
+
})
|
|
244
|
+
const results = await Promise.all(promises)
|
|
245
|
+
return results.some((r) => r === false)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Find a global Option action from the option name (short or long)
|
|
250
|
+
*
|
|
251
|
+
* @param name Short or long name
|
|
252
|
+
* @internal
|
|
253
|
+
*/
|
|
254
|
+
export function findGlobalOptAction(name: string): Action | undefined {
|
|
255
|
+
for (const [opt, action] of getGlobalOptions()) {
|
|
256
|
+
if (opt.allNames.includes(name)) {
|
|
257
|
+
return action
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return undefined
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Find a global Option by it's name (short or long)
|
|
265
|
+
*
|
|
266
|
+
* @param name Short or long name
|
|
267
|
+
* @internal
|
|
268
|
+
*/
|
|
269
|
+
export function findGlobalOption(name: string): Option | undefined {
|
|
270
|
+
for (const [opt] of getGlobalOptions()) {
|
|
271
|
+
if (opt.allNames.includes(name)) {
|
|
272
|
+
return opt
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return undefined
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function isOptionObject(obj: unknown): obj is Option {
|
|
279
|
+
return typeof obj == "object" && obj !== null && (obj as Option).kind == "option"
|
|
280
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Command } from "../command"
|
|
7
|
+
import map from "lodash/map"
|
|
8
|
+
import zipObject from "lodash/zipObject"
|
|
9
|
+
import invert from "lodash/invert"
|
|
10
|
+
import pickBy from "lodash/pickBy"
|
|
11
|
+
|
|
12
|
+
export function getOptsMapping(cmd: Command): Record<string, string> {
|
|
13
|
+
const names = map(cmd.options, "name")
|
|
14
|
+
const aliases = map(cmd.options, (o) => o.shortName || o.longName)
|
|
15
|
+
const result = zipObject(names, aliases)
|
|
16
|
+
return pickBy({ ...result, ...invert(result) }) as Record<string, string>
|
|
17
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import camelCase from "lodash/camelCase"
|
|
7
|
+
import kebabCase from "lodash/kebabCase"
|
|
8
|
+
import { OptionSynopsisSyntaxError } from "../error"
|
|
9
|
+
import { OptionValueType } from "../types"
|
|
10
|
+
import type { OptionSynopsis, ParserTypes } from "../types"
|
|
11
|
+
|
|
12
|
+
const REG_SHORT_OPT = /^-[a-z]$/i
|
|
13
|
+
const REG_LONG_OPT = /^--[a-z]{2,}/i
|
|
14
|
+
const REG_OPT = /^(-[a-zA-Z]|--\D{1}[\w-]+)/
|
|
15
|
+
|
|
16
|
+
function isShortOpt(flag: string): boolean {
|
|
17
|
+
return REG_SHORT_OPT.test(flag)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isLongOpt(flag: string): boolean {
|
|
21
|
+
return REG_LONG_OPT.test(flag)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Specific version of camelCase which does not lowercase short flags
|
|
26
|
+
*
|
|
27
|
+
* @param name Flag short or long name
|
|
28
|
+
*/
|
|
29
|
+
function camelCaseOpt(name: string): string {
|
|
30
|
+
return name.length === 1 ? name : camelCase(name)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function getCleanNameFromNotation(str: string, camelCased = true): string {
|
|
34
|
+
str = str
|
|
35
|
+
.replace(/([[\]<>]+)/g, "")
|
|
36
|
+
.replace("...", "")
|
|
37
|
+
.replace(/^no-/, "")
|
|
38
|
+
return camelCased ? camelCaseOpt(str) : str
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getDashedOpt(name: string): string {
|
|
42
|
+
const l = Math.min(name.length, 2)
|
|
43
|
+
return "-".repeat(l) + kebabCase(name)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function isNumeric(n: string): boolean {
|
|
47
|
+
return !isNaN(parseFloat(n)) && isFinite(Number(n))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isOptionStr(str?: string): str is string {
|
|
51
|
+
return str !== undefined && str !== "--" && REG_OPT.test(str)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function isConcatenatedOpt(str: string): string[] | false {
|
|
55
|
+
if (str.match(/^-([a-z]{2,})/i)) {
|
|
56
|
+
return str.substr(1).split("")
|
|
57
|
+
}
|
|
58
|
+
return false
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function isNegativeOpt(opt: string): boolean {
|
|
62
|
+
return opt.substr(0, 5) === "--no-"
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function isOptArray(flag: ParserTypes | ParserTypes[]): flag is ParserTypes[] {
|
|
66
|
+
return Array.isArray(flag)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function formatOptName(name: string): string {
|
|
70
|
+
return camelCaseOpt(name.replace(/^--?(no-)?/, ""))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Parse a option synopsis
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* parseSynopsis("-f, --file <path>")
|
|
78
|
+
* // Returns...
|
|
79
|
+
* {
|
|
80
|
+
* longName: 'file',
|
|
81
|
+
* longNotation: '--file',
|
|
82
|
+
* shortNotation: '-f',
|
|
83
|
+
* shortName: 'f'
|
|
84
|
+
* valueType: 0, // 0 = optional, 1 = required, 2 = no value
|
|
85
|
+
* variadic: false
|
|
86
|
+
* name: 'file'
|
|
87
|
+
* notation: '--file' // either the long or short notation
|
|
88
|
+
* }
|
|
89
|
+
*
|
|
90
|
+
* @param synopsis
|
|
91
|
+
* @ignore
|
|
92
|
+
*/
|
|
93
|
+
export function parseOptionSynopsis(synopsis: string): OptionSynopsis {
|
|
94
|
+
// synopsis = synopsis.trim()
|
|
95
|
+
const analysis: OptionSynopsis = {
|
|
96
|
+
variadic: false,
|
|
97
|
+
valueType: OptionValueType.None,
|
|
98
|
+
valueRequired: false,
|
|
99
|
+
allNames: [],
|
|
100
|
+
allNotations: [],
|
|
101
|
+
name: "",
|
|
102
|
+
notation: "",
|
|
103
|
+
synopsis,
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const infos: Partial<OptionSynopsis> = synopsis
|
|
107
|
+
.split(/[\s\t,]+/)
|
|
108
|
+
.reduce((acc, value) => {
|
|
109
|
+
if (isLongOpt(value)) {
|
|
110
|
+
acc.longNotation = value
|
|
111
|
+
acc.longName = getCleanNameFromNotation(value.substring(2))
|
|
112
|
+
acc.allNames.push(acc.longName)
|
|
113
|
+
acc.allNotations.push(value)
|
|
114
|
+
} else if (isShortOpt(value)) {
|
|
115
|
+
acc.shortNotation = value
|
|
116
|
+
acc.shortName = value.substring(1)
|
|
117
|
+
acc.allNames.push(acc.shortName)
|
|
118
|
+
acc.allNotations.push(value)
|
|
119
|
+
} else if (value.substring(0, 1) === "[") {
|
|
120
|
+
acc.valueType = OptionValueType.Optional
|
|
121
|
+
acc.valueRequired = false
|
|
122
|
+
acc.variadic = value.substr(-4, 3) === "..."
|
|
123
|
+
} else if (value.substring(0, 1) === "<") {
|
|
124
|
+
acc.valueType = OptionValueType.Required
|
|
125
|
+
acc.valueRequired = true
|
|
126
|
+
acc.variadic = value.substr(-4, 3) === "..."
|
|
127
|
+
}
|
|
128
|
+
return acc
|
|
129
|
+
}, analysis)
|
|
130
|
+
|
|
131
|
+
if (infos.longName === undefined && infos.shortName === undefined) {
|
|
132
|
+
throw new OptionSynopsisSyntaxError(synopsis)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
infos.name = infos.longName || (infos.shortName as string)
|
|
136
|
+
infos.notation = infos.longNotation || (infos.shortNotation as string)
|
|
137
|
+
|
|
138
|
+
const fullSynopsis = { ...infos } as OptionSynopsis
|
|
139
|
+
|
|
140
|
+
return fullSynopsis
|
|
141
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import reduce from "lodash/reduce"
|
|
7
|
+
import { findOption } from "./find"
|
|
8
|
+
import { MissingFlagError, UnknownOptionError, CommonError } from "../error"
|
|
9
|
+
import { findGlobalOption } from "."
|
|
10
|
+
import { validate } from "../validator/validate"
|
|
11
|
+
|
|
12
|
+
import type { ParsedOption, ParsedOptions, Promisable, Option } from "../types"
|
|
13
|
+
import type { Command } from "../command"
|
|
14
|
+
|
|
15
|
+
function validateOption(opt: Option, value: ParsedOption): ReturnType<typeof validate> {
|
|
16
|
+
return opt.validator ? validate(value, opt.validator, opt) : value
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function checkRequiredOpts(cmd: Command, opts: ParsedOptions) {
|
|
20
|
+
return cmd.options.reduce((acc, opt) => {
|
|
21
|
+
if (opts[opt.name] === undefined && opt.required) {
|
|
22
|
+
acc.push(new MissingFlagError(opt, cmd))
|
|
23
|
+
}
|
|
24
|
+
return acc
|
|
25
|
+
}, [] as CommonError[])
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function applyDefaults(cmd: Command, opts: ParsedOptions): ParsedOptions {
|
|
29
|
+
return cmd.options.reduce((acc, opt) => {
|
|
30
|
+
if (acc[opt.name] === undefined && opt.default !== undefined) {
|
|
31
|
+
acc[opt.name] = opt.default
|
|
32
|
+
}
|
|
33
|
+
return acc
|
|
34
|
+
}, opts)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type OptionsPromises = Record<string, Promisable<ParsedOption>>
|
|
38
|
+
|
|
39
|
+
interface OptionsValidationResult {
|
|
40
|
+
options: ParsedOptions
|
|
41
|
+
errors: CommonError[]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function validateOptions(
|
|
45
|
+
cmd: Command,
|
|
46
|
+
options: ParsedOptions,
|
|
47
|
+
): Promise<OptionsValidationResult> {
|
|
48
|
+
options = applyDefaults(cmd, options)
|
|
49
|
+
const errors: CommonError[] = []
|
|
50
|
+
const validations = reduce(
|
|
51
|
+
options,
|
|
52
|
+
(...args) => {
|
|
53
|
+
const [acc, value, name] = args
|
|
54
|
+
const opt = findGlobalOption(name) || findOption(cmd, name)
|
|
55
|
+
try {
|
|
56
|
+
if (opt) {
|
|
57
|
+
acc[name] = validateOption(opt, value)
|
|
58
|
+
} else if (cmd.strictOptions) {
|
|
59
|
+
throw new UnknownOptionError(name, cmd)
|
|
60
|
+
}
|
|
61
|
+
} catch (e) {
|
|
62
|
+
errors.push(e)
|
|
63
|
+
}
|
|
64
|
+
return acc
|
|
65
|
+
},
|
|
66
|
+
{} as OptionsPromises,
|
|
67
|
+
)
|
|
68
|
+
const result = await reduce(
|
|
69
|
+
validations,
|
|
70
|
+
async (prevPromise, value, key): Promise<ParsedOptions> => {
|
|
71
|
+
const collection = await prevPromise
|
|
72
|
+
collection[key] = await value
|
|
73
|
+
return collection
|
|
74
|
+
},
|
|
75
|
+
Promise.resolve({}) as Promise<ParsedOptions>,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
errors.push(...checkRequiredOpts(cmd, result))
|
|
79
|
+
return { options: result, errors }
|
|
80
|
+
}
|