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
Provenance
- Commit
64f57fe- Tree
bea05ee- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
46b22d0
4 files changed+74 added−20 removed
modified:click/__init__.py+2 added-2 removed
36 unmodified lines37 pause38 39 # Exceptions40 from .exceptions import ClickException, UsageError, BadParameter, \41- FileError, Abort, NoSuchOption, BadOptionUsage41+ FileError, Abort, NoSuchOption, BadOptionUsage, MissingParameter42 43 # Formatting44 from .formatting import HelpFormatter, wrap_text45 25 unmodified lines71 'getchar', 'pause',72 73 # Exceptions74 'ClickException', 'UsageError', 'BadParameter', 'FileError',75- 'Abort', 'NoSuchOption', 'BadOptionUsage',75+ 'Abort', 'NoSuchOption', 'BadOptionUsage', 'MissingParameter',76 77 # Formatting78 'HelpFormatter', 'wrap_text',79 6 unmodified linesmodified:click/core.py+17 added-14 removed
1 import os2 import sys3 import codecs4 from contextlib import contextmanager5-from itertools import chain, repeat5+from itertools import repeat6 from functools import update_wrapper7 8 from .types import convert_type, IntRange, BOOL9 from .utils import make_str, make_default_short_help, echo10-from .exceptions import ClickException, UsageError, BadParameter, Abort10+from .exceptions import ClickException, UsageError, BadParameter, Abort, \11+ MissingParameter12 from .termui import prompt, confirm13 from .formatting import HelpFormatter, join_options14 from .parser import OptionParser, split_opt15 1178 unmodified lines1194 self.is_eager = is_eager1195 self.metavar = metavar1196 self.envvar = envvar1197 1198+ @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.name1204+1205 def make_metavar(self):1206 if self.metavar is not None:1207 return self.metavar1208 metavar = self.type.get_metavar(self)57 unmodified lines1266 if value is None:1267 value = self.get_default(ctx)1268 1269 if self.required and self.value_is_missing(value):1262- ctx.fail(self.get_missing_message(ctx))1270+ raise MissingParameter(ctx=ctx, param=self)1271 1272 return value1273 1266- 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 rv1276-1274 def resolve_envvar_value(self, ctx):1275 if self.envvar is None:1276 return1277 if isinstance(self.envvar, (tuple, list)):315 unmodified lines1593 else:1594 required = attrs.get('nargs', 1) > 01595 Parameter.__init__(self, param_decls, required=required, **attrs)1596 1597+ @property1598+ def human_readable_name(self):1599+ if self.metavar is not None:1600+ return self.metavar1601+ return self.name.upper()1602+1603 def make_metavar(self):1604 if self.metavar is not None:1605 return self.metavar1606 var = self.name.upper()31 unmodified linesmodified:click/exceptions.py+51 added-4 removed
8 unmodified lines9 exit_code = 110 11 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 = message17 18 def format_message(self):19 return self.message55 unmodified lines75 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)85 86 87+class MissingParameter(BadParameter):88+ """Raised if click required an option or argument but it was not89+ provided when invoking the script.90+91+ .. versionadded:: 4.092+93+ :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+ """98+99+ 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_type103+104+ 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)113+114+ param_type = self.param_type115+ if param_type is None and self.param is not None:116+ param_type = self.param.param_type_name117+118+ 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_extra125+126+ return 'Missing %s%s%s%s' % (127+ param_type,128+ param_hint and ' %s' % param_hint or '',129+ msg and '. ' or '.',130+ msg,131+ )132+133+134 class NoSuchOption(UsageError):135 """Raised if click attempted to handle an option that does not136 exist.137 51 unmodified linesmodified:CHANGES+4 added
27 unmodified lines28 - 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.36 37 Version 3.338 -----------39 176 unmodified lines