Skip to content
Myr

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

5 files+48 added−2 removed

modified:click/__init__.py+2 added-2 removed
22 unmodified lines
23 command, group, argument, option, confirmation_option, \24 password_option, version_option, help_option2526# Types27from .types import ParamType, File, Choice, STRING, INT, FLOAT, BOOL27from .types import ParamType, File, Choice, STRING, INT, FLOAT, BOOL, UUID2829# Helper functions30from .helpers import prompt, confirm, get_terminal_size, echo31
14 unmodified lines
46 'argument', 'option', 'confirmation_option', 'password_option',47 'version_option', 'help_option',4849 # Types50 'ParamType', 'File', 'Choice', 'STRING', 'INT', 'FLOAT', 'BOOL',50 'ParamType', 'File', 'Choice', 'STRING', 'INT', 'FLOAT', 'BOOL', 'UUID',5152 # Helper functions53 'prompt', 'confirm', 'get_terminal_size', 'echo',54
6 unmodified lines
modified:click/types.py+17 added
1import sys2import uuid34from ._compat import open_stream, text_type5from .exceptions import UsageError6from .utils import safecall
141 unmodified lines
148 def __repr__(self):149 return 'FLOAT'150151152class UUIDParameterType(ParamType):153 name = 'uuid'154155 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)160161 def __repr__(self):162 return 'UUID'163164165class 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 lines
252253#: A boolean parameter. This is the default for boolean flags. This can254#: also be selected by using ``bool`` as a type.255BOOL = BoolParamType()256257#: A UUID parameter.258UUID = UUIDParameterType()
modified:docs/api.rst+2 added
82 unmodified lines
83.. autodata:: FLOAT8485.. autodata:: BOOL8687.. autodata:: UUID8889.. autoclass:: File9091.. autoclass:: Choice92
16 unmodified lines
modified:docs/parameters.rst+4 added
49 unmodified lines
50 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`.5354:data:`click.UUID`:55 A parameter that accepts UUID values. This is not automatically56 guessed but represented as :class:`uuid.UUID`.5758: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 lines
modified:tests/test_basic.py+23 added
1# -*- coding: utf-8 -*-2import uuid3import click456def test_basic_functionality(runner):
86 unmodified lines
93 assert 'Invalid value for "--foo": bar is not a valid integer' \94 in result.output959697def 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)104105 result = runner.invoke(cli, [])106 assert not result.exception107 assert 'U:[ba122011-349f-423b-873b-9d6a79c688ab]' in result.output108109 result = runner.invoke(cli, ['--u=821592c1-c50e-4971-9cd6-e89dc6832f86'])110 assert not result.exception111 assert 'U:[821592c1-c50e-4971-9cd6-e89dc6832f86]' in result.output112113 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.output117118119def test_float_option(runner):120 @click.command()121 @click.option('--foo', default=42, type=click.FLOAT)122 def cli(foo):
67 unmodified lines