Skip to content
Myr

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

5 files+67 added−1 removed

modified:docs/api.rst+2 added
79 unmodified lines
80.. autodata:: BOOL8182.. autoclass:: File8384.. autoclass:: Choice8586.. autoclass:: ParamType8788Exceptions89----------
4 unmodified lines
modified:docs/arguments.rst+2 added
50 unmodified lines
51 $ python touch.py foo.txt bar.txt52 foo.txt53 bar.txt5455.. _file-args:5657File Arguments58--------------5960Since all the examples have already worked with file names it makes sense
56 unmodified lines
modified:docs/options.rst+33 added
164 unmodified lines
165 darwin166 $ python info.py167 DARWIN168169.. _choice-opts:170171Choice Options172--------------173174Sometimes you want to have a parameter be a choice of a list of values.175In that case you can use :class:`Choice` type. It can be instanciated176with a list of valid values.177178Example::179180 @click.command()181 @click.option('--hash-type', type=click.Choice(['md5', 'sha1']))182 def digest(hash_type):183 print(hash_type)184185What it looks like::186187 $ python digest.py --hash-type=md5188 md5189190 $ python digest.py --hash-type=foo191 Usage: digest.py [OPTIONS]192193 Error: Invalid value for hash_type: invalid choice: foo. (chose from md5, sha1)194195 $ python digest.py --help196 Usage: digest.py [OPTIONS]197198 Options:199 --hash-type=[md5|sha1]200 --help Show this message and exit.201202.. _option-prompting:203204Prompting205---------
200 unmodified lines
modified:docs/parameters.rst+5 added-1 removed
51 unmodified lines
52 convert to `False`.5354: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.5758:class:`click.Choice`:59 A parameter that represents a choice of something. See60 :ref:`choice-opts` for an example.6162Custom parameter types can be implemented by subclassing63:class:`click.ParamType`. For simple cases passing a python function that64fails with a `ValueError` is also supported though discouraged.65
47 unmodified lines
modified:click.py+25 added
760 unmodified lines
761 def __call__(self, value, param=None, ctx=None):762 if value is not None:763 return self.convert(value, param, ctx)764765 def get_metavar(self, param):766 """Returns the metavar default for this param if it provides one."""767768 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 lines
816 def __repr__(self):817 return 'STRING'818819820class Choice(ParamType):821 name = 'choice'822823 def __init__(self, choices):824 self.choices = choices825826 def get_metavar(self, param):827 return '[%s]' % '|'.join(self.choices)828829 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)834835 def __repr__(self):836 return 'Choice(%r)' % list(self.choices)837838839class IntParamType(ParamType):840 name = 'integer'841842 def convert(self, value, param, ctx):
169 unmodified lines
10121013 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 metavar
629 unmodified lines