Added support for multiple arguments.
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 9d027aa · Browse files · 2 files·+24 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+24 added−3 removed
- other:+24 added−3 removed
Provenance
- Commit
18fff34- Tree
915237a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
9d027aa
2 files changed+24 added−3 removed
modified:examples/repo/repo.py+9 added
68 unmodified lines69 print('Repo: %s' % repo)70 print('Username: %s' % username)71 print('E-Mail: %s' % email)72 print('Password: %s' % ('*' * len(password)))73+74+75+@cli.command()76+@click.option('--message', '-m', required=True, multiple=True)77+@pass_repo78+def commit(repo, message):79+ """Commits outstanding changes."""80+ print('Repo: %s' % repo)81+ print('\n'.join(message))modified:click.py+15 added-3 removed
979 unmodified lines980 self.required = required981 self.help = help982 self.callback = callback983 self.nargs = nargs984+ self.multiple = False985 self.expose_value = expose_value986 self.default = default987 self.is_eager = is_eager988 self.metavar = metavar23 unmodified lines1012 """Given a value and context this runs the logic to convert the1013 value as necessary.1014 """1015 if self.type is not None:1015- if self.nargs != 1 and value is not None:1016+ if (self.nargs != 1 or self.multiple) and value is not None:1017 value = tuple(self.type(x, self, ctx) for x in value)1018 else:1019 value = self.type(value, self, ctx)1020 return value59 unmodified lines1080 auto detection.1081 :param flag_value: which value should be used for this flag if it's1082 enabled. This is set to a boolean automatically if1083 the option string contains a slash to mark two options.1084+ :param multiple: if this is set to True then the argument is accepted1085+ multiple times and recorded. This is similar to nargs1086+ in how it works but supports arbitrary number of1087+ arguments.1088 :param allow_from_autoenv: if this is enabled then the value of this1089 parameter will be pulled from an environment1090 variable in case a prefix is defined on the1091 context.2 unmodified lines1094 1095 def __init__(self, param_decls=None, show_default=None,1096 prompt=False, confirmation_prompt=False,1097 hide_input=False, is_flag=None, flag_value=None,1093- allow_from_autoenv=True, type=None, **attrs):1098+ multiple=False, allow_from_autoenv=True, type=None,1099+ **attrs):1100 Parameter.__init__(self, param_decls, type=type, **attrs)1101 if prompt is True:1102 prompt = self.name.replace('_', ' ').capitalize()1103 elif prompt is False:18 unmodified lines1122 1123 self.show_default = show_default1124 self.is_flag = is_flag1125 self.flag_value = flag_value1126+ self.multiple = multiple1127 1128 if self.is_flag and isinstance(self.flag_value, bool) \1129 and type is None:1130 self.type = BoolParamType()4 unmodified lines1135 self.allow_from_autoenv = allow_from_autoenv1136 1137 # Sanity check for stuff we don't support1138 if __debug__:1139+ if self.multiple and self.is_bool_flag:1140+ raise TypeError('Boolean flags and multiple values are not '1141+ 'compatible.')1142 if self.prompt and self.is_flag and not self.is_bool_flag:1143 raise TypeError('Cannot prompt for flags that are not bools.')1144 if not self.is_bool_flag and self.secondary_opts:1145 raise TypeError('Got secondary option for non boolean flag.')65 unmodified lines1211 pos_opt._negative_version = neg_opt1212 parser.add_option(pos_opt)1213 parser.add_option(neg_opt)1214 else:1215+ action = self.multiple and 'append_const' or 'store_const'1205- parser.add_option(*self.opts, action='store_const',1216+ parser.add_option(*self.opts, action=action,1217 const=self.flag_value,1218 **kwargs)1219 else:1220+ kwargs['action'] = self.multiple and 'append' or 'store'1221 parser.add_option(*self.opts, **kwargs)1222 1223 def get_default(self, ctx):1224 # If we're a non boolean flag out default is more complex because406 unmodified lines