@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,144 @@
|
|
|
1
|
+
import { getHelp, tpl, getContext, registerTemplate } from ".."
|
|
2
|
+
import { Program } from "../../program"
|
|
3
|
+
import { createCommand } from "../../command"
|
|
4
|
+
import { findCommand } from "../../command/find"
|
|
5
|
+
import strip from "strip-ansi"
|
|
6
|
+
import { expect, it, describe, beforeEach } from "vitest"
|
|
7
|
+
|
|
8
|
+
describe("help", () => {
|
|
9
|
+
let prog = new Program()
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
prog = new Program()
|
|
13
|
+
prog.name("test-prog")
|
|
14
|
+
prog.bin("test-prog")
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
describe("tpl()", () => {
|
|
18
|
+
it("should compile template with given context", () => {
|
|
19
|
+
const cmd = createCommand(prog, "test", "test command")
|
|
20
|
+
const ctx = getContext(prog, cmd)
|
|
21
|
+
return expect(tpl("header", ctx)).resolves.toMatchSnapshot()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it("should compile a custom template with given context", async () => {
|
|
25
|
+
const cmd = createCommand(prog, "test", "test command")
|
|
26
|
+
const ctx = getContext(prog, cmd)
|
|
27
|
+
registerTemplate("mine", () => "template-contents")
|
|
28
|
+
expect(await tpl("mine", ctx)).toBe("template-contents")
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it("should fail if template does not exist", () => {
|
|
32
|
+
const cmd = createCommand(prog, "test", "test command")
|
|
33
|
+
const ctx = getContext(prog, cmd)
|
|
34
|
+
return expect(tpl("unknown", ctx)).rejects.toBeInstanceOf(Error)
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
describe("getHelp()", () => {
|
|
39
|
+
it("should display help for a basic program", async () => {
|
|
40
|
+
return expect(strip(await getHelp(prog))).toMatchSnapshot()
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it("should display help for a program-command", async () => {
|
|
44
|
+
prog
|
|
45
|
+
.argument("<foo>", "Mandarory foo arg")
|
|
46
|
+
.argument("[other]", "Other args")
|
|
47
|
+
.option("-f, --file <file>", "Output file")
|
|
48
|
+
|
|
49
|
+
return expect(strip(await getHelp(prog))).toMatchSnapshot()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it("should display help for a program having at least one command (with args & options)", async () => {
|
|
53
|
+
prog
|
|
54
|
+
.command("test-command", "Test command")
|
|
55
|
+
.argument("<foo>", "Mandarory foo arg")
|
|
56
|
+
.argument("[other]", "Other args")
|
|
57
|
+
.option("-f, --file <file>", "Output file")
|
|
58
|
+
return expect(strip(await getHelp(prog))).toMatchSnapshot()
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it("should display help for a program having at least one command (with args only)", async () => {
|
|
62
|
+
prog
|
|
63
|
+
.command("test-command", "Test command")
|
|
64
|
+
.argument("<foo>", "Mandarory foo arg")
|
|
65
|
+
.argument("[other]", "Other args")
|
|
66
|
+
return expect(strip(await getHelp(prog))).toMatchSnapshot()
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it("should display help for a program having at least one command (with options only)", async () => {
|
|
70
|
+
prog
|
|
71
|
+
.command("test-command", "Test command")
|
|
72
|
+
.option("-f, --file <file>", "Output file")
|
|
73
|
+
return expect(strip(await getHelp(prog))).toMatchSnapshot()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it("should handle required options", async () => {
|
|
77
|
+
prog
|
|
78
|
+
.command("test-command", "Test command")
|
|
79
|
+
.option("-f, --file <file>", "Output file", { required: true })
|
|
80
|
+
const cmd = await findCommand(prog, ["test-command"])
|
|
81
|
+
|
|
82
|
+
expect(strip(await getHelp(prog, cmd))).toContain("required")
|
|
83
|
+
return expect(strip(await getHelp(prog, cmd))).toMatchSnapshot()
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it("should handle type hints", async () => {
|
|
87
|
+
prog
|
|
88
|
+
.command("test-command", "Test command")
|
|
89
|
+
.argument("<foo>", "Desc", { validator: prog.NUMBER })
|
|
90
|
+
.option("-f, --file <file>", "Output file", {
|
|
91
|
+
required: true,
|
|
92
|
+
validator: prog.NUMBER,
|
|
93
|
+
})
|
|
94
|
+
const cmd = await findCommand(prog, ["test-command"])
|
|
95
|
+
|
|
96
|
+
return expect(strip(await getHelp(prog, cmd))).toMatchSnapshot()
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it("should work with a program without a specified name", async () => {
|
|
100
|
+
prog = new Program()
|
|
101
|
+
prog.bin("test-prog")
|
|
102
|
+
prog
|
|
103
|
+
.command("test-command", "Test command")
|
|
104
|
+
.option("-f, --file <file>", "Output file")
|
|
105
|
+
return expect(strip(await getHelp(prog))).toMatchSnapshot()
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it("should work with a program without a version", async () => {
|
|
109
|
+
prog = new Program()
|
|
110
|
+
prog.bin("test-prog")
|
|
111
|
+
prog.version("")
|
|
112
|
+
prog
|
|
113
|
+
.command("test-command", "Test command")
|
|
114
|
+
.option("-f, --file <file>", "Output file")
|
|
115
|
+
expect(strip(await getHelp(prog))).toMatchSnapshot()
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it("should display program description", async () => {
|
|
119
|
+
prog.description("Description test")
|
|
120
|
+
prog
|
|
121
|
+
.command("test-command", "Test command")
|
|
122
|
+
.option("-f, --file <file>", "Output file")
|
|
123
|
+
expect(strip(await getHelp(prog))).toMatchSnapshot()
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it("should display customized program help", async () => {
|
|
127
|
+
prog.description("Description test")
|
|
128
|
+
prog
|
|
129
|
+
.help("My custom help")
|
|
130
|
+
.command("test-command", "Test command")
|
|
131
|
+
.option("-f, --file <file>", "Output file")
|
|
132
|
+
expect(strip(await getHelp(prog))).toMatchSnapshot()
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it("should display customized program help on multiple lines", async () => {
|
|
136
|
+
prog.description("Description test")
|
|
137
|
+
prog
|
|
138
|
+
.command("test-command", "Test command")
|
|
139
|
+
.option("-f, --file <file>", "Output file")
|
|
140
|
+
.help("My custom help\nAnother line\nOne last line")
|
|
141
|
+
expect(strip(await getHelp(prog))).toMatchSnapshot()
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
})
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getDefaultValueHint } from "../utils"
|
|
2
|
+
import { createArgument } from "../../argument"
|
|
3
|
+
import { CaporalValidator } from "../../validator/caporal"
|
|
4
|
+
import { expect, it, describe } from "vitest"
|
|
5
|
+
|
|
6
|
+
describe("help/utils", () => {
|
|
7
|
+
it("getDefaultValueHint() should return the correct value hint", () => {
|
|
8
|
+
const arg = createArgument("<arg>", "My arg", {
|
|
9
|
+
validator: CaporalValidator.BOOLEAN,
|
|
10
|
+
default: true,
|
|
11
|
+
})
|
|
12
|
+
expect(getDefaultValueHint(arg)).toBe("default: true")
|
|
13
|
+
})
|
|
14
|
+
})
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @module caporal/help
|
|
4
|
+
*/
|
|
5
|
+
import { Command } from "../command"
|
|
6
|
+
import { Program } from "../program"
|
|
7
|
+
import replace from "lodash/replace"
|
|
8
|
+
import chalk from "chalk"
|
|
9
|
+
import { buildTable } from "./utils"
|
|
10
|
+
import { colorize } from "../utils/colorize"
|
|
11
|
+
import * as allTemplates from "./templates"
|
|
12
|
+
import { getGlobalOptions } from "../option"
|
|
13
|
+
import { CustomizedHelpMap, CustomizedHelpOpts, TemplateContext, Template } from "./types"
|
|
14
|
+
|
|
15
|
+
const templates = new Map(Object.entries(allTemplates))
|
|
16
|
+
const customHelpMap: CustomizedHelpMap = new Map()
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Customize the help
|
|
20
|
+
*
|
|
21
|
+
* @param obj
|
|
22
|
+
* @param text
|
|
23
|
+
* @param options
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
export function customizeHelp(
|
|
27
|
+
obj: Command | Program,
|
|
28
|
+
text: string,
|
|
29
|
+
options: Partial<CustomizedHelpOpts>,
|
|
30
|
+
): void {
|
|
31
|
+
const opts: CustomizedHelpOpts = {
|
|
32
|
+
sectionName: "",
|
|
33
|
+
colorize: true,
|
|
34
|
+
...options,
|
|
35
|
+
}
|
|
36
|
+
const data = customHelpMap.get(obj) || []
|
|
37
|
+
data.push({ text, options: opts })
|
|
38
|
+
customHelpMap.set(obj, data)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Register a new help template
|
|
43
|
+
*
|
|
44
|
+
* @param name Template name
|
|
45
|
+
* @param template Template function
|
|
46
|
+
*
|
|
47
|
+
*/
|
|
48
|
+
export function registerTemplate(
|
|
49
|
+
name: string,
|
|
50
|
+
template: Template,
|
|
51
|
+
): Map<string, Template> {
|
|
52
|
+
return templates.set(name, template)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Helper to be used to call templates from within templates
|
|
57
|
+
*
|
|
58
|
+
* @param name Template name
|
|
59
|
+
* @param ctx Execution context
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
export async function tpl(name: string, ctx: TemplateContext): Promise<string> {
|
|
63
|
+
const template = templates.get(name)
|
|
64
|
+
if (!template) {
|
|
65
|
+
throw Error(`Caporal setup error: Unknown help template '${name}'`)
|
|
66
|
+
}
|
|
67
|
+
return template(ctx)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @internal
|
|
72
|
+
* @param program
|
|
73
|
+
* @param command
|
|
74
|
+
*/
|
|
75
|
+
export function getContext(program: Program, command?: Command): TemplateContext {
|
|
76
|
+
const spaces = " ".repeat(2)
|
|
77
|
+
const ctx: TemplateContext = {
|
|
78
|
+
prog: program,
|
|
79
|
+
cmd: command,
|
|
80
|
+
chalk: chalk,
|
|
81
|
+
colorize: colorize,
|
|
82
|
+
customHelp: customHelpMap,
|
|
83
|
+
tpl,
|
|
84
|
+
globalOptions: getGlobalOptions(),
|
|
85
|
+
table: buildTable,
|
|
86
|
+
spaces,
|
|
87
|
+
indent(str: string, sp = spaces) {
|
|
88
|
+
return sp + replace(str.trim(), /(\r\n|\r|\n)/g, "\n" + sp)
|
|
89
|
+
},
|
|
90
|
+
eol: "\n",
|
|
91
|
+
eol2: "\n\n",
|
|
92
|
+
eol3: "\n\n\n",
|
|
93
|
+
}
|
|
94
|
+
return ctx
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Return the help text
|
|
99
|
+
*
|
|
100
|
+
* @param program Program instance
|
|
101
|
+
* @param command Command instance, if any
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
export async function getHelp(program: Program, command?: Command): Promise<string> {
|
|
105
|
+
const ctx = getContext(program, command)
|
|
106
|
+
return [await tpl("header", ctx), await tpl("usage", ctx)].join("")
|
|
107
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
6
|
+
import type { TemplateContext, Template } from "../types"
|
|
7
|
+
import { getOptionsTable, getArgumentsTable } from "../utils"
|
|
8
|
+
import sortBy from "lodash/sortBy"
|
|
9
|
+
|
|
10
|
+
export const command: Template = async (ctx: TemplateContext) => {
|
|
11
|
+
const { cmd, globalOptions: globalFlags, eol, eol3, colorize, tpl } = ctx
|
|
12
|
+
|
|
13
|
+
const options = sortBy(cmd!.options, "name"),
|
|
14
|
+
globalOptions = Array.from(globalFlags.keys())
|
|
15
|
+
|
|
16
|
+
const help =
|
|
17
|
+
cmd!.synopsis +
|
|
18
|
+
eol3 +
|
|
19
|
+
(await tpl("custom", ctx)) +
|
|
20
|
+
getArgumentsTable(cmd!.args, ctx) +
|
|
21
|
+
eol +
|
|
22
|
+
getOptionsTable(options, ctx) +
|
|
23
|
+
eol +
|
|
24
|
+
getOptionsTable(globalOptions, ctx, "GLOBAL OPTIONS")
|
|
25
|
+
|
|
26
|
+
return colorize(help)
|
|
27
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
import type { TemplateContext, Template } from "../types"
|
|
6
|
+
|
|
7
|
+
export const custom: Template = (ctx: TemplateContext) => {
|
|
8
|
+
const { prog, cmd, eol2, eol3, chalk, colorize, customHelp, indent } = ctx
|
|
9
|
+
const data = customHelp.get(cmd || prog)
|
|
10
|
+
if (data) {
|
|
11
|
+
const txt = data
|
|
12
|
+
.map(({ text, options }) => {
|
|
13
|
+
let str = ""
|
|
14
|
+
if (options.sectionName) {
|
|
15
|
+
str += chalk.bold(options.sectionName) + eol2
|
|
16
|
+
}
|
|
17
|
+
const subtxt = options.colorize ? colorize(text) : text
|
|
18
|
+
str += options.sectionName ? indent(subtxt) : subtxt
|
|
19
|
+
return str + eol3
|
|
20
|
+
})
|
|
21
|
+
.join("")
|
|
22
|
+
return indent(txt) + eol3
|
|
23
|
+
}
|
|
24
|
+
return ""
|
|
25
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
import type { TemplateContext, Template } from "../types"
|
|
6
|
+
|
|
7
|
+
export const header: Template = (ctx: TemplateContext) => {
|
|
8
|
+
const { prog, chalk: c, spaces, eol, eol2 } = ctx
|
|
9
|
+
const version = process.env?.NODE_ENV === "test" ? "" : prog.getVersion()
|
|
10
|
+
return (
|
|
11
|
+
eol +
|
|
12
|
+
spaces +
|
|
13
|
+
(prog.getName() || prog.getBin()) +
|
|
14
|
+
" " +
|
|
15
|
+
(version || "") +
|
|
16
|
+
(prog.getDescription() ? " \u2014 " + c.dim(prog.getDescription()) : "") +
|
|
17
|
+
eol2
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
import type { TemplateContext, Template } from "../types"
|
|
6
|
+
import { getOptionsTable, getCommandsTable } from "../utils"
|
|
7
|
+
|
|
8
|
+
export const program: Template = async (ctx: TemplateContext) => {
|
|
9
|
+
const { prog, globalOptions, eol, eol3, colorize, tpl } = ctx
|
|
10
|
+
const commands = await prog.getAllCommands()
|
|
11
|
+
const options = Array.from(globalOptions.keys())
|
|
12
|
+
const help =
|
|
13
|
+
(await prog.getSynopsis()) +
|
|
14
|
+
eol3 +
|
|
15
|
+
(await tpl("custom", ctx)) +
|
|
16
|
+
getCommandsTable(commands, ctx) +
|
|
17
|
+
eol +
|
|
18
|
+
getOptionsTable(options, ctx, "GLOBAL OPTIONS")
|
|
19
|
+
|
|
20
|
+
return colorize(help)
|
|
21
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
import type { TemplateContext, Template } from "../types"
|
|
6
|
+
|
|
7
|
+
export const usage: Template = async (ctx: TemplateContext) => {
|
|
8
|
+
const { tpl, prog, chalk: c, spaces, eol } = ctx
|
|
9
|
+
let { cmd } = ctx
|
|
10
|
+
|
|
11
|
+
// if help is asked without a `cmd` and that no command exists
|
|
12
|
+
// within the program, override `cmd` with the program-command
|
|
13
|
+
if (!cmd && !(await prog.hasCommands())) {
|
|
14
|
+
ctx.cmd = cmd = prog.progCommand
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// usage
|
|
18
|
+
const usage = `${spaces + c.bold("USAGE")} ${cmd?.name ? "— " + c.dim(cmd.name) : ""}
|
|
19
|
+
${eol + spaces + spaces + c.dim("\u25B8")} `
|
|
20
|
+
|
|
21
|
+
const next = cmd ? await tpl("command", ctx) : await tpl("program", ctx)
|
|
22
|
+
|
|
23
|
+
return usage + next
|
|
24
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @module caporal/types
|
|
4
|
+
*/
|
|
5
|
+
import { Command } from "../command"
|
|
6
|
+
import { Program } from "../program"
|
|
7
|
+
import chalk from "chalk"
|
|
8
|
+
import { colorize } from "../utils/colorize"
|
|
9
|
+
import { buildTable } from "./utils"
|
|
10
|
+
import type { GlobalOptions } from "../types"
|
|
11
|
+
|
|
12
|
+
export interface CustomizedHelpOpts {
|
|
13
|
+
/**
|
|
14
|
+
* Name of the section to be added in help.
|
|
15
|
+
*/
|
|
16
|
+
sectionName: string
|
|
17
|
+
/**
|
|
18
|
+
* Enable or disable the automatic coloration of text.
|
|
19
|
+
*/
|
|
20
|
+
colorize: boolean
|
|
21
|
+
}
|
|
22
|
+
export interface CustomizedHelp {
|
|
23
|
+
/**
|
|
24
|
+
* Various display options.
|
|
25
|
+
*/
|
|
26
|
+
options: CustomizedHelpOpts
|
|
27
|
+
/**
|
|
28
|
+
* Help text. Padding of the text is automatically handled for you.
|
|
29
|
+
*/
|
|
30
|
+
text: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type CustomizedHelpMap = Map<Command | Program, CustomizedHelp[]>
|
|
34
|
+
|
|
35
|
+
export interface Template {
|
|
36
|
+
(ctx: TemplateContext): Promise<string> | string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface TemplateFunction {
|
|
40
|
+
(name: string, ctx: TemplateContext): Promise<string> | string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface TemplateContext {
|
|
44
|
+
prog: Program
|
|
45
|
+
cmd?: Command
|
|
46
|
+
customHelp: CustomizedHelpMap
|
|
47
|
+
globalOptions: GlobalOptions
|
|
48
|
+
chalk: typeof chalk
|
|
49
|
+
colorize: typeof colorize
|
|
50
|
+
tpl: TemplateFunction
|
|
51
|
+
table: typeof buildTable
|
|
52
|
+
indent: (str: string) => string
|
|
53
|
+
eol: string
|
|
54
|
+
eol2: string
|
|
55
|
+
eol3: string
|
|
56
|
+
spaces: string
|
|
57
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
import { table, getBorderCharacters } from "table"
|
|
6
|
+
import filter from "lodash/filter"
|
|
7
|
+
import type { TemplateContext } from "./types"
|
|
8
|
+
import type { Option, Argument } from "../types"
|
|
9
|
+
import type { Command } from "../command"
|
|
10
|
+
|
|
11
|
+
export function buildTable(data: string[][], options = {}): string {
|
|
12
|
+
return table(data, {
|
|
13
|
+
border: getBorderCharacters(`void`),
|
|
14
|
+
columnDefault: {
|
|
15
|
+
paddingLeft: 0,
|
|
16
|
+
paddingRight: 2,
|
|
17
|
+
},
|
|
18
|
+
columns: {
|
|
19
|
+
0: {
|
|
20
|
+
paddingLeft: 4,
|
|
21
|
+
width: 35,
|
|
22
|
+
},
|
|
23
|
+
1: {
|
|
24
|
+
width: 55,
|
|
25
|
+
wrapWord: true,
|
|
26
|
+
paddingRight: 0,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
drawHorizontalLine: () => {
|
|
30
|
+
return false
|
|
31
|
+
},
|
|
32
|
+
...options,
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function getDefaultValueHint(obj: Argument | Option): string | undefined {
|
|
37
|
+
return obj.default !== undefined &&
|
|
38
|
+
!("boolean" in obj && obj.boolean && obj.default === false)
|
|
39
|
+
? "default: " + JSON.stringify(obj.default)
|
|
40
|
+
: undefined
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function getOptionSynopsisHelp(
|
|
44
|
+
opt: Option,
|
|
45
|
+
{ eol: crlf, chalk: c }: TemplateContext,
|
|
46
|
+
): string {
|
|
47
|
+
return (
|
|
48
|
+
opt.synopsis +
|
|
49
|
+
(opt.required && opt.default === undefined ? crlf + c.dim("required") : "")
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function getOptionsTable(
|
|
54
|
+
options: Option[],
|
|
55
|
+
ctx: TemplateContext,
|
|
56
|
+
title = "OPTIONS",
|
|
57
|
+
): string {
|
|
58
|
+
options = filter(options, "visible")
|
|
59
|
+
if (!options.length) {
|
|
60
|
+
return ""
|
|
61
|
+
}
|
|
62
|
+
const { chalk: c, eol: crlf, table, spaces } = ctx
|
|
63
|
+
const help = spaces + c.bold(title) + crlf + crlf
|
|
64
|
+
const rows = options.map((opt) => {
|
|
65
|
+
const def = getDefaultValueHint(opt)
|
|
66
|
+
const more = [opt.typeHint, def].filter((d) => d).join(", ")
|
|
67
|
+
const syno = getOptionSynopsisHelp(opt, ctx)
|
|
68
|
+
const desc = opt.description + (more.length ? crlf + c.dim(more) : "")
|
|
69
|
+
return [syno, desc]
|
|
70
|
+
})
|
|
71
|
+
return help + table(rows)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function getArgumentsTable(
|
|
75
|
+
args: Argument[],
|
|
76
|
+
ctx: TemplateContext,
|
|
77
|
+
title = "ARGUMENTS",
|
|
78
|
+
): string {
|
|
79
|
+
if (!args.length) {
|
|
80
|
+
return ""
|
|
81
|
+
}
|
|
82
|
+
const { chalk: c, eol, eol2, table, spaces } = ctx
|
|
83
|
+
const help = spaces + c.bold(title) + eol2
|
|
84
|
+
const rows = args.map((a) => {
|
|
85
|
+
const def = getDefaultValueHint(a)
|
|
86
|
+
const more = [a.typeHint, def].filter((d) => d).join(", ")
|
|
87
|
+
const desc = a.description + (more.length ? eol + c.dim(more) : "")
|
|
88
|
+
return [a.synopsis, desc]
|
|
89
|
+
})
|
|
90
|
+
return help + table(rows)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function getCommandsTable(
|
|
94
|
+
commands: Command[],
|
|
95
|
+
ctx: TemplateContext,
|
|
96
|
+
title = "COMMANDS",
|
|
97
|
+
): string {
|
|
98
|
+
const { chalk, prog, eol2, table, spaces } = ctx
|
|
99
|
+
const cmdHint = `Type '${prog.getBin()} help <command>' to get some help about a command`
|
|
100
|
+
const help =
|
|
101
|
+
spaces + chalk.bold(title) + ` ${chalk.dim("\u2014")} ` + chalk.dim(cmdHint) + eol2
|
|
102
|
+
const rows = commands
|
|
103
|
+
.filter((c) => c.visible)
|
|
104
|
+
.map((cmd) => {
|
|
105
|
+
return [chalk.hex("#ff9900")(cmd.name), cmd.description || ""]
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
return help + table(rows)
|
|
109
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main Caporal module.
|
|
3
|
+
*
|
|
4
|
+
* ## program
|
|
5
|
+
*
|
|
6
|
+
* This represents your program. You don't have to instanciate the {@link Program} class,
|
|
7
|
+
* it's already done for you.
|
|
8
|
+
*
|
|
9
|
+
* **Usage**
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* // The Program instance generated for you
|
|
13
|
+
* import program from "@caporal/core"
|
|
14
|
+
*
|
|
15
|
+
* program
|
|
16
|
+
* .command(...)
|
|
17
|
+
* .action(...)
|
|
18
|
+
* [...]
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
*
|
|
22
|
+
* ## parseArgv()
|
|
23
|
+
*
|
|
24
|
+
* This is the command line parser internaly used by Caporal.
|
|
25
|
+
*
|
|
26
|
+
* ::: tip Advanced usage
|
|
27
|
+
* Usually, **you won't need to use the parser** directly, but if you
|
|
28
|
+
* just want to parse some args without all capabilities brought
|
|
29
|
+
* by Caporal, feel free to play with it.
|
|
30
|
+
* :::
|
|
31
|
+
*
|
|
32
|
+
* **Usage**
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { parseArgv } from "@caporal/core"
|
|
36
|
+
*
|
|
37
|
+
* const {args, options} = parseArgv({
|
|
38
|
+
* // ... options
|
|
39
|
+
* })
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* Checkout `parseArgv()` [documentation here](/api/modules/parser.md).
|
|
43
|
+
*
|
|
44
|
+
*
|
|
45
|
+
* ## chalk
|
|
46
|
+
*
|
|
47
|
+
* `chalk` npm module re-export
|
|
48
|
+
*
|
|
49
|
+
* **Usage**
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { program, chalk } from "caporal"
|
|
53
|
+
*
|
|
54
|
+
* program
|
|
55
|
+
* .command('pay')
|
|
56
|
+
* .argument('<amount>', 'Amount to pay', Validator.NUMBER)
|
|
57
|
+
* .action(({logger, args}) => {
|
|
58
|
+
* logger.info("You paid $%s", chalk.red(args.amount))
|
|
59
|
+
* })
|
|
60
|
+
* [...]
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
*
|
|
64
|
+
* @packageDocumentation
|
|
65
|
+
* @module @villedemontreal/caporal
|
|
66
|
+
*/
|
|
67
|
+
import { Program } from "./program"
|
|
68
|
+
export { Command } from "./command"
|
|
69
|
+
export * from "./types"
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @ignore
|
|
73
|
+
*/
|
|
74
|
+
export { default as chalk } from "chalk"
|
|
75
|
+
/**
|
|
76
|
+
* @ignore
|
|
77
|
+
*/
|
|
78
|
+
export { parseArgv, parseLine } from "./parser"
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @ignore
|
|
82
|
+
*/
|
|
83
|
+
export const program = new Program()
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @ignore
|
|
87
|
+
*/
|
|
88
|
+
export default program
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @ignore
|
|
92
|
+
*/
|
|
93
|
+
export { Program }
|