Added UUID type support
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 052ee21 · Browse files · 5 files·+48 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+48 added−2 removed
- tests:+23 added−0 removed
- other:+25 added−2 removed
Provenance
- Commit
4ed961d- Tree
a637ee8- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
052ee21
5 files changed+48 added−2 removed
modified:click/__init__.py+2 added-2 removed
22 unmodified lines23 command, group, argument, option, confirmation_option, \24 password_option, version_option, help_option25 26 # Types27-from .types import ParamType, File, Choice, STRING, INT, FLOAT, BOOL27+from .types import ParamType, File, Choice, STRING, INT, FLOAT, BOOL, UUID28 29 # Helper functions30 from .helpers import prompt, confirm, get_terminal_size, echo31 14 unmodified lines46 'argument', 'option', 'confirmation_option', 'password_option',47 'version_option', 'help_option',48 49 # Types50- 'ParamType', 'File', 'Choice', 'STRING', 'INT', 'FLOAT', 'BOOL',50+ 'ParamType', 'File', 'Choice', 'STRING', 'INT', 'FLOAT', 'BOOL', 'UUID',51 52 # Helper functions53 'prompt', 'confirm', 'get_terminal_size', 'echo',54 6 unmodified linesmodified:click/types.py+17 added
1 import sys2+import uuid3 4 from ._compat import open_stream, text_type5 from .exceptions import UsageError6 from .utils import safecall141 unmodified lines148 def __repr__(self):149 return 'FLOAT'150 151 152+class UUIDParameterType(ParamType):153+ name = 'uuid'154+155+ def convert(self, value, param, ctx):156+ try:157+ return uuid.UUID(value)158+ except ValueError:159+ self.fail('%s is not a valid UUID value' % value, param, ctx)160+161+ def __repr__(self):162+ return 'UUID'163+164+165 class File(ParamType):166 """Declares a parameter to be a file for reading or writing. The file167 is automatically closed once the context tears down (after the command168 finished working).83 unmodified lines252 253 #: A boolean parameter. This is the default for boolean flags. This can254 #: also be selected by using ``bool`` as a type.255 BOOL = BoolParamType()256+257+#: A UUID parameter.258+UUID = UUIDParameterType()modified:docs/api.rst+2 added
82 unmodified lines83 .. autodata:: FLOAT84 85 .. autodata:: BOOL86 87+.. autodata:: UUID88+89 .. autoclass:: File90 91 .. autoclass:: Choice92 16 unmodified linesmodified:docs/parameters.rst+4 added
49 unmodified lines50 for boolean flags. If used with string values ``1``, ``yes``, ``y``51 and ``true`` convert to `True` and ``0``, ``no``, ``n`` and ``false``52 convert to `False`.53 54+:data:`click.UUID`:55+ A parameter that accepts UUID values. This is not automatically56+ guessed but represented as :class:`uuid.UUID`.57+58 :class:`click.File`:59 A parameter that is an opened file. It can be opened for reading or60 writing. See :ref:`file-args` for an example.61 55 unmodified linesmodified:tests/test_basic.py+23 added
1 # -*- coding: utf-8 -*-2+import uuid3 import click4 5 6 def test_basic_functionality(runner):86 unmodified lines93 assert 'Invalid value for "--foo": bar is not a valid integer' \94 in result.output95 96 97+def test_uuid_option(runner):98+ @click.command()99+ @click.option('--u', default='ba122011-349f-423b-873b-9d6a79c688ab',100+ type=click.UUID)101+ def cli(u):102+ assert type(u) is uuid.UUID103+ click.echo('U:[%s]' % u)104+105+ result = runner.invoke(cli, [])106+ assert not result.exception107+ assert 'U:[ba122011-349f-423b-873b-9d6a79c688ab]' in result.output108+109+ result = runner.invoke(cli, ['--u=821592c1-c50e-4971-9cd6-e89dc6832f86'])110+ assert not result.exception111+ assert 'U:[821592c1-c50e-4971-9cd6-e89dc6832f86]' in result.output112+113+ result = runner.invoke(cli, ['--u=bar'])114+ assert result.exception115+ assert 'Invalid value for "--u": bar is not a valid UUID value' \116+ in result.output117+118+119 def test_float_option(runner):120 @click.command()121 @click.option('--foo', default=42, type=click.FLOAT)122 def cli(foo):67 unmodified lines