Skip to content

mcp_registry_client.commands.base

Base command class for CLI command pattern implementation.

BaseCommand

Bases: ABC

Abstract base class for CLI commands.

Provides a standard interface for command validation, execution, and output formatting.

Source code in mcp_registry_client/commands/base.py
class BaseCommand(ABC):
    """Abstract base class for CLI commands.

    Provides a standard interface for command validation, execution,
    and output formatting.
    """

    def __init__(self, args: argparse.Namespace) -> None:
        """Initialize command with parsed arguments.

        Args:
            args: Parsed command line arguments

        """
        self.args = args

    @abstractmethod
    def validate_args(self) -> None:
        """Validate command arguments.

        Raises:
            ValueError: If arguments are invalid

        """

    @abstractmethod
    async def execute(self) -> Any:  # noqa: ANN401
        """Execute the command logic.

        Returns:
            Command execution result

        Raises:
            Exception: Various exceptions based on command implementation

        """

    @abstractmethod
    def format_output(self, result: Any) -> None:  # noqa: ANN401
        """Format and display command output.

        Args:
            result: Result from execute() method

        """

    async def run(self) -> int:
        """Run the complete command workflow.

        Returns:
            Exit code (0 for success, non-zero for error)

        """
        try:
            self.validate_args()
            result: Any = await self.execute()
            self.format_output(result)
        except KeyboardInterrupt:
            raise
        except (ValueError, RegistryClientError, OSError, RuntimeError) as e:
            return self._handle_error(e)
        else:
            return 0

    def _handle_error(self, exc: Exception) -> int:
        """Handle command errors with consistent messaging.

        Args:
            exc: The exception that occurred

        Returns:
            Appropriate exit code for the error type

        """
        # Handle ValueError specially (user input validation errors)
        if isinstance(exc, ValueError):
            print_error(f'Error: {exc}')
            return 1

        context = f'{self.__class__.__name__.lower().replace("command", "")} operation'
        return handle_command_error(exc, context)

__init__(args)

Initialize command with parsed arguments.

Parameters:

Name Type Description Default
args Namespace

Parsed command line arguments

required
Source code in mcp_registry_client/commands/base.py
def __init__(self, args: argparse.Namespace) -> None:
    """Initialize command with parsed arguments.

    Args:
        args: Parsed command line arguments

    """
    self.args = args

execute() abstractmethod async

Execute the command logic.

Returns:

Type Description
Any

Command execution result

Raises:

Type Description
Exception

Various exceptions based on command implementation

Source code in mcp_registry_client/commands/base.py
@abstractmethod
async def execute(self) -> Any:  # noqa: ANN401
    """Execute the command logic.

    Returns:
        Command execution result

    Raises:
        Exception: Various exceptions based on command implementation

    """

format_output(result) abstractmethod

Format and display command output.

Parameters:

Name Type Description Default
result Any

Result from execute() method

required
Source code in mcp_registry_client/commands/base.py
@abstractmethod
def format_output(self, result: Any) -> None:  # noqa: ANN401
    """Format and display command output.

    Args:
        result: Result from execute() method

    """

run() async

Run the complete command workflow.

Returns:

Type Description
int

Exit code (0 for success, non-zero for error)

Source code in mcp_registry_client/commands/base.py
async def run(self) -> int:
    """Run the complete command workflow.

    Returns:
        Exit code (0 for success, non-zero for error)

    """
    try:
        self.validate_args()
        result: Any = await self.execute()
        self.format_output(result)
    except KeyboardInterrupt:
        raise
    except (ValueError, RegistryClientError, OSError, RuntimeError) as e:
        return self._handle_error(e)
    else:
        return 0

validate_args() abstractmethod

Validate command arguments.

Raises:

Type Description
ValueError

If arguments are invalid

Source code in mcp_registry_client/commands/base.py
@abstractmethod
def validate_args(self) -> None:
    """Validate command arguments.

    Raises:
        ValueError: If arguments are invalid

    """