Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide sub-commands from top level help #74

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Types of changes are:

## [Unreleased]

## [3.0.2] - 2023-10-20

### Fixes

- Hide sub-commands from top level help.

## [3.0.1] - 2023-10-12

### Fixes
Expand Down Expand Up @@ -480,7 +486,8 @@ Commands can raise `AssertionError` exceptions to tell `delfino` some pre-condit

- Initial copy of source codes.

[Unreleased]: https://github.com/radeklat/delfino/compare/3.0.1...HEAD
[Unreleased]: https://github.com/radeklat/delfino/compare/3.0.2...HEAD
[3.0.2]: https://github.com/radeklat/delfino/compare/3.0.1...3.0.2
[3.0.1]: https://github.com/radeklat/delfino/compare/3.0.0...3.0.1
[3.0.0]: https://github.com/radeklat/delfino/compare/2.0.0...3.0.0
[2.0.0]: https://github.com/radeklat/delfino/compare/1.3.0...2.0.0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "delfino"
version = "3.0.1"
version = "3.0.2"
description = "A collection of command line helper scripts wrapping tools used during Python development."
authors = ["Radek Lát <[email protected]>"]
license = "MIT License"
Expand Down
6 changes: 5 additions & 1 deletion src/delfino/click_utils/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def _filter_and_log_invalid_command_names(
return group_command_names - missing_commands if missing_commands else group_command_names

def _register_packages(self):
sub_commands: Set[str] = set()
for command_package in self._command_packages:
commands = {command.name: command for command in find_commands(command_package)}
available_command_names = set(commands.keys())
Expand All @@ -241,7 +242,10 @@ def _register_packages(self):
enabled_commands.difference_update(disabled_commands)

for command_name, command in commands.items():
self._register(command, command_name in enabled_commands)
if command_name not in sub_commands: # hide sub-commands
self._register(command, command_name in enabled_commands)
if hasattr(command.command, "commands"):
sub_commands.update(command.command.commands.keys())

def _register(self, command: _Command, enabled: bool):
existing_command = self._visible_commands.pop(command.name, None) or self._hidden_commands.pop(
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/test_command_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ def should_not_load_commands_that_come_from_import_statements_and_start_with_an_
assert not registry.visible_commands
assert not registry.hidden_commands

@staticmethod
def should_not_load_sub_commands_of_a_group():
model_path = Path("group_command")
fake_command_files = [
FakeCommandFile(
content_template="import click\n"
"@click.group()\ndef visible_group():\n pass\n"
"@visible_group.group()\ndef hidden_group():\n pass\n"
"@hidden_group.command()\ndef hidden_sub_command():\n pass\n",
),
]
with demo_commands(model_path, fake_command_files):
registry = CommandRegistry({}, local_command_folders=[model_path])
assert {command.name for command in registry.visible_commands} == {"visible-group"}
assert not registry.hidden_commands


@pytest.mark.usefixtures("install_fake_plugins")
class TestCommandRegistryPluginAndCommandSelection:
Expand Down
Loading