Skip to content
Myr

Deduplicate `--help` option definition (#2563)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kevin Deldycke · committed by GitHub · parent 9aeb586 · Browse files · 3 files+38 added−34 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Andreas Backx <andreas@backx.org>

Diff stats

3 files+38 added−34 removed

  • other:+38 added−34 removed

3 files+38 added−34 removed

modified:src/click/__init__.py+1 added
17 unmodified lines
18from .decorators import command as command19from .decorators import confirmation_option as confirmation_option20from .decorators import group as group21from .decorators import help_option as help_option22from .decorators import HelpOption as HelpOption23from .decorators import make_pass_decorator as make_pass_decorator24from .decorators import option as option25from .decorators import pass_context as pass_context26from .decorators import pass_obj as pass_obj
49 unmodified lines
modified:src/click/core.py+7 added-13 removed
1283 unmodified lines
1284 all_names.difference_update(param.secondary_opts)1285 return list(all_names)12861287 def get_help_option(self, ctx: Context) -> t.Optional["Option"]:1288 """Returns the help option object."""1288 """Returns the help option object.12891290 Unless ``add_help_option`` is ``False``.1291 """1292 help_options = self.get_help_option_names(ctx)12931294 if not help_options or not self.add_help_option:1295 return None12961294 def show_help(ctx: Context, param: "Parameter", value: str) -> None:1295 if value and not ctx.resilient_parsing:1296 echo(ctx.get_help(), color=ctx.color)1297 ctx.exit()1297 # Avoid circular import.1298 from .decorators import HelpOption12991299 return Option(1300 help_options,1301 is_flag=True,1302 is_eager=True,1303 expose_value=False,1304 callback=show_help,1305 help=_("Show this message and exit."),1306 )1300 return HelpOption(help_options)13011302 def make_parser(self, ctx: Context) -> OptionParser:1303 """Creates the underlying option parser for this command."""1304 parser = OptionParser(ctx)
1724 unmodified lines
modified:src/click/decorators.py+30 added-21 removed
520 unmodified lines
521 kwargs["callback"] = callback522 return option(*param_decls, **kwargs)523524525def help_option(*param_decls: str, **kwargs: t.Any) -> t.Callable[[FC], FC]:526 """Add a ``--help`` option which immediately prints the help page525class HelpOption(Option):526 """Pre-configured ``--help`` option which immediately prints the help page527 and exits the program.528 """529529 This is usually unnecessary, as the ``--help`` option is added to530 each command automatically unless ``add_help_option=False`` is531 passed.530 def __init__(531 self,532 param_decls: t.Optional[t.Sequence[str]] = None,533 **kwargs: t.Any,534 ) -> None:535 if not param_decls:536 param_decls = ("--help",)537533 :param param_decls: One or more option names. Defaults to the single534 value ``"--help"``.535 :param kwargs: Extra arguments are passed to :func:`option`.536 """538 kwargs.setdefault("is_flag", True)539 kwargs.setdefault("expose_value", False)540 kwargs.setdefault("is_eager", True)541 kwargs.setdefault("help", _("Show this message and exit."))542 kwargs.setdefault("callback", self.show_help)543538 def callback(ctx: Context, param: Parameter, value: bool) -> None:539 if not value or ctx.resilient_parsing:540 return544 super().__init__(param_decls, **kwargs)545546 @staticmethod547 def show_help(ctx: Context, param: Parameter, value: bool) -> None:548 """Callback that print the help page on ``<stdout>`` and exits."""549 if value and not ctx.resilient_parsing:542 echo(ctx.get_help(), color=ctx.color)543 ctx.exit()550 echo(ctx.get_help(), color=ctx.color)551 ctx.exit()552545 if not param_decls:546 param_decls = ("--help",)553548 kwargs.setdefault("is_flag", True)549 kwargs.setdefault("expose_value", False)550 kwargs.setdefault("is_eager", True)551 kwargs.setdefault("help", _("Show this message and exit."))552 kwargs["callback"] = callback554def help_option(*param_decls: str, **kwargs: t.Any) -> t.Callable[[FC], FC]:555 """Decorator for the pre-configured ``--help`` option defined above.556557 :param param_decls: One or more option names. Defaults to the single558 value ``"--help"``.559 :param kwargs: Extra arguments are passed to :func:`option`.560 """561 kwargs.setdefault("cls", HelpOption)562 return option(*param_decls, **kwargs)