exceptions.py
This file
Path
src/click/exceptions.py
289 lines · 9.0 KiB · Python
Last changed by
enable deferred evaluation of annotations (#2270)David Lordbefore adoption56db546
History of this fileTake it
1from __future__ import annotations23import collections.abc as cabc4import typing as t5from gettext import gettext as _6from gettext import ngettext78from ._compat import get_text_stderr9from .utils import echo10from .utils import format_filename1112if t.TYPE_CHECKING:13 from .core import Command14 from .core import Context15 from .core import Parameter161718def _join_param_hints(param_hint: cabc.Sequence[str] | str | None) -> str | None:19 if param_hint is not None and not isinstance(param_hint, str):20 return " / ".join(repr(x) for x in param_hint)2122 return param_hint232425class ClickException(Exception):26 """An exception that Click can handle and show to the user."""2728 #: The exit code for this exception.29 exit_code = 13031 def __init__(self, message: str) -> None:32 super().__init__(message)33 self.message = message3435 def format_message(self) -> str:36 return self.message3738 def __str__(self) -> str:39 return self.message4041 def show(self, file: t.IO[t.Any] | None = None) -> None:42 if file is None:43 file = get_text_stderr()4445 echo(_("Error: {message}").format(message=self.format_message()), file=file)464748class UsageError(ClickException):49 """An internal exception that signals a usage error. This typically50 aborts any further handling.5152 :param message: the error message to display.53 :param ctx: optionally the context that caused this error. Click will54 fill in the context automatically in some situations.55 """5657 exit_code = 25859 def __init__(self, message: str, ctx: Context | None = None) -> None:60 super().__init__(message)61 self.ctx = ctx62 self.cmd: Command | None = self.ctx.command if self.ctx else None6364 def show(self, file: t.IO[t.Any] | None = None) -> None:65 if file is None:66 file = get_text_stderr()67 color = None68 hint = ""69 if (70 self.ctx is not None71 and self.ctx.command.get_help_option(self.ctx) is not None72 ):73 hint = _("Try '{command} {option}' for help.").format(74 command=self.ctx.command_path, option=self.ctx.help_option_names[0]75 )76 hint = f"{hint}\n"77 if self.ctx is not None:78 color = self.ctx.color79 echo(f"{self.ctx.get_usage()}\n{hint}", file=file, color=color)80 echo(81 _("Error: {message}").format(message=self.format_message()),82 file=file,83 color=color,84 )858687class BadParameter(UsageError):88 """An exception that formats out a standardized error message for a89 bad parameter. This is useful when thrown from a callback or type as90 Click will attach contextual information to it (for instance, which91 parameter it is).9293 .. versionadded:: 2.09495 :param param: the parameter object that caused this error. This can96 be left out, and Click will attach this info itself97 if possible.98 :param param_hint: a string that shows up as parameter name. This99 can be used as alternative to `param` in cases100 where custom validation should happen. If it is101 a string it's used as such, if it's a list then102 each item is quoted and separated.103 """104105 def __init__(106 self,107 message: str,108 ctx: Context | None = None,109 param: Parameter | None = None,110 param_hint: str | None = None,111 ) -> None:112 super().__init__(message, ctx)113 self.param = param114 self.param_hint = param_hint115116 def format_message(self) -> str:117 if self.param_hint is not None:118 param_hint = self.param_hint119 elif self.param is not None:120 param_hint = self.param.get_error_hint(self.ctx) # type: ignore121 else:122 return _("Invalid value: {message}").format(message=self.message)123124 return _("Invalid value for {param_hint}: {message}").format(125 param_hint=_join_param_hints(param_hint), message=self.message126 )127128129class MissingParameter(BadParameter):130 """Raised if click required an option or argument but it was not131 provided when invoking the script.132133 .. versionadded:: 4.0134135 :param param_type: a string that indicates the type of the parameter.136 The default is to inherit the parameter type from137 the given `param`. Valid values are ``'parameter'``,138 ``'option'`` or ``'argument'``.139 """140141 def __init__(142 self,143 message: str | None = None,144 ctx: Context | None = None,145 param: Parameter | None = None,146 param_hint: str | None = None,147 param_type: str | None = None,148 ) -> None:149 super().__init__(message or "", ctx, param, param_hint)150 self.param_type = param_type151152 def format_message(self) -> str:153 if self.param_hint is not None:154 param_hint: str | None = self.param_hint155 elif self.param is not None:156 param_hint = self.param.get_error_hint(self.ctx) # type: ignore157 else:158 param_hint = None159160 param_hint = _join_param_hints(param_hint)161 param_hint = f" {param_hint}" if param_hint else ""162163 param_type = self.param_type164 if param_type is None and self.param is not None:165 param_type = self.param.param_type_name166167 msg = self.message168 if self.param is not None:169 msg_extra = self.param.type.get_missing_message(self.param)170 if msg_extra:171 if msg:172 msg += f". {msg_extra}"173 else:174 msg = msg_extra175176 msg = f" {msg}" if msg else ""177178 # Translate param_type for known types.179 if param_type == "argument":180 missing = _("Missing argument")181 elif param_type == "option":182 missing = _("Missing option")183 elif param_type == "parameter":184 missing = _("Missing parameter")185 else:186 missing = _("Missing {param_type}").format(param_type=param_type)187188 return f"{missing}{param_hint}.{msg}"189190 def __str__(self) -> str:191 if not self.message:192 param_name = self.param.name if self.param else None193 return _("Missing parameter: {param_name}").format(param_name=param_name)194 else:195 return self.message196197198class NoSuchOption(UsageError):199 """Raised if click attempted to handle an option that does not200 exist.201202 .. versionadded:: 4.0203 """204205 def __init__(206 self,207 option_name: str,208 message: str | None = None,209 possibilities: cabc.Sequence[str] | None = None,210 ctx: Context | None = None,211 ) -> None:212 if message is None:213 message = _("No such option: {name}").format(name=option_name)214215 super().__init__(message, ctx)216 self.option_name = option_name217 self.possibilities = possibilities218219 def format_message(self) -> str:220 if not self.possibilities:221 return self.message222223 possibility_str = ", ".join(sorted(self.possibilities))224 suggest = ngettext(225 "Did you mean {possibility}?",226 "(Possible options: {possibilities})",227 len(self.possibilities),228 ).format(possibility=possibility_str, possibilities=possibility_str)229 return f"{self.message} {suggest}"230231232class BadOptionUsage(UsageError):233 """Raised if an option is generally supplied but the use of the option234 was incorrect. This is for instance raised if the number of arguments235 for an option is not correct.236237 .. versionadded:: 4.0238239 :param option_name: the name of the option being used incorrectly.240 """241242 def __init__(243 self, option_name: str, message: str, ctx: Context | None = None244 ) -> None:245 super().__init__(message, ctx)246 self.option_name = option_name247248249class BadArgumentUsage(UsageError):250 """Raised if an argument is generally supplied but the use of the argument251 was incorrect. This is for instance raised if the number of values252 for an argument is not correct.253254 .. versionadded:: 6.0255 """256257258class FileError(ClickException):259 """Raised if a file cannot be opened."""260261 def __init__(self, filename: str, hint: str | None = None) -> None:262 if hint is None:263 hint = _("unknown error")264265 super().__init__(hint)266 self.ui_filename: str = format_filename(filename)267 self.filename = filename268269 def format_message(self) -> str:270 return _("Could not open file {filename!r}: {message}").format(271 filename=self.ui_filename, message=self.message272 )273274275class Abort(RuntimeError):276 """An internal signalling exception that signals Click to abort."""277278279class Exit(RuntimeError):280 """An exception that indicates that the application should exit with some281 status code.282283 :param code: the status code to exit with.284 """285286 __slots__ = ("exit_code",)287288 def __init__(self, code: int = 0) -> None:289 self.exit_code: int = code