From b77007d3c8f9b3e18de83845d0f84176361cb9c1 Mon Sep 17 00:00:00 2001 From: Kevin Deldycke Date: Thu, 9 Jan 2025 16:41:45 -0800 Subject: [PATCH] Set --help/-h as defaults for @help_option decorator Remove custom HelpOption class. Refs: https://github.com/pallets/click/pull/2563 https://github.com/pallets/click/issues/2832 https://github.com/pallets/click/pull/2840 --- changelog.md | 3 +++ click_extra/__init__.py | 1 - click_extra/colorize.py | 17 ----------------- click_extra/commands.py | 13 +++++++------ click_extra/decorators.py | 26 +++++++++++++++++++++----- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/changelog.md b/changelog.md index e659528cc..6f9dfbe26 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,9 @@ > [!IMPORTANT] > This version is not released yet and is under active development. +- Remove Click Extra's own implementation of `HelpOption` class now that fixes have reached Click's upstream. +- Redefine `@help_option` decorator to default to `--help`/`-h` options. + ## [4.11.7 (2024-11-30)](https://github.com/kdeldycke/click-extra/compare/v4.11.6...v4.11.7) - Remove support for comments in JSON configuration files. Remove dependency on unmaintained `commentjson`. Closes [`click-extra#1152`](https://github.com/kdeldycke/click-extra/issues/1152). diff --git a/click_extra/__init__.py b/click_extra/__init__.py index 42c98ff4f..dac83ba6b 100644 --- a/click_extra/__init__.py +++ b/click_extra/__init__.py @@ -40,7 +40,6 @@ ColorOption, HelpExtraFormatter, HelpExtraTheme, - HelpOption, ) from .commands import ( # noqa: E402 ExtraCommand, diff --git a/click_extra/colorize.py b/click_extra/colorize.py index ad409666e..a1b747785 100644 --- a/click_extra/colorize.py +++ b/click_extra/colorize.py @@ -291,23 +291,6 @@ def __init__( ) -class HelpOption(click.HelpOption): - """Same defaults as Click's @help_option but with ``-h`` short option. - - See: https://github.com/pallets/click/blob/934813e/src/click/decorators.py#L536 - """ - - def __init__( - self, - param_decls: Sequence[str] | None = None, - **kwargs, - ) -> None: - if not param_decls: - param_decls = ("--help", "-h") - - super().__init__(param_decls, **kwargs) - - class ExtraHelpColorsMixin: # (Command)?? """Adds extra-keywords highlighting to Click commands. diff --git a/click_extra/commands.py b/click_extra/commands.py index 2f4824c63..c5ed036e7 100644 --- a/click_extra/commands.py +++ b/click_extra/commands.py @@ -150,12 +150,13 @@ def default_extra_params() -> list[Option]: #. ``--version`` #. ``-h``, ``--help`` .. attention:: - This is an instance of `Click Extra's own HelpOption - `_. - It is not explicitly added in the implementation of this function. + This is an instance of `click.decorators.HelpOption + `_. - That's because it's `going to be added by Click itself - `_, + It is not explicitly referenced in the implementation of this function. + + That's because it's `going to be appended by Click itself + `_, at the end of the list of options. By letting Click handle this, we ensure that the help option will take into account the `help_option_names `_ @@ -181,7 +182,7 @@ def default_extra_params() -> list[Option]: ShowParamsOption(), VerbosityOption(), ExtraVersionOption(), - # HelpOption(), + # click.decorators.HelpOption(), ] diff --git a/click_extra/decorators.py b/click_extra/decorators.py index fb23f48b3..1aa76e455 100644 --- a/click_extra/decorators.py +++ b/click_extra/decorators.py @@ -17,9 +17,10 @@ from functools import wraps +import click import cloup -from .colorize import ColorOption, HelpOption +from .colorize import ColorOption from .commands import ExtraCommand, ExtraGroup, default_extra_params from .config import ConfigOption from .logging import VerbosityOption @@ -46,7 +47,7 @@ def new_factory(*args, **kwargs): return new_factory -def decorator_factory(dec, **new_defaults): +def decorator_factory(dec, *new_args, **new_defaults): """Clone decorator with a set of new defaults. Used to create our own collection of decorators for our custom options, based on @@ -64,6 +65,9 @@ def decorator(*args, **kwargs): This decorator can be used with or without arguments. """ + if not args: + args = new_args + # Use a copy of the defaults to avoid modifying the original dict. new_kwargs = new_defaults.copy() new_kwargs.update(kwargs) @@ -80,11 +84,24 @@ def decorator(*args, **kwargs): return decorator -# Redefine cloup decorators to allow them to be used with or without parenthesis. +# Redefine Cloup decorators to allow them to be used with or without parenthesis. command = decorator_factory(dec=cloup.command) group = decorator_factory(dec=cloup.group) -# Extra-prefixed decorators augments the default Cloup and Click ones. +# Customize existing Click decorators with better default parameters. +help_option = decorator_factory( + # XXX parameters are not named because of the way the default option names are + # passed to HelpOption. + # TODO: try the following instead once https://github.com/pallets/click/pull/2840 + # is merged: + # dec=click.decorators.help_option, + # param_decls=("--help", "-h"), + click.decorators.help_option, + "--help", + "-h", +) + +# Copy Click and Cloup decorators with better defaults, and prefix them with "extra_". extra_command = decorator_factory( dec=cloup.command, cls=ExtraCommand, @@ -100,7 +117,6 @@ def decorator(*args, **kwargs): # New option decorators provided by Click Extra. color_option = decorator_factory(dec=cloup.option, cls=ColorOption) config_option = decorator_factory(dec=cloup.option, cls=ConfigOption) -help_option = decorator_factory(dec=cloup.option, cls=HelpOption) show_params_option = decorator_factory(dec=cloup.option, cls=ShowParamsOption) table_format_option = decorator_factory(dec=cloup.option, cls=TableFormatOption) telemetry_option = decorator_factory(dec=cloup.option, cls=TelemetryOption)