Skip to content

Commit

Permalink
Set --help/-h as defaults for @help_option decorator
Browse files Browse the repository at this point in the history
Remove custom HelpOption class.

Refs:
pallets/click#2563
pallets/click#2832
pallets/click#2840
  • Loading branch information
kdeldycke committed Jan 10, 2025
1 parent 3f79d02 commit b77007d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 0 additions & 1 deletion click_extra/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
ColorOption,
HelpExtraFormatter,
HelpExtraTheme,
HelpOption,
)
from .commands import ( # noqa: E402
ExtraCommand,
Expand Down
17 changes: 0 additions & 17 deletions click_extra/colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 7 additions & 6 deletions click_extra/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,13 @@ def default_extra_params() -> list[Option]:
#. ``--version``
#. ``-h``, ``--help``
.. attention::
This is an instance of `Click Extra's own HelpOption
<https://kdeldycke.github.io/click-extra/colorize.html#click_extra.colorize.HelpOption>`_.
It is not explicitly added in the implementation of this function.
This is an instance of `click.decorators.HelpOption
<https://click.palletsprojects.com/en/stable/api/#click.help_option>`_.
That's because it's `going to be added by Click itself
<https://github.com/pallets/click/blob/874ca2b/src/click/core.py#L1257>`_,
It is not explicitly referenced in the implementation of this function.
That's because it's `going to be appended by Click itself
<https://github.com/pallets/click/blob/934813e/src/click/core.py#L1262-L1265>`_,
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
<https://click.palletsprojects.com/en/stable/documentation/#help-parameter-customization>`_
Expand All @@ -181,7 +182,7 @@ def default_extra_params() -> list[Option]:
ShowParamsOption(),
VerbosityOption(),
ExtraVersionOption(),
# HelpOption(),
# click.decorators.HelpOption(),
]


Expand Down
26 changes: 21 additions & 5 deletions click_extra/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit b77007d

Please sign in to comment.