From 3fc96daa9bb61f3d17206ba26bef0cc2be828777 Mon Sep 17 00:00:00 2001 From: Juro Oravec Date: Tue, 10 Jun 2025 11:11:24 +0200 Subject: [PATCH] refactor: in `components ext run`, display only those extensions that actually have subcommands (#1251) --- CHANGELOG.md | 4 ++++ src/django_components/commands/ext_run.py | 3 +++ tests/test_command_ext.py | 25 +++++++++++------------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10c3e6d4..f7e7d7e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,10 @@ - Fix bug when error formatting failed when error value was not a string. +#### Refactor + +- `components ext run` CLI command now allows to call only those extensions that actually have subcommands. + ## v0.140.1 #### Fix diff --git a/src/django_components/commands/ext_run.py b/src/django_components/commands/ext_run.py index 17bda9fd..d1dd0a8a 100644 --- a/src/django_components/commands/ext_run.py +++ b/src/django_components/commands/ext_run.py @@ -19,6 +19,9 @@ from django_components.util.command import ComponentCommand def _gen_subcommands() -> List[Type[ComponentCommand]]: commands: List[Type[ComponentCommand]] = [] for extension in extensions.extensions: + if not extension.commands: + continue + ExtCommand = type( "ExtRunSubcommand_" + extension.name, (ComponentCommand,), diff --git a/tests/test_command_ext.py b/tests/test_command_ext.py index 9c461bed..8b42e5c4 100644 --- a/tests/test_command_ext.py +++ b/tests/test_command_ext.py @@ -206,21 +206,16 @@ class TestExtensionsRunCommand: output == dedent( f""" - usage: components ext run [-h] {{cache,defaults,view,debug_highlight,empty,dummy}} ... + usage: components ext run [-h] {{dummy}} ... Run a command added by an extension. {OPTIONS_TITLE}: - -h, --help show this help message and exit + -h, --help show this help message and exit subcommands: - {{cache,defaults,view,debug_highlight,empty,dummy}} - cache Run commands added by the 'cache' extension. - defaults Run commands added by the 'defaults' extension. - view Run commands added by the 'view' extension. - debug_highlight Run commands added by the 'debug_highlight' extension. - empty Run commands added by the 'empty' extension. - dummy Run commands added by the 'dummy' extension. + {{dummy}} + dummy Run commands added by the 'dummy' extension. """ ).lstrip() ) @@ -231,19 +226,23 @@ class TestExtensionsRunCommand: def test_run_command_ext_empty(self): out = StringIO() with patch("sys.stdout", new=out): - call_command("components", "ext", "run", "empty") + call_command("components", "ext", "run", "dummy") output = out.getvalue() assert ( output == dedent( f""" - usage: components ext run empty [-h] + usage: components ext run dummy [-h] {{dummy_cmd}} ... - Run commands added by the 'empty' extension. + Run commands added by the 'dummy' extension. {OPTIONS_TITLE}: - -h, --help show this help message and exit + -h, --help show this help message and exit + + subcommands: + {{dummy_cmd}} + dummy_cmd Dummy command description. """ ).lstrip() )