exceptions.py
This file
Path
src/click/exceptions.py
287 lines · 9.0 KiB · Python
Last changed by
Merge pull request #1856 from pallets/typingDavid Lordbefore adoptionffeddba
History of this fileTake it
1import os2import typing as t3from gettext import gettext as _4from gettext import ngettext56from ._compat import get_text_stderr7from .utils import echo89if t.TYPE_CHECKING:10 from .core import Context11 from .core import Parameter121314def _join_param_hints(15 param_hint: t.Optional[t.Union[t.Sequence[str], str]]16) -> t.Optional[str]:17 if param_hint is not None and not isinstance(param_hint, str):18 return " / ".join(repr(x) for x in param_hint)1920 return param_hint212223class ClickException(Exception):24 """An exception that Click can handle and show to the user."""2526 #: The exit code for this exception.27 exit_code = 12829 def __init__(self, message: str) -> None:30 super().__init__(message)31 self.message = message3233 def format_message(self) -> str:34 return self.message3536 def __str__(self) -> str:37 return self.message3839 def show(self, file: t.Optional[t.IO] = None) -> None:40 if file is None:41 file = get_text_stderr()4243 echo(_("Error: {message}").format(message=self.format_message()), file=file)444546class UsageError(ClickException):47 """An internal exception that signals a usage error. This typically48 aborts any further handling.4950 :param message: the error message to display.51 :param ctx: optionally the context that caused this error. Click will52 fill in the context automatically in some situations.53 """5455 exit_code = 25657 def __init__(self, message: str, ctx: t.Optional["Context"] = None) -> None:58 super().__init__(message)59 self.ctx = ctx60 self.cmd = self.ctx.command if self.ctx else None6162 def show(self, file: t.Optional[t.IO] = None) -> None:63 if file is None:64 file = get_text_stderr()65 color = None66 hint = ""67 if (68 self.ctx is not None69 and self.ctx.command.get_help_option(self.ctx) is not None70 ):71 hint = _("Try '{command} {option}' for help.").format(72 command=self.ctx.command_path, option=self.ctx.help_option_names[0]73 )74 hint = f"{hint}\n"75 if self.ctx is not None:76 color = self.ctx.color77 echo(f"{self.ctx.get_usage()}\n{hint}", file=file, color=color)78 echo(79 _("Error: {message}").format(message=self.format_message()),80 file=file,81 color=color,82 )838485class BadParameter(UsageError):86 """An exception that formats out a standardized error message for a87 bad parameter. This is useful when thrown from a callback or type as88 Click will attach contextual information to it (for instance, which89 parameter it is).9091 .. versionadded:: 2.09293 :param param: the parameter object that caused this error. This can94 be left out, and Click will attach this info itself95 if possible.96 :param param_hint: a string that shows up as parameter name. This97 can be used as alternative to `param` in cases98 where custom validation should happen. If it is99 a string it's used as such, if it's a list then100 each item is quoted and separated.101 """102103 def __init__(104 self,105 message: str,106 ctx: t.Optional["Context"] = None,107 param: t.Optional["Parameter"] = None,108 param_hint: t.Optional[str] = None,109 ) -> None:110 super().__init__(message, ctx)111 self.param = param112 self.param_hint = param_hint113114 def format_message(self) -> str:115 if self.param_hint is not None:116 param_hint = self.param_hint117 elif self.param is not None:118 param_hint = self.param.get_error_hint(self.ctx) # type: ignore119 else:120 return _("Invalid value: {message}").format(message=self.message)121122 return _("Invalid value for {param_hint}: {message}").format(123 param_hint=_join_param_hints(param_hint), message=self.message124 )125126127class MissingParameter(BadParameter):128 """Raised if click required an option or argument but it was not129 provided when invoking the script.130131 .. versionadded:: 4.0132133 :param param_type: a string that indicates the type of the parameter.134 The default is to inherit the parameter type from135 the given `param`. Valid values are ``'parameter'``,136 ``'option'`` or ``'argument'``.137 """138139 def __init__(140 self,141 message: t.Optional[str] = None,142 ctx: t.Optional["Context"] = None,143 param: t.Optional["Parameter"] = None,144 param_hint: t.Optional[str] = None,145 param_type: t.Optional[str] = None,146 ) -> None:147 super().__init__(message or "", ctx, param, param_hint)148 self.param_type = param_type149150 def format_message(self) -> str:151 if self.param_hint is not None:152 param_hint: t.Optional[str] = self.param_hint153 elif self.param is not None:154 param_hint = self.param.get_error_hint(self.ctx) # type: ignore155 else:156 param_hint = None157158 param_hint = _join_param_hints(param_hint)159 param_hint = f" {param_hint}" if param_hint else ""160161 param_type = self.param_type162 if param_type is None and self.param is not None:163 param_type = self.param.param_type_name164165 msg = self.message166 if self.param is not None:167 msg_extra = self.param.type.get_missing_message(self.param)168 if msg_extra:169 if msg:170 msg += f". {msg_extra}"171 else:172 msg = msg_extra173174 msg = f" {msg}" if msg else ""175176 # Translate param_type for known types.177 if param_type == "argument":178 missing = _("Missing argument")179 elif param_type == "option":180 missing = _("Missing option")181 elif param_type == "parameter":182 missing = _("Missing parameter")183 else:184 missing = _("Missing {param_type}").format(param_type=param_type)185186 return f"{missing}{param_hint}.{msg}"187188 def __str__(self) -> str:189 if not self.message:190 param_name = self.param.name if self.param else None191 return _("Missing parameter: {param_name}").format(param_name=param_name)192 else:193 return self.message194195196class NoSuchOption(UsageError):197 """Raised if click attempted to handle an option that does not198 exist.199200 .. versionadded:: 4.0201 """202203 def __init__(204 self,205 option_name: str,206 message: t.Optional[str] = None,207 possibilities: t.Optional[t.Sequence[str]] = None,208 ctx: t.Optional["Context"] = None,209 ) -> None:210 if message is None:211 message = _("No such option: {name}").format(name=option_name)212213 super().__init__(message, ctx)214 self.option_name = option_name215 self.possibilities = possibilities216217 def format_message(self) -> str:218 if not self.possibilities:219 return self.message220221 possibility_str = ", ".join(sorted(self.possibilities))222 suggest = ngettext(223 "Did you mean {possibility}?",224 "(Possible options: {possibilities})",225 len(self.possibilities),226 ).format(possibility=possibility_str, possibilities=possibility_str)227 return f"{self.message} {suggest}"228229230class BadOptionUsage(UsageError):231 """Raised if an option is generally supplied but the use of the option232 was incorrect. This is for instance raised if the number of arguments233 for an option is not correct.234235 .. versionadded:: 4.0236237 :param option_name: the name of the option being used incorrectly.238 """239240 def __init__(241 self, option_name: str, message: str, ctx: t.Optional["Context"] = None242 ) -> None:243 super().__init__(message, ctx)244 self.option_name = option_name245246247class BadArgumentUsage(UsageError):248 """Raised if an argument is generally supplied but the use of the argument249 was incorrect. This is for instance raised if the number of values250 for an argument is not correct.251252 .. versionadded:: 6.0253 """254255256class FileError(ClickException):257 """Raised if a file cannot be opened."""258259 def __init__(self, filename: str, hint: t.Optional[str] = None) -> None:260 if hint is None:261 hint = _("unknown error")262263 super().__init__(hint)264 self.ui_filename = os.fsdecode(filename)265 self.filename = filename266267 def format_message(self) -> str:268 return _("Could not open file {filename!r}: {message}").format(269 filename=self.ui_filename, message=self.message270 )271272273class Abort(RuntimeError):274 """An internal signalling exception that signals Click to abort."""275276277class Exit(RuntimeError):278 """An exception that indicates that the application should exit with some279 status code.280281 :param code: the status code to exit with.282 """283284 __slots__ = ("exit_code",)285286 def __init__(self, code: int = 0) -> None:287 self.exit_code = code