Skip to content
Myr

Refactor missing parameter handling.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This improves the formatting of missing arguments (they are now rendered as metavars) and it introduces a MissingParameter exception.

authored by Armin Ronacher · committed by Armin Ronacher · parent 46b22d0 · Browse files · 4 files+74 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+74 added−20 removed

  • other:+74 added−20 removed

4 files+74 added−20 removed

modified:click/__init__.py+2 added-2 removed
36 unmodified lines
37 pause3839# Exceptions40from .exceptions import ClickException, UsageError, BadParameter, \41 FileError, Abort, NoSuchOption, BadOptionUsage41 FileError, Abort, NoSuchOption, BadOptionUsage, MissingParameter4243# Formatting44from .formatting import HelpFormatter, wrap_text45
25 unmodified lines
71 'getchar', 'pause',7273 # Exceptions74 'ClickException', 'UsageError', 'BadParameter', 'FileError',75 'Abort', 'NoSuchOption', 'BadOptionUsage',75 'Abort', 'NoSuchOption', 'BadOptionUsage', 'MissingParameter',7677 # Formatting78 'HelpFormatter', 'wrap_text',79
6 unmodified lines
modified:click/core.py+17 added-14 removed
1import os2import sys3import codecs4from contextlib import contextmanager5from itertools import chain, repeat5from itertools import repeat6from functools import update_wrapper78from .types import convert_type, IntRange, BOOL9from .utils import make_str, make_default_short_help, echo10from .exceptions import ClickException, UsageError, BadParameter, Abort10from .exceptions import ClickException, UsageError, BadParameter, Abort, \11 MissingParameter12from .termui import prompt, confirm13from .formatting import HelpFormatter, join_options14from .parser import OptionParser, split_opt15
1178 unmodified lines
1194 self.is_eager = is_eager1195 self.metavar = metavar1196 self.envvar = envvar11971198 @property1199 def human_readable_name(self):1200 """Returns the human readable name of this parameter. This is the1201 same as the name for options, but the metavar for arguments.1202 """1203 return self.name12041205 def make_metavar(self):1206 if self.metavar is not None:1207 return self.metavar1208 metavar = self.type.get_metavar(self)
57 unmodified lines
1266 if value is None:1267 value = self.get_default(ctx)12681269 if self.required and self.value_is_missing(value):1262 ctx.fail(self.get_missing_message(ctx))1270 raise MissingParameter(ctx=ctx, param=self)12711272 return value12731266 def get_missing_message(self, ctx):1267 rv = 'Missing %s %s.' % (1268 self.param_type_name,1269 ' / '.join('"%s"' % x for x in chain(1270 self.opts, self.secondary_opts)),1271 )1272 extra = self.type.get_missing_message(self)1273 if extra:1274 rv += ' ' + extra1275 return rv12761274 def resolve_envvar_value(self, ctx):1275 if self.envvar is None:1276 return1277 if isinstance(self.envvar, (tuple, list)):
315 unmodified lines
1593 else:1594 required = attrs.get('nargs', 1) > 01595 Parameter.__init__(self, param_decls, required=required, **attrs)15961597 @property1598 def human_readable_name(self):1599 if self.metavar is not None:1600 return self.metavar1601 return self.name.upper()16021603 def make_metavar(self):1604 if self.metavar is not None:1605 return self.metavar1606 var = self.name.upper()
31 unmodified lines
modified:click/exceptions.py+51 added-4 removed
8 unmodified lines
9 exit_code = 11011 def __init__(self, message):12 if PY2:13 Exception.__init__(self, message.encode('utf-8'))14 else:15 Exception.__init__(self, message)13 if message is not None:14 message = message.encode('utf-8')15 Exception.__init__(self, message)16 self.message = message1718 def format_message(self):19 return self.message
55 unmodified lines
75 def format_message(self):76 if self.param_hint is not None:77 param_hint = self.param_hint78 elif self.param is not None:79 param_hint = self.param.opts or [self.param.name]79 param_hint = self.param.opts or [self.param.human_readable_name]80 else:81 return 'Invalid value: %s' % self.message82 if isinstance(param_hint, (tuple, list)):83 param_hint = ' / '.join('"%s"' % x for x in param_hint)84 return 'Invalid value for %s: %s' % (param_hint, self.message)858687class MissingParameter(BadParameter):88 """Raised if click required an option or argument but it was not89 provided when invoking the script.9091 .. versionadded:: 4.09293 :param param_type: a string that indicates the type of the parameter.94 The default is to inherit the parameter type from95 the given `param`. Valid values are ``'parameter'``,96 ``'option'`` or ``'argument'``.97 """9899 def __init__(self, message=None, ctx=None, param=None,100 param_hint=None, param_type=None):101 BadParameter.__init__(self, message, ctx, param, param_hint)102 self.param_type = param_type103104 def format_message(self):105 if self.param_hint is not None:106 param_hint = self.param_hint107 elif self.param is not None:108 param_hint = self.param.opts or [self.param.human_readable_name]109 else:110 param_hint = None111 if isinstance(param_hint, (tuple, list)):112 param_hint = ' / '.join('"%s"' % x for x in param_hint)113114 param_type = self.param_type115 if param_type is None and self.param is not None:116 param_type = self.param.param_type_name117118 msg = self.message119 msg_extra = self.param.type.get_missing_message(self.param)120 if msg_extra:121 if msg:122 msg += '. ' + msg_extra123 else:124 msg = msg_extra125126 return 'Missing %s%s%s%s' % (127 param_type,128 param_hint and ' %s' % param_hint or '',129 msg and '. ' or '.',130 msg,131 )132133134class NoSuchOption(UsageError):135 """Raised if click attempted to handle an option that does not136 exist.137
51 unmodified lines
modified:CHANGES+4 added
27 unmodified lines
28- Reject ``nargs=-1`` for options properly. Options cannot be variadic.29- Fixed an issue with bash completion not working properly for commands with30 non ASCII characters or dashes.31- Added a way to manually update the progressbar.32- Changed the formatting of missing arguments. Previously the internal33 argument name was shown in error messages, now the metavar is shown if34 passed. In case an automated metavar is selected, it's stripped of35 extra formatting first.3637Version 3.338-----------39
176 unmodified lines