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
Provenance
- Commit
26aa7bf- Tree
0ac6b44- Author
Kevin Deldycke <kdeldycke@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
9aeb586
3 files changed+38 added−34 removed
modified:src/click/__init__.py+1 added
17 unmodified lines18 from .decorators import command as command19 from .decorators import confirmation_option as confirmation_option20 from .decorators import group as group21 from .decorators import help_option as help_option22+from .decorators import HelpOption as HelpOption23 from .decorators import make_pass_decorator as make_pass_decorator24 from .decorators import option as option25 from .decorators import pass_context as pass_context26 from .decorators import pass_obj as pass_obj49 unmodified linesmodified:src/click/core.py+7 added-13 removed
1283 unmodified lines1284 all_names.difference_update(param.secondary_opts)1285 return list(all_names)1286 1287 def get_help_option(self, ctx: Context) -> t.Optional["Option"]:1288- """Returns the help option object."""1288+ """Returns the help option object.1289+1290+ Unless ``add_help_option`` is ``False``.1291+ """1292 help_options = self.get_help_option_names(ctx)1293 1294 if not help_options or not self.add_help_option:1295 return None1296 1294- 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 HelpOption1299 1299- 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)1301 1302 def make_parser(self, ctx: Context) -> OptionParser:1303 """Creates the underlying option parser for this command."""1304 parser = OptionParser(ctx)1724 unmodified linesmodified:src/click/decorators.py+30 added-21 removed
520 unmodified lines521 kwargs["callback"] = callback522 return option(*param_decls, **kwargs)523 524 525-def help_option(*param_decls: str, **kwargs: t.Any) -> t.Callable[[FC], FC]:526- """Add a ``--help`` option which immediately prints the help page525+class HelpOption(Option):526+ """Pre-configured ``--help`` option which immediately prints the help page527 and exits the program.528+ """529 529- 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",)537 533- :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)543 538- def callback(ctx: Context, param: Parameter, value: bool) -> None:539- if not value or ctx.resilient_parsing:540- return544+ super().__init__(param_decls, **kwargs)545 546+ @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()552 545- if not param_decls:546- param_decls = ("--help",)553 548- 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"] = callback554+def help_option(*param_decls: str, **kwargs: t.Any) -> t.Callable[[FC], FC]:555+ """Decorator for the pre-configured ``--help`` option defined above.556+557+ :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)