More modernization in _optparse
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 141a692 · Browse files · 1 file·+19 added−62 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+19 added−62 removed
- other:+19 added−62 removed
Provenance
- Commit
5ffa7c8- Tree
293be2c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
141a692
1 file changed+19 added−62 removed
modified:click/_optparse.py+19 added-62 removed
115 unmodified lines116 self.help_position = min(max_len + 2, self.max_help_position)117 self.help_width = max(self.width - self.help_position, 11)118 119 def format_option_strings(self, option):120- if option.takes_value():120+ if option.takes_value:121 metavar = option.metavar or option.dest.upper()122 short_opts = [self._short_opt_fmt % (sopt, metavar)123 for sopt in option._short_opts]124 long_opts = [self._long_opt_fmt % (lopt, metavar)37 unmodified lines162 163 def __init__(self, *opts, **attrs):164 self._short_opts = []165 self._long_opts = []166- self._set_opt_strings(self._check_opt_strings(opts))167- self._set_attrs(attrs)168- self._check_attrs()166 170- def _check_attrs(self):171- if self.action is None:172- self.action = 'store'173- elif self.action not in self.ACTIONS:174- raise TypeError('invalid action: %r' % self.action)175-176- takes_value = (self.action in self.STORE_ACTIONS or177- self.takes_value)178- if self.dest is None and takes_value:179- # Glean a destination from the first long option string,180- # or from the first short option string if no long options.181- if self._long_opts:182- # eg. '--foo-bar' -> 'foo_bar'183- self.dest = self._long_opts[0][2:].replace('-', '_')184- else:185- self.dest = self._short_opts[0][1]186-187- if self.action in self.TYPED_ACTIONS:188- if self.nargs is None:189- self.nargs = 1190- elif self.nargs is not None:191- raise TypeError(192- '\'nargs\' must not be supplied for action %r' % self.action)193-194- def _check_opt_strings(self, opts):195- # Filter out None because early versions of Optik had exactly196- # one short option and one long option, either of which197- # could be None.198- opts = [opt for opt in opts if opt]199- if not opts:200- raise TypeError('at least one option string must be supplied')201- return opts202-203- def _set_opt_strings(self, opts):167 for opt in opts:168 if len(opt) < 2:169 raise TypeError(170 'invalid option string %r: '4 unmodified lines175 'invalid short option string %r: '176 'must be of the form -x, (x any non-dash char)' % opt)177 self._short_opts.append(opt)178 else:216- if not (opt[0:2] == '--' and opt[2] != '-'):179+ if not (opt[:2] == '--' and opt[2] != '-'):180 raise TypeError(181 'invalid long option string %r: '182 'must start with --, followed by non-dash' % opt)183 self._long_opts.append(opt)184 222- def _set_attrs(self, attrs):185 for attr in self.ATTRS:186 if attr in attrs:187 setattr(self, attr, attrs[attr])188 del attrs[attr]3 unmodified lines192 attrs = sorted(attrs.keys())193 raise TypeError('invalid keyword arguments: %s' %194 ', '.join(attrs))195 234- def __str__(self):235- return '/'.join(self._short_opts + self._long_opts)196+ if self.action is None:197+ self.action = 'store'198+ if self.action in self.TYPED_ACTIONS:199+ if self.nargs is None:200+ self.nargs = 1201 202+ @property203 def takes_value(self):204 return self.action in self.TAKES_VALUE_ACTIONS205 206 def get_opt_string(self):207 if self._long_opts:208 return self._long_opts[0]243- else:244- return self._short_opts[0]209+ return self._short_opts[0]210 211 def process(self, opt, value, values, parser):247- # And then take whatever action is expected of us.248- # This is a separate method to make life easier for249- # subclasses to add new actions.250- return self.take_action(251- self.action, self.dest, opt, value, values, parser)252-253- def take_action(self, action, dest, opt, value, values, parser):254- if action == 'store':255- values[dest] = value256- elif action == 'store_const':257- values[dest] = self.const258- elif action == 'append':259- values.setdefault(dest, []).append(value)260- elif action == 'append_const':261- values.setdefault(dest, []).append(self.const)212+ if self.action == 'store':213+ values[self.dest] = value214+ elif self.action == 'store_const':215+ values[self.dest] = self.const216+ elif self.action == 'append':217+ values.setdefault(self.dest, []).append(value)218+ elif self.action == 'append_const':219+ values.setdefault(self.dest, []).append(self.const)220 else:221 raise ValueError('unknown action %r' % self.action)264- return 1222 223 224 SUPPRESS_HELP = object()225 212 unmodified lines438 had_explicit_value = False439 440 opt = self._match_long_opt(opt)441 option = self._long_opt[opt]485- if option.takes_value():442+ if option.takes_value:443 nargs = option.nargs444 if len(rargs) < nargs:445 if nargs == 1:446 self.error('%s option requires an argument' % opt)23 unmodified lines470 i += 1 # we have consumed a character471 472 if not option:473 self.fail('no such option: %s' % opt)517- if option.takes_value():474+ if option.takes_value:475 # Any characters left in arg? Pretend they're the476 # next arg, and stop consuming characters of arg.477 if i < len(arg):478 rargs.insert(0, arg[i:])120 unmodified lines