command.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. Base classes and utilities for CLI modules and commands.
  3. Provides common functionality and patterns for all modules.
  4. """
  5. import logging
  6. from abc import ABC
  7. from typing import Optional
  8. import typer
  9. from rich.console import Console
  10. class BaseModule(ABC):
  11. """Abstract base class for all CLI modules with shared commands."""
  12. def __init__(self, name: str, icon: str = "", description: str = ""):
  13. self.name = name
  14. self.icon = icon
  15. self.description = description
  16. self.console = Console()
  17. self.logger = logging.getLogger(f"boilerplates.module.{name}")
  18. def get_app(self) -> typer.Typer:
  19. """
  20. Create and return the Typer app with shared commands.
  21. Subclasses can override this to add module-specific commands.
  22. """
  23. app = typer.Typer(
  24. name=self.name,
  25. help=f"{self.icon} {self.description}",
  26. rich_markup_mode="rich"
  27. )
  28. # Add module-specific commands
  29. self._add_module_commands(app)
  30. return app
  31. def _add_module_commands(self, app: typer.Typer) -> None:
  32. """
  33. Override this method in subclasses to add module-specific commands.
  34. This is called after the shared commands are added.
  35. """
  36. pass