@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,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import reduce from "lodash/reduce"
|
|
7
|
+
import {
|
|
8
|
+
TooManyArgumentsError,
|
|
9
|
+
MissingArgumentError,
|
|
10
|
+
BaseError,
|
|
11
|
+
CommonError,
|
|
12
|
+
} from "../error"
|
|
13
|
+
import type {
|
|
14
|
+
Argument,
|
|
15
|
+
ArgumentsRange,
|
|
16
|
+
ParsedArguments,
|
|
17
|
+
ParsedArgumentsObject,
|
|
18
|
+
ParsedArgument,
|
|
19
|
+
Promisable,
|
|
20
|
+
} from "../types"
|
|
21
|
+
|
|
22
|
+
import type { Command } from "../command"
|
|
23
|
+
import { validate } from "../validator/validate"
|
|
24
|
+
import { findArgument } from "./find"
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get the number of required argument for a given command
|
|
28
|
+
*
|
|
29
|
+
* @param cmd
|
|
30
|
+
*/
|
|
31
|
+
export function getRequiredArgsCount(cmd: Command): number {
|
|
32
|
+
return cmd.args.filter((a) => a.required).length
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function getArgsObjectFromArray(
|
|
36
|
+
cmd: Command,
|
|
37
|
+
args: ParsedArguments,
|
|
38
|
+
): ParsedArgumentsObject {
|
|
39
|
+
const result: ParsedArgumentsObject = {}
|
|
40
|
+
return cmd.args.reduce((acc, arg, index) => {
|
|
41
|
+
if (args[index] !== undefined) {
|
|
42
|
+
acc[arg.name] = args[index]
|
|
43
|
+
} else if (arg.default !== undefined) {
|
|
44
|
+
acc[arg.name] = arg.default
|
|
45
|
+
}
|
|
46
|
+
return acc
|
|
47
|
+
}, result)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Check if the given command has at leat one variadic argument
|
|
52
|
+
*
|
|
53
|
+
* @param cmd
|
|
54
|
+
*/
|
|
55
|
+
export function hasVariadicArgument(cmd: Command): boolean {
|
|
56
|
+
return cmd.args.some((a) => a.variadic)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function getArgsRange(cmd: Command): ArgumentsRange {
|
|
60
|
+
const min = getRequiredArgsCount(cmd)
|
|
61
|
+
const max = hasVariadicArgument(cmd) ? Infinity : cmd.args.length
|
|
62
|
+
return { min, max }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function checkRequiredArgs(
|
|
66
|
+
cmd: Command,
|
|
67
|
+
args: ParsedArgumentsObject,
|
|
68
|
+
parsedArgv: ParsedArguments,
|
|
69
|
+
): BaseError[] {
|
|
70
|
+
const errors = cmd.args.reduce((acc, arg) => {
|
|
71
|
+
if (args[arg.name] === undefined && arg.required) {
|
|
72
|
+
acc.push(new MissingArgumentError(arg, cmd))
|
|
73
|
+
}
|
|
74
|
+
return acc
|
|
75
|
+
}, [] as BaseError[])
|
|
76
|
+
|
|
77
|
+
// Check if there is more args than specified
|
|
78
|
+
if (cmd.strictArgsCount) {
|
|
79
|
+
const numArgsError = checkNumberOfArgs(cmd, parsedArgv)
|
|
80
|
+
if (numArgsError) {
|
|
81
|
+
errors.push(numArgsError)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return errors
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function checkNumberOfArgs(
|
|
89
|
+
cmd: Command,
|
|
90
|
+
args: ParsedArguments,
|
|
91
|
+
): TooManyArgumentsError | void {
|
|
92
|
+
const range = getArgsRange(cmd)
|
|
93
|
+
const argsCount = Object.keys(args).length
|
|
94
|
+
if (range.max !== Infinity && range.max < Object.keys(args).length) {
|
|
95
|
+
return new TooManyArgumentsError(cmd, range, argsCount)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function removeCommandFromArgs(
|
|
100
|
+
cmd: Command,
|
|
101
|
+
args: ParsedArguments,
|
|
102
|
+
): ParsedArguments {
|
|
103
|
+
const words = cmd.name.split(" ").length
|
|
104
|
+
return args.slice(words)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function validateArg(arg: Argument, value: ParsedArgument): ReturnType<typeof validate> {
|
|
108
|
+
return arg.validator ? validate(value, arg.validator, arg) : value
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
type VariadicArgument = ParsedArgument
|
|
112
|
+
type ArgsValidatorAccumulator = Record<string, Promisable<VariadicArgument>>
|
|
113
|
+
|
|
114
|
+
interface ArgsValidationResult {
|
|
115
|
+
args: ParsedArgumentsObject
|
|
116
|
+
errors: CommonError[]
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
*
|
|
121
|
+
* @param cmd
|
|
122
|
+
* @param parsedArgv
|
|
123
|
+
*
|
|
124
|
+
* @todo Bugs:
|
|
125
|
+
*
|
|
126
|
+
*
|
|
127
|
+
* ts-node examples/pizza/pizza.ts cancel my-order jhazd hazd
|
|
128
|
+
*
|
|
129
|
+
* -> result ok, should be too many arguments
|
|
130
|
+
*
|
|
131
|
+
*/
|
|
132
|
+
export async function validateArgs(
|
|
133
|
+
cmd: Command,
|
|
134
|
+
parsedArgv: ParsedArguments,
|
|
135
|
+
): Promise<ArgsValidationResult> {
|
|
136
|
+
// remove the command from the argv array
|
|
137
|
+
const formatedArgs = cmd.isProgramCommand()
|
|
138
|
+
? parsedArgv
|
|
139
|
+
: removeCommandFromArgs(cmd, parsedArgv)
|
|
140
|
+
|
|
141
|
+
// transfrom args array to object, and set defaults for arguments not passed
|
|
142
|
+
const argsObj = getArgsObjectFromArray(cmd, formatedArgs)
|
|
143
|
+
const errors: CommonError[] = []
|
|
144
|
+
|
|
145
|
+
const validations = reduce(
|
|
146
|
+
argsObj,
|
|
147
|
+
(acc, value, key) => {
|
|
148
|
+
const arg = findArgument(cmd, key)
|
|
149
|
+
try {
|
|
150
|
+
/* istanbul ignore if -- should not happen */
|
|
151
|
+
if (!arg) {
|
|
152
|
+
throw new BaseError(`Unknown argumment ${key}`)
|
|
153
|
+
}
|
|
154
|
+
acc[key] = validateArg(arg, value)
|
|
155
|
+
} catch (e) {
|
|
156
|
+
errors.push(e)
|
|
157
|
+
}
|
|
158
|
+
return acc
|
|
159
|
+
},
|
|
160
|
+
{} as ArgsValidatorAccumulator,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
const result = await reduce(
|
|
164
|
+
validations,
|
|
165
|
+
async (prevPromise, value, key): Promise<ParsedArgumentsObject> => {
|
|
166
|
+
const collection = await prevPromise
|
|
167
|
+
collection[key] = await value
|
|
168
|
+
return collection
|
|
169
|
+
},
|
|
170
|
+
Promise.resolve({}) as Promise<ParsedArgumentsObject>,
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
errors.push(...checkRequiredArgs(cmd, result, formatedArgs))
|
|
174
|
+
|
|
175
|
+
return { args: result, errors }
|
|
176
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { program } from "../.."
|
|
2
|
+
|
|
3
|
+
program
|
|
4
|
+
.bin("pizza")
|
|
5
|
+
|
|
6
|
+
// First possible command: "order"
|
|
7
|
+
.command("order", "Order a pizza")
|
|
8
|
+
.argument("<type>", "Type of pizza", {
|
|
9
|
+
validator: ["margherita", "caprese", "quattro fromaggi"],
|
|
10
|
+
})
|
|
11
|
+
.option("-e, --extra-ingredients <ingredients>", "Extra ingredients")
|
|
12
|
+
|
|
13
|
+
// Another command: "cancel"
|
|
14
|
+
.command("cancel", "Cancel an order")
|
|
15
|
+
.argument("<order-id>", "Order id")
|
|
16
|
+
.complete(function () {
|
|
17
|
+
return ["1234", "5678"]
|
|
18
|
+
})
|
|
19
|
+
.option("-c, --cash-out <type>", "Cash out type")
|
|
20
|
+
.complete(() => {
|
|
21
|
+
return Promise.resolve(["cash", "paypal", "credit-card"])
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
export { program }
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { complete } from ".."
|
|
2
|
+
import { program } from "../__fixtures__/prog-autocomplete"
|
|
3
|
+
import filter from "lodash/filter"
|
|
4
|
+
import { expect, it, describe } from "vitest"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Helper to cimulate auto comp env
|
|
8
|
+
* @param cl Command line
|
|
9
|
+
* @param opts
|
|
10
|
+
* @param env
|
|
11
|
+
*/
|
|
12
|
+
function makeFakeEnv(
|
|
13
|
+
cl: string,
|
|
14
|
+
opts: Record<string, string> = {},
|
|
15
|
+
env = process.env,
|
|
16
|
+
): { env: NodeJS.ProcessEnv; argv: string[] } {
|
|
17
|
+
let cursorPos = cl.indexOf("|")
|
|
18
|
+
if (cursorPos === -1) {
|
|
19
|
+
cursorPos = cl.length
|
|
20
|
+
}
|
|
21
|
+
cl = cl.replace("|", "")
|
|
22
|
+
const argv = filter(cl.split(" "))
|
|
23
|
+
|
|
24
|
+
env.COMP_CWORD = String(argv.length)
|
|
25
|
+
env.COMP_LINE = cl
|
|
26
|
+
env.COMP_POINT = cursorPos === -1 ? "0" : String(cursorPos)
|
|
27
|
+
env = { ...env, ...opts }
|
|
28
|
+
|
|
29
|
+
return { env, argv: [process.execPath, __filename, "completion", "--", ...argv] }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe("autocomplete", () => {
|
|
33
|
+
describe("complete()", () => {
|
|
34
|
+
it("should auto-complete command name if possible", async () => {
|
|
35
|
+
const argv = "ord|"
|
|
36
|
+
await expect(complete(program, makeFakeEnv(argv))).resolves.toEqual([
|
|
37
|
+
{
|
|
38
|
+
name: "order",
|
|
39
|
+
description: expect.any(String),
|
|
40
|
+
},
|
|
41
|
+
])
|
|
42
|
+
await expect(complete(program, makeFakeEnv("c|"))).resolves.toEqual([
|
|
43
|
+
{
|
|
44
|
+
name: "cancel",
|
|
45
|
+
description: expect.any(String),
|
|
46
|
+
},
|
|
47
|
+
])
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it("should not auto-complete command name if none are available", async () => {
|
|
51
|
+
await expect(complete(program, makeFakeEnv("wrong|"))).resolves.toEqual([])
|
|
52
|
+
await expect(complete(program, makeFakeEnv("cancel|"))).resolves.not.toContain(
|
|
53
|
+
expect.objectContaining({
|
|
54
|
+
name: "cancel",
|
|
55
|
+
}),
|
|
56
|
+
)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it("should auto-complete arguments values from choices", async () => {
|
|
60
|
+
const argv = "order |"
|
|
61
|
+
await expect(complete(program, makeFakeEnv(argv))).resolves.toEqual([
|
|
62
|
+
{
|
|
63
|
+
name: "margherita",
|
|
64
|
+
description: expect.any(String),
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: "caprese",
|
|
68
|
+
description: expect.any(String),
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "quattro fromaggi",
|
|
72
|
+
description: expect.any(String),
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: "--extra-ingredients",
|
|
76
|
+
description: expect.any(String),
|
|
77
|
+
},
|
|
78
|
+
])
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it("should auto-complete arguments values from complete()", async () => {
|
|
82
|
+
const argv = "cancel |"
|
|
83
|
+
await expect(complete(program, makeFakeEnv(argv))).resolves.toEqual([
|
|
84
|
+
{
|
|
85
|
+
name: "1234",
|
|
86
|
+
description: expect.any(String),
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: "5678",
|
|
90
|
+
description: expect.any(String),
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
name: "--cash-out",
|
|
94
|
+
description: expect.any(String),
|
|
95
|
+
},
|
|
96
|
+
])
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it("should auto-complete partial options values from complete()", async () => {
|
|
100
|
+
const argv = "cancel --ca|"
|
|
101
|
+
await expect(complete(program, makeFakeEnv(argv))).resolves.toEqual([
|
|
102
|
+
{
|
|
103
|
+
name: "--cash-out",
|
|
104
|
+
description: expect.any(String),
|
|
105
|
+
},
|
|
106
|
+
])
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it("should auto-complete partial long options", async () => {
|
|
110
|
+
const argv = "order --ex"
|
|
111
|
+
await expect(complete(program, makeFakeEnv(argv))).resolves.toEqual([
|
|
112
|
+
{
|
|
113
|
+
name: "--extra-ingredients",
|
|
114
|
+
description: expect.any(String),
|
|
115
|
+
},
|
|
116
|
+
])
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it("should auto-complete a single dash", async () => {
|
|
120
|
+
const argv = "order -"
|
|
121
|
+
await expect(complete(program, makeFakeEnv(argv))).resolves.toEqual([
|
|
122
|
+
{
|
|
123
|
+
name: "--extra-ingredients",
|
|
124
|
+
description: expect.any(String),
|
|
125
|
+
},
|
|
126
|
+
])
|
|
127
|
+
})
|
|
128
|
+
})
|
|
129
|
+
})
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @module caporal/autocomplete
|
|
4
|
+
*/
|
|
5
|
+
import tabtab from "tabtab"
|
|
6
|
+
import { parseArgv } from "../parser"
|
|
7
|
+
import { Program } from "../program"
|
|
8
|
+
import { removeCommandFromArgs } from "../argument/validate"
|
|
9
|
+
import { Argument, Option } from "../types"
|
|
10
|
+
import { Command } from "../command"
|
|
11
|
+
import { isOptionObject } from "../option"
|
|
12
|
+
import { findCommand } from "../command/find"
|
|
13
|
+
import filter from "lodash/filter"
|
|
14
|
+
import flatMap from "lodash/flatMap"
|
|
15
|
+
|
|
16
|
+
import { Completions, Completer, CompletionItem, CompletionContext } from "./types"
|
|
17
|
+
|
|
18
|
+
const completions: Completions = new Map()
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Register a completion handler
|
|
22
|
+
*
|
|
23
|
+
* @param {Argument|Option} arg_or_opt argument or option to complete
|
|
24
|
+
* @param {Function} completer
|
|
25
|
+
*/
|
|
26
|
+
export function registerCompletion(
|
|
27
|
+
argOrOpt: Argument | Option,
|
|
28
|
+
completer: Completer,
|
|
29
|
+
): void {
|
|
30
|
+
completions.set(argOrOpt, completer)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function installCompletion(program: Program): Promise<void> {
|
|
34
|
+
return tabtab.install({
|
|
35
|
+
name: program.getBin(),
|
|
36
|
+
completer: program.getBin(),
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function uninstallCompletion(program: Program): Promise<void> {
|
|
41
|
+
return tabtab.uninstall({
|
|
42
|
+
name: program.getBin(),
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Called by tabtab
|
|
48
|
+
*/
|
|
49
|
+
export async function complete(
|
|
50
|
+
program: Program,
|
|
51
|
+
{ env, argv } = { env: process.env, argv: process.argv },
|
|
52
|
+
): Promise<CompletionItem[] | false> {
|
|
53
|
+
const compEnv = tabtab.parseEnv(env)
|
|
54
|
+
if (!compEnv.complete) {
|
|
55
|
+
return false
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const ctx = await getContext(program, compEnv, argv)
|
|
59
|
+
const compPromises = [
|
|
60
|
+
getCompCommands(ctx),
|
|
61
|
+
getCompArgValues(ctx),
|
|
62
|
+
getCompOptNames(ctx),
|
|
63
|
+
getCompOptValues(ctx),
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
const comps = flatMap(await Promise.all(compPromises))
|
|
67
|
+
tabtab.log(comps)
|
|
68
|
+
|
|
69
|
+
return comps
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function getContext(
|
|
73
|
+
program: Program,
|
|
74
|
+
compEnv: tabtab.TabtabEnv,
|
|
75
|
+
argv: string[],
|
|
76
|
+
): Promise<CompletionContext> {
|
|
77
|
+
const { lastPartial } = compEnv
|
|
78
|
+
argv = argv.slice(4)
|
|
79
|
+
const currentCmd = await findCommand(program, argv)
|
|
80
|
+
const parserResult = parseArgv(currentCmd?.getParserConfig(), argv)
|
|
81
|
+
const lastPartIsOpt = lastPartial.startsWith("-")
|
|
82
|
+
const lastPartIsKnownOpt = Boolean(
|
|
83
|
+
lastPartIsOpt && currentCmd && getLastPartIsKnownOpt(currentCmd, lastPartial),
|
|
84
|
+
)
|
|
85
|
+
const currentOpt = currentCmd
|
|
86
|
+
? currentCmd.options.find((o) => o.allNames.includes(lastPartial))
|
|
87
|
+
: undefined
|
|
88
|
+
|
|
89
|
+
const ctx = {
|
|
90
|
+
program,
|
|
91
|
+
currentCmd,
|
|
92
|
+
compEnv,
|
|
93
|
+
parserResult,
|
|
94
|
+
lastPartIsOpt,
|
|
95
|
+
lastPartIsKnownOpt,
|
|
96
|
+
currentOpt,
|
|
97
|
+
}
|
|
98
|
+
return ctx
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function getCompCommands(ctx: CompletionContext): Promise<CompletionItem[]> {
|
|
102
|
+
const {
|
|
103
|
+
compEnv: { line },
|
|
104
|
+
program,
|
|
105
|
+
currentCmd,
|
|
106
|
+
} = ctx
|
|
107
|
+
const commandMatch = (cmd: Command, str: string): boolean => {
|
|
108
|
+
return cmd.name.startsWith(str) || cmd.getAliases().some((a) => a.startsWith(str))
|
|
109
|
+
}
|
|
110
|
+
let commands = await program.getAllCommands()
|
|
111
|
+
commands = filter(commands, (cmd) => commandMatch(cmd, line) && cmd !== currentCmd)
|
|
112
|
+
return commands.map((cmd) => ({
|
|
113
|
+
name: cmd.name,
|
|
114
|
+
description: cmd.description,
|
|
115
|
+
}))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function getCompOptNames(ctx: CompletionContext): Promise<CompletionItem[]> {
|
|
119
|
+
const {
|
|
120
|
+
compEnv: { lastPartial },
|
|
121
|
+
currentCmd,
|
|
122
|
+
} = ctx
|
|
123
|
+
|
|
124
|
+
if (!currentCmd) {
|
|
125
|
+
return []
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const matchOptionName = (o: Option): boolean => {
|
|
129
|
+
return Boolean(
|
|
130
|
+
(o.shortNotation != lastPartial && o.shortNotation?.startsWith(lastPartial)) ||
|
|
131
|
+
(o.longNotation != lastPartial && o.longNotation?.startsWith(lastPartial)),
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return filter(currentCmd.options, matchOptionName).map((opt) => ({
|
|
136
|
+
name: opt.notation,
|
|
137
|
+
description: opt.description,
|
|
138
|
+
}))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function getCompOptValues(ctx: CompletionContext): Promise<CompletionItem[]> {
|
|
142
|
+
const { currentOpt } = ctx
|
|
143
|
+
if (!currentOpt) {
|
|
144
|
+
return []
|
|
145
|
+
}
|
|
146
|
+
// Choices
|
|
147
|
+
if (currentOpt.choices.length) {
|
|
148
|
+
return currentOpt.choices.map((choice) => ({
|
|
149
|
+
name: choice.toString(),
|
|
150
|
+
description: "Value for option " + currentOpt.synopsis,
|
|
151
|
+
}))
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Promise completion
|
|
155
|
+
const completer = completions.get(currentOpt)
|
|
156
|
+
if (completer) {
|
|
157
|
+
return hanldleCompleter(ctx, completer, currentOpt)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return []
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function getCompArgValues(ctx: CompletionContext): Promise<CompletionItem[]> {
|
|
164
|
+
const {
|
|
165
|
+
compEnv: { lastPartial },
|
|
166
|
+
parserResult: { args },
|
|
167
|
+
currentCmd,
|
|
168
|
+
lastPartIsOpt,
|
|
169
|
+
} = ctx
|
|
170
|
+
|
|
171
|
+
if (!currentCmd || lastPartIsOpt) {
|
|
172
|
+
return []
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const formatedArgs = currentCmd.isProgramCommand()
|
|
176
|
+
? args
|
|
177
|
+
: removeCommandFromArgs(currentCmd, args)
|
|
178
|
+
|
|
179
|
+
const argsCount = formatedArgs.length
|
|
180
|
+
const arg = currentCmd.args[argsCount]
|
|
181
|
+
|
|
182
|
+
if (!arg) {
|
|
183
|
+
return []
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Choices
|
|
187
|
+
if (arg.choices.length) {
|
|
188
|
+
return arg.choices
|
|
189
|
+
.map((choice) => ({
|
|
190
|
+
name: choice.toString(),
|
|
191
|
+
description: "Value for argument " + arg.synopsis,
|
|
192
|
+
}))
|
|
193
|
+
.filter((choice) => !lastPartial || choice.name.toString().startsWith(lastPartial))
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Promise completion
|
|
197
|
+
const completion = completions.get(arg)
|
|
198
|
+
if (completion) {
|
|
199
|
+
return hanldleCompleter(ctx, completion, arg)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return []
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function hanldleCompleter(
|
|
206
|
+
ctx: CompletionContext,
|
|
207
|
+
completer: Completer,
|
|
208
|
+
argOrOpt: Argument | Option,
|
|
209
|
+
): Promise<CompletionItem[]> {
|
|
210
|
+
const type = isOptionObject(argOrOpt) ? "option" : "argument"
|
|
211
|
+
const res = await completer(ctx)
|
|
212
|
+
return res.map((item) => {
|
|
213
|
+
if (typeof item === "object") {
|
|
214
|
+
return item
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
name: "" + item,
|
|
218
|
+
description: `Value for ${type} ${argOrOpt.synopsis}`,
|
|
219
|
+
}
|
|
220
|
+
})
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function getLastPartIsKnownOpt(cmd: Command, lastPartial: string): boolean {
|
|
224
|
+
return cmd.options.some((o) => o.allNames.includes(lastPartial))
|
|
225
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @module caporal/types
|
|
4
|
+
*/
|
|
5
|
+
import type { Argument, Option, Promisable, ParserResult } from "../types"
|
|
6
|
+
import type tabtab from "tabtab"
|
|
7
|
+
import type { Command } from "../command"
|
|
8
|
+
import type { Program } from "../program"
|
|
9
|
+
|
|
10
|
+
export interface CompletionItem {
|
|
11
|
+
name: string
|
|
12
|
+
description: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Completer {
|
|
16
|
+
(ctx: CompletionContext): Promisable<(string | CompletionItem)[]>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface CompletionContext {
|
|
20
|
+
program: Program
|
|
21
|
+
currentCmd?: Command
|
|
22
|
+
compEnv: tabtab.TabtabEnv
|
|
23
|
+
parserResult: ParserResult
|
|
24
|
+
lastPartIsOpt: boolean
|
|
25
|
+
lastPartIsKnownOpt: boolean
|
|
26
|
+
currentOpt?: Option
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type Completions = Map<Argument | Option, Completer>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @ignore
|
|
4
|
+
*/
|
|
5
|
+
/* istanbul ignore file */
|
|
6
|
+
import type { Command } from ".."
|
|
7
|
+
import type { CreateCommandParameters } from "../../types"
|
|
8
|
+
|
|
9
|
+
export default function ({ createCommand }: CreateCommandParameters): Command {
|
|
10
|
+
return createCommand("My command").action(({ logger }) => {
|
|
11
|
+
logger.info("Output of my command")
|
|
12
|
+
return "hey"
|
|
13
|
+
})
|
|
14
|
+
}
|