Skip to content

Commit f71cb63

Browse files
committed
Fix pulp --help not showing available commands
fixes: #1267 Assisted by: claude-sonnet4
1 parent 89ef700 commit f71cb63

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

CHANGES/1267.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed `pulp --help` not showing all available commands.

src/pulp_cli/__init__.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pulp_glue.common.i18n import get_translation
1111

1212
from pulp_cli.config import CONFIG_LOCATIONS, config, config_options, validate_config
13-
from pulp_cli.generic import PulpCLIContext, pulp_group
13+
from pulp_cli.generic import PulpCLIContext, PulpGroup
1414

1515
if sys.version_info >= (3, 11):
1616
import tomllib
@@ -60,6 +60,23 @@ def load_plugins(
6060
PROFILE_KEY = f"{__name__}.profile"
6161

6262

63+
class MainPulpGroup(PulpGroup):
64+
"""PulpGroup subclass for the main CLI entry point.
65+
66+
Ensures plugins are loaded before help is displayed. This is needed because
67+
Click's ``iter_params_for_processing`` sorts eager parameters by invocation
68+
order, so ``--help`` (which is on the command line) gets processed before
69+
``--config`` and ``--profile`` (which are not). This causes ``--help`` to
70+
exit before the plugin-loading callbacks ever fire, resulting in a help page
71+
that only shows directly-registered commands.
72+
"""
73+
74+
def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
75+
if PLUGIN_KEY not in ctx.meta:
76+
ctx.meta[PLUGIN_KEY] = load_plugins()
77+
super().format_help(ctx, formatter)
78+
79+
6380
# config and config-profile need to be combined in order to fetch the desired defaults.
6481
# Click will call both these callbacks exactly once, but we cannot know the order.
6582
# Therefore whichever one has seen the that the other deposited it's value will coninue with
@@ -183,7 +200,7 @@ def _version_callback(ctx: click.Context, param: t.Any, value: bool) -> None:
183200
),
184201
)
185202
@config_options
186-
@pulp_group(no_args_is_help=False)
203+
@click.group(cls=MainPulpGroup, no_args_is_help=False)
187204
@click.pass_context
188205
def main(
189206
ctx: click.Context,

tests/test_help_pages.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ def test_accessing_the_help_page_does_not_invoke_api(
6868
assert result.stdout.startswith("Usage:") or result.stdout.startswith("DeprecationWarning:")
6969

7070

71+
@pytest.mark.help_page(base_cmd=[])
72+
def test_help_shows_all_available_commands(
73+
no_api: None,
74+
args: list[str],
75+
) -> None:
76+
runner = CliRunner()
77+
result = runner.invoke(main, args + ["--help"], catch_exceptions=False)
78+
assert result.exit_code == 0
79+
for command in main.commands.keys():
80+
assert command in result.stdout
81+
82+
7183
@pytest.mark.parametrize(
7284
"command,options",
7385
[

0 commit comments

Comments
 (0)