refactor: in components ext run, display only those extensions that actually have subcommands (#1251)

This commit is contained in:
Juro Oravec 2025-06-10 11:11:24 +02:00 committed by GitHub
parent 458e1894db
commit 3fc96daa9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 13 deletions

View file

@ -38,6 +38,10 @@
- Fix bug when error formatting failed when error value was not a string. - 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 ## v0.140.1
#### Fix #### Fix

View file

@ -19,6 +19,9 @@ from django_components.util.command import ComponentCommand
def _gen_subcommands() -> List[Type[ComponentCommand]]: def _gen_subcommands() -> List[Type[ComponentCommand]]:
commands: List[Type[ComponentCommand]] = [] commands: List[Type[ComponentCommand]] = []
for extension in extensions.extensions: for extension in extensions.extensions:
if not extension.commands:
continue
ExtCommand = type( ExtCommand = type(
"ExtRunSubcommand_" + extension.name, "ExtRunSubcommand_" + extension.name,
(ComponentCommand,), (ComponentCommand,),

View file

@ -206,21 +206,16 @@ class TestExtensionsRunCommand:
output output
== dedent( == dedent(
f""" 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. Run a command added by an extension.
{OPTIONS_TITLE}: {OPTIONS_TITLE}:
-h, --help show this help message and exit -h, --help show this help message and exit
subcommands: subcommands:
{{cache,defaults,view,debug_highlight,empty,dummy}} {{dummy}}
cache Run commands added by the 'cache' extension. dummy Run commands added by the 'dummy' 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.
""" """
).lstrip() ).lstrip()
) )
@ -231,19 +226,23 @@ class TestExtensionsRunCommand:
def test_run_command_ext_empty(self): def test_run_command_ext_empty(self):
out = StringIO() out = StringIO()
with patch("sys.stdout", new=out): with patch("sys.stdout", new=out):
call_command("components", "ext", "run", "empty") call_command("components", "ext", "run", "dummy")
output = out.getvalue() output = out.getvalue()
assert ( assert (
output output
== dedent( == dedent(
f""" 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}: {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() ).lstrip()
) )