Added choice 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 84fbcb2 · Browse files · 5 files·+67 added−1 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+67 added−1 removed
- other:+67 added−1 removed
Provenance
- Commit
9eacc90- Tree
27b63e7- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
84fbcb2
5 files changed+67 added−1 removed
modified:docs/api.rst+2 added
79 unmodified lines80 .. autodata:: BOOL81 82 .. autoclass:: File83 84+.. autoclass:: Choice85+86 .. autoclass:: ParamType87 88 Exceptions89 ----------4 unmodified linesmodified:docs/arguments.rst+2 added
50 unmodified lines51 $ python touch.py foo.txt bar.txt52 foo.txt53 bar.txt54 55+.. _file-args:56+57 File Arguments58 --------------59 60 Since all the examples have already worked with file names it makes sense56 unmodified linesmodified:docs/options.rst+33 added
164 unmodified lines165 darwin166 $ python info.py167 DARWIN168 169+.. _choice-opts:170+171+Choice Options172+--------------173+174+Sometimes you want to have a parameter be a choice of a list of values.175+In that case you can use :class:`Choice` type. It can be instanciated176+with a list of valid values.177+178+Example::179+180+ @click.command()181+ @click.option('--hash-type', type=click.Choice(['md5', 'sha1']))182+ def digest(hash_type):183+ print(hash_type)184+185+What it looks like::186+187+ $ python digest.py --hash-type=md5188+ md5189+190+ $ python digest.py --hash-type=foo191+ Usage: digest.py [OPTIONS]192+193+ Error: Invalid value for hash_type: invalid choice: foo. (chose from md5, sha1)194+195+ $ python digest.py --help196+ Usage: digest.py [OPTIONS]197+198+ Options:199+ --hash-type=[md5|sha1]200+ --help Show this message and exit.201+202 .. _option-prompting:203 204 Prompting205 ---------200 unmodified linesmodified:docs/parameters.rst+5 added-1 removed
51 unmodified lines52 convert to `False`.53 54 :class:`click.File`:55 A parameter that is an opened file. It can be opened for reading or56- writing.56+ writing. See :ref:`file-args` for an example.57 58+:class:`click.Choice`:59+ A parameter that represents a choice of something. See60+ :ref:`choice-opts` for an example.61+62 Custom parameter types can be implemented by subclassing63 :class:`click.ParamType`. For simple cases passing a python function that64 fails with a `ValueError` is also supported though discouraged.65 47 unmodified linesmodified:click.py+25 added
760 unmodified lines761 def __call__(self, value, param=None, ctx=None):762 if value is not None:763 return self.convert(value, param, ctx)764 765+ def get_metavar(self, param):766+ """Returns the metavar default for this param if it provides one."""767+768 def convert(self, param, ctx, value):769 """Converts the value. This is not invoked for values that are770 `None` (the missing value).771 """44 unmodified lines816 def __repr__(self):817 return 'STRING'818 819 820+class Choice(ParamType):821+ name = 'choice'822+823+ def __init__(self, choices):824+ self.choices = choices825+826+ def get_metavar(self, param):827+ return '[%s]' % '|'.join(self.choices)828+829+ def convert(self, value, param, ctx):830+ if value in self.choices:831+ return value832+ self.fail('invalid choice: %s. (chose from %s)' %833+ (value, ', '.join(self.choices)), param, ctx)834+835+ def __repr__(self):836+ return 'Choice(%r)' % list(self.choices)837+838+839 class IntParamType(ParamType):840 name = 'integer'841 842 def convert(self, value, param, ctx):169 unmodified lines1012 1013 def make_metavar(self):1014 if self.metavar is not None:1015 return self.metavar1016+ metavar = self.type.get_metavar(self)1017+ if metavar is not None:1018+ return metavar1019 metavar = self.name.upper()1020 if self.nargs != 1:1021 metavar += '...'1022 return metavar629 unmodified lines