An internal exception that signals a usage error. This typically aborts any further handling. :param message: the error message to display. :param ctx: optionally the context that caused this error. Click will fill in the context automatically in some situations.
| 66 | |
| 67 | |
| 68 | class UsageError(ClickException): |
| 69 | """An internal exception that signals a usage error. This typically |
| 70 | aborts any further handling. |
| 71 | |
| 72 | :param message: the error message to display. |
| 73 | :param ctx: optionally the context that caused this error. Click will |
| 74 | fill in the context automatically in some situations. |
| 75 | """ |
| 76 | |
| 77 | exit_code: t.ClassVar[int] = 2 |
| 78 | |
| 79 | ctx: Context | None |
| 80 | cmd: t.Final[Command | None] |
| 81 | |
| 82 | def __init__(self, message: str, ctx: Context | None = None) -> None: |
| 83 | super().__init__(message) |
| 84 | self.ctx = ctx |
| 85 | self.cmd = self.ctx.command if self.ctx else None |
| 86 | |
| 87 | def show(self, file: t.IO[t.Any] | None = None) -> None: |
| 88 | if file is None: |
| 89 | file = get_text_stderr() |
| 90 | color = None |
| 91 | hint = "" |
| 92 | if ( |
| 93 | self.ctx is not None |
| 94 | and self.ctx.command.get_help_option(self.ctx) is not None |
| 95 | ): |
| 96 | help_names = self.ctx.command.get_help_option_names(self.ctx) |
| 97 | # Pick the longest name (like ``--help`` over ``-h``) for |
| 98 | # readability in error messages. |
| 99 | hint = _("Try '{command} {option}' for help.").format( |
| 100 | command=self.ctx.command_path, |
| 101 | option=max(help_names, key=len), |
| 102 | ) |
| 103 | hint = f"{hint}\n" |
| 104 | if self.ctx is not None: |
| 105 | color = self.ctx.color |
| 106 | echo(f"{self.ctx.get_usage()}\n{hint}", file=file, color=color) |
| 107 | echo( |
| 108 | _("Error: {message}").format(message=self.format_message()), |
| 109 | file=file, |
| 110 | color=color, |
| 111 | ) |
| 112 | |
| 113 | |
| 114 | class BadParameter(UsageError): |