diff --git a/CHANGES.rst b/CHANGES.rst index 7f1530ab4..946aaa061 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -31,6 +31,8 @@ Unreleased - When generating a command's name from a decorated function's name, the suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed. :issue:`2322` +- Add new method to `` Choice`` for getting a fail message and refactor + the convert method to use it. Version 8.1.7 diff --git a/src/click/types.py b/src/click/types.py index 270e63952..12e1da48c 100644 --- a/src/click/types.py +++ b/src/click/types.py @@ -297,16 +297,15 @@ def convert( if normed_value in normed_choices: return normed_choices[normed_value] + self.fail(self.get_missing_message(value), param, ctx) + + def get_invalid_choice_fail_message(self, value: t.Any) -> str: choices_str = ", ".join(map(repr, self.choices)) - self.fail( - ngettext( - "{value!r} is not {choice}.", - "{value!r} is not one of {choices}.", - len(self.choices), - ).format(value=value, choice=choices_str, choices=choices_str), - param, - ctx, - ) + return ngettext( + "{value!r} is not {choice}.", + "{value!r} is not one of {choices}.", + len(self.choices), + ).format(value=value, choice=choices_str, choices=choices_str) def __repr__(self) -> str: return f"Choice({list(self.choices)})"