Skip to content

Commit eca0156

Browse files
authored
Fix the help text of commands that have dynamic parameters (#47)
1 parent adf3b34 commit eca0156

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1111
***Fixed:***
1212

1313
- Fix escaping on the non-Windows entry points of prebuilt distributions
14+
- Fix the help text of commands that have dynamic parameters
1415

1516
## 0.6.1 - 2025-03-10
1617

src/dda/cli/base.py

+28
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__(
6464

6565
self._features = features
6666
self._dependencies = dependencies
67+
self.__help_option: click.Option | None = None
6768

6869
def get_params(self, ctx: click.Context) -> list[click.Parameter]:
6970
# https://github.com/pallets/click/pull/2784
@@ -85,6 +86,33 @@ def invoke(self, ctx: click.Context) -> Any:
8586

8687
return super().invoke(ctx)
8788

89+
def get_help_option(self, ctx: click.Context) -> click.Option | None:
90+
if self.__help_option is not None:
91+
return self.__help_option
92+
93+
help_option = super().get_help_option(ctx)
94+
if help_option is not None:
95+
original_callback = help_option.callback
96+
97+
all_callbacks_executed = False
98+
99+
def callback(ctx: click.Context, param: click.Parameter, value: Any) -> None:
100+
nonlocal all_callbacks_executed
101+
if not all_callbacks_executed:
102+
# Callbacks for other parameters may influence the help text
103+
for other_param in ctx.command.get_params(ctx):
104+
if other_param is not param and other_param.callback is not None:
105+
other_param.callback(ctx, other_param, None)
106+
all_callbacks_executed = True
107+
108+
if original_callback is not None:
109+
original_callback(ctx, param, value)
110+
111+
help_option.callback = callback
112+
self.__help_option = help_option
113+
114+
return self.__help_option
115+
88116

89117
class DynamicGroup(click.RichGroup):
90118
context_class = DynamicContext

src/dda/cli/env/dev/utils.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ def get_env_type(ctx: click.Context, param: click.Option, value: str | None) ->
2424
return env_type
2525

2626

27-
def option_env_type_callback(ctx: click.Context, param: click.Option, value: str | None) -> str:
28-
from dda.cli.env.dev.utils import get_env_type
29-
30-
return get_env_type(ctx, param, value)
31-
32-
3327
option_env_type = partial(
3428
click.option,
3529
"--type",
@@ -38,6 +32,6 @@ def option_env_type_callback(ctx: click.Context, param: click.Option, value: str
3832
type=click.Choice(AVAILABLE_DEV_ENVS),
3933
show_default=True,
4034
is_eager=True,
41-
callback=option_env_type_callback,
35+
callback=get_env_type,
4236
help="The type of developer environment",
4337
)

src/dda/cli/inv/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"--help",
4949
"show_help",
5050
is_flag=True,
51-
help="Show this help message and exit",
51+
help="Show this help message and exit.",
5252
)
5353
@click.pass_context
5454
def cmd(

0 commit comments

Comments
 (0)