Corrected a help option regression.
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 0999c78 · Browse files · 2 files·+30 added−45 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+30 added−45 removed
- other:+30 added−45 removed
Provenance
- Commit
6ab7b03- Tree
194629a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
0999c78
2 files changed+30 added−45 removed
modified:click/core.py+28 added-45 removed
534 unmodified lines535 formatter = ctx.make_formatter()536 self.format_usage(ctx, formatter)537 return formatter.getvalue().rstrip('\n')538 539+ def get_params(self, ctx):540+ rv = self.params541+ help_option = self.get_help_option(ctx)542+ if help_option is not None:543+ rv = rv + [help_option]544+ return rv545+546 def format_usage(self, ctx, formatter):547 """Writes the usage line into the formatter."""548 pieces = self.collect_usage_pieces(ctx)549 formatter.write_usage(ctx.command_path, ' '.join(pieces))2 unmodified lines552 """Returns all the pieces that go into the usage line and returns553 it as a list of strings.554 """555 rv = [self.options_metavar]549- for param in self.params:556+ for param in self.get_params(ctx):557 rv.extend(param.get_usage_pieces(ctx))558 return rv559 560 def get_help_option_names(self, ctx):3 unmodified lines564 all_names.difference_update(param.opts)565 all_names.difference_update(param.secondary_opts)566 return all_names567 568+ def get_help_option(self, ctx):569+ """Returns the help option object."""570+ help_options = self.get_help_option_names(ctx)571+ if not help_options:572+ return573+574+ def show_help(ctx, param, value):575+ if value and not ctx.resilient_parsing:576+ echo(ctx.get_help())577+ ctx.exit()578+ return Option(help_options, is_flag=True,579+ is_eager=True, expose_value=False,580+ callback=show_help,581+ help='Show this message and exit.')582+583 def make_parser(self, ctx):584 """Creates the underlying option parser for this command."""585 parser = OptionParser(ctx)564- for param in self.params:586+ for param in self.get_params(ctx):587 param.add_to_parser(parser, ctx)566-567- if self.add_help_option:568- help_options = self.get_help_option_names(ctx)569- if help_options:570- parser.add_option(help_options, action='store_const',571- const=True, dest='$help')572-588 return parser589 590 def get_help(self, ctx):591 """Formats the help into a string and returns it. This creates a27 unmodified lines619 620 def format_options(self, ctx, formatter):621 """Writes all the options into the formatter if they exist."""622 opts = []608- for param in self.params:623+ for param in self.get_params(ctx):624 rv = param.get_help_record(ctx)625 if rv is not None:626 opts.append(rv)627 613- if self.add_help_option:614- help_options = self.get_help_option_names(ctx)615- if help_options:616- opts.append([join_options(help_options)[0],617- 'Show this message and exit.'])618-628 if opts:629 with formatter.section('Options'):630 formatter.write_dl(opts)631 7 unmodified lines639 def parse_args(self, ctx, args):640 parser = self.make_parser(ctx)641 opts, args, param_order = parser.parse_args(args=args)642 634- # XXX: this logic requires a bit of explanation. As a change in635- # 2.x we accidentally broke a pattern used in 1.x which was636- # to customize the commands by passing more arguments. Because637- # the help option however was moved away from regular handling638- # to the front it meant that other arguments can no longer be639- # handled at all. The workaround for 2.x is that we try this:640- #641- # 1. start with regular handling. If a click exception happens642- # we show the help instead if wanted.643- # 2. if every callback went through we try to show the help now.644- #645- # This restores the old behavior in practical terms while still646- # special casing the help.647- #648- # Maybe this should be done differently?643+ for param in iter_params_for_processing(644+ param_order, self.get_params(ctx)):645+ value, args = param.handle_parse_result(ctx, opts, args)646 650- def _try_show_help():651- if opts.get('$help') and not ctx.resilient_parsing:652- echo(ctx.get_help())653- ctx.exit()654-655- try:656- for param in iter_params_for_processing(param_order, self.params):657- value, args = param.handle_parse_result(ctx, opts, args)658- except ClickException:659- _try_show_help()660- raise661- else:662- _try_show_help()663-647 if args and not self.allow_extra_args and not ctx.resilient_parsing:648 ctx.fail('Got unexpected extra argument%s (%s)'649 % (len(args) != 1 and 's' or '',650 ' '.join(map(make_str, args))))687 unmodified linesmodified:CHANGES+2 added
6 unmodified lines7 -----------8 9 (bugfix release, release date to be decided)10 11+- Corrected a bug in the change of the help option in 2.3.12+13 Version 2.314 -----------15 16 (bugfix release, released on July 3rd 2014)88 unmodified lines