You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
7.5 KiB

4 years ago
  1. from ._compat import PY2, filename_to_ui, get_text_stderr
  2. from .utils import echo
  3. def _join_param_hints(param_hint):
  4. if isinstance(param_hint, (tuple, list)):
  5. return ' / '.join('"%s"' % x for x in param_hint)
  6. return param_hint
  7. class ClickException(Exception):
  8. """An exception that Click can handle and show to the user."""
  9. #: The exit code for this exception
  10. exit_code = 1
  11. def __init__(self, message):
  12. ctor_msg = message
  13. if PY2:
  14. if ctor_msg is not None:
  15. ctor_msg = ctor_msg.encode('utf-8')
  16. Exception.__init__(self, ctor_msg)
  17. self.message = message
  18. def format_message(self):
  19. return self.message
  20. def __str__(self):
  21. return self.message
  22. if PY2:
  23. __unicode__ = __str__
  24. def __str__(self):
  25. return self.message.encode('utf-8')
  26. def show(self, file=None):
  27. if file is None:
  28. file = get_text_stderr()
  29. echo('Error: %s' % self.format_message(), file=file)
  30. class UsageError(ClickException):
  31. """An internal exception that signals a usage error. This typically
  32. aborts any further handling.
  33. :param message: the error message to display.
  34. :param ctx: optionally the context that caused this error. Click will
  35. fill in the context automatically in some situations.
  36. """
  37. exit_code = 2
  38. def __init__(self, message, ctx=None):
  39. ClickException.__init__(self, message)
  40. self.ctx = ctx
  41. self.cmd = self.ctx and self.ctx.command or None
  42. def show(self, file=None):
  43. if file is None:
  44. file = get_text_stderr()
  45. color = None
  46. hint = ''
  47. if (self.cmd is not None and
  48. self.cmd.get_help_option(self.ctx) is not None):
  49. hint = ('Try "%s %s" for help.\n'
  50. % (self.ctx.command_path, self.ctx.help_option_names[0]))
  51. if self.ctx is not None:
  52. color = self.ctx.color
  53. echo(self.ctx.get_usage() + '\n%s' % hint, file=file, color=color)
  54. echo('Error: %s' % self.format_message(), file=file, color=color)
  55. class BadParameter(UsageError):
  56. """An exception that formats out a standardized error message for a
  57. bad parameter. This is useful when thrown from a callback or type as
  58. Click will attach contextual information to it (for instance, which
  59. parameter it is).
  60. .. versionadded:: 2.0
  61. :param param: the parameter object that caused this error. This can
  62. be left out, and Click will attach this info itself
  63. if possible.
  64. :param param_hint: a string that shows up as parameter name. This
  65. can be used as alternative to `param` in cases
  66. where custom validation should happen. If it is
  67. a string it's used as such, if it's a list then
  68. each item is quoted and separated.
  69. """
  70. def __init__(self, message, ctx=None, param=None,
  71. param_hint=None):
  72. UsageError.__init__(self, message, ctx)
  73. self.param = param
  74. self.param_hint = param_hint
  75. def format_message(self):
  76. if self.param_hint is not None:
  77. param_hint = self.param_hint
  78. elif self.param is not None:
  79. param_hint = self.param.get_error_hint(self.ctx)
  80. else:
  81. return 'Invalid value: %s' % self.message
  82. param_hint = _join_param_hints(param_hint)
  83. return 'Invalid value for %s: %s' % (param_hint, self.message)
  84. class MissingParameter(BadParameter):
  85. """Raised if click required an option or argument but it was not
  86. provided when invoking the script.
  87. .. versionadded:: 4.0
  88. :param param_type: a string that indicates the type of the parameter.
  89. The default is to inherit the parameter type from
  90. the given `param`. Valid values are ``'parameter'``,
  91. ``'option'`` or ``'argument'``.
  92. """
  93. def __init__(self, message=None, ctx=None, param=None,
  94. param_hint=None, param_type=None):
  95. BadParameter.__init__(self, message, ctx, param, param_hint)
  96. self.param_type = param_type
  97. def format_message(self):
  98. if self.param_hint is not None:
  99. param_hint = self.param_hint
  100. elif self.param is not None:
  101. param_hint = self.param.get_error_hint(self.ctx)
  102. else:
  103. param_hint = None
  104. param_hint = _join_param_hints(param_hint)
  105. param_type = self.param_type
  106. if param_type is None and self.param is not None:
  107. param_type = self.param.param_type_name
  108. msg = self.message
  109. if self.param is not None:
  110. msg_extra = self.param.type.get_missing_message(self.param)
  111. if msg_extra:
  112. if msg:
  113. msg += '. ' + msg_extra
  114. else:
  115. msg = msg_extra
  116. return 'Missing %s%s%s%s' % (
  117. param_type,
  118. param_hint and ' %s' % param_hint or '',
  119. msg and '. ' or '.',
  120. msg or '',
  121. )
  122. class NoSuchOption(UsageError):
  123. """Raised if click attempted to handle an option that does not
  124. exist.
  125. .. versionadded:: 4.0
  126. """
  127. def __init__(self, option_name, message=None, possibilities=None,
  128. ctx=None):
  129. if message is None:
  130. message = 'no such option: %s' % option_name
  131. UsageError.__init__(self, message, ctx)
  132. self.option_name = option_name
  133. self.possibilities = possibilities
  134. def format_message(self):
  135. bits = [self.message]
  136. if self.possibilities:
  137. if len(self.possibilities) == 1:
  138. bits.append('Did you mean %s?' % self.possibilities[0])
  139. else:
  140. possibilities = sorted(self.possibilities)
  141. bits.append('(Possible options: %s)' % ', '.join(possibilities))
  142. return ' '.join(bits)
  143. class BadOptionUsage(UsageError):
  144. """Raised if an option is generally supplied but the use of the option
  145. was incorrect. This is for instance raised if the number of arguments
  146. for an option is not correct.
  147. .. versionadded:: 4.0
  148. :param option_name: the name of the option being used incorrectly.
  149. """
  150. def __init__(self, option_name, message, ctx=None):
  151. UsageError.__init__(self, message, ctx)
  152. self.option_name = option_name
  153. class BadArgumentUsage(UsageError):
  154. """Raised if an argument is generally supplied but the use of the argument
  155. was incorrect. This is for instance raised if the number of values
  156. for an argument is not correct.
  157. .. versionadded:: 6.0
  158. """
  159. def __init__(self, message, ctx=None):
  160. UsageError.__init__(self, message, ctx)
  161. class FileError(ClickException):
  162. """Raised if a file cannot be opened."""
  163. def __init__(self, filename, hint=None):
  164. ui_filename = filename_to_ui(filename)
  165. if hint is None:
  166. hint = 'unknown error'
  167. ClickException.__init__(self, hint)
  168. self.ui_filename = ui_filename
  169. self.filename = filename
  170. def format_message(self):
  171. return 'Could not open file %s: %s' % (self.ui_filename, self.message)
  172. class Abort(RuntimeError):
  173. """An internal signalling exception that signals Click to abort."""
  174. class Exit(RuntimeError):
  175. """An exception that indicates that the application should exit with some
  176. status code.
  177. :param code: the status code to exit with.
  178. """
  179. def __init__(self, code=0):
  180. self.exit_code = code