Files
TinyTorch/tito/commands/base.py
Vijay Janapa Reddi a51bfa0ab2 MAJOR: Separate CLI from framework - proper architectural separation
BREAKING CHANGE: CLI moved from tinytorch/cli/ to tito/

Perfect Senior Engineer Architecture:
- tinytorch/ = Pure ML framework (production)
- tito/ = Development/management CLI tool
- modules/ = Educational content

Benefits:
 Clean separation of concerns
 Framework stays lightweight (no CLI dependencies)
 Clear mental model for users
 Professional project organization
 Proper dependency management

Structure:
tinytorch/          # 🧠 Core ML Framework
├── core/          # Tensors, layers, operations
├── training/      # Training loops, optimizers
├── models/        # Model architectures
└── ...           # Pure ML functionality

tito/              # 🔧 Development CLI Tool
├── main.py        # CLI entry point
├── core/          # CLI configuration & console
├── commands/      # Command implementations
└── tools/         # CLI utilities

Key Changes:
- Moved all CLI code from tinytorch/cli/ to tito/
- Updated imports and entry points
- Separated dependencies (Rich only for dev tools)
- Updated documentation to reflect proper separation
- Maintained backward compatibility with bin/tito wrapper

This demonstrates how senior engineers separate:
- Production code (framework) from development tools (CLI)
- Core functionality from management utilities
- User-facing APIs from internal tooling

Educational Value:
- Shows proper software architecture
- Teaches separation of concerns
- Demonstrates dependency management
- Models real-world project organization
2025-07-10 22:08:56 -04:00

62 lines
1.8 KiB
Python

"""
Base command class for TinyTorch CLI.
"""
from abc import ABC, abstractmethod
from argparse import ArgumentParser, Namespace
from typing import Optional
import logging
from ..core.config import CLIConfig
from ..core.console import get_console
from ..core.exceptions import TinyTorchCLIError
logger = logging.getLogger(__name__)
class BaseCommand(ABC):
"""Base class for all CLI commands."""
def __init__(self, config: CLIConfig):
"""Initialize the command with configuration."""
self.config = config
self.console = get_console()
@property
@abstractmethod
def name(self) -> str:
"""Return the command name."""
pass
@property
@abstractmethod
def description(self) -> str:
"""Return the command description."""
pass
@abstractmethod
def add_arguments(self, parser: ArgumentParser) -> None:
"""Add command-specific arguments to the parser."""
pass
@abstractmethod
def run(self, args: Namespace) -> int:
"""Execute the command and return exit code."""
pass
def validate_args(self, args: Namespace) -> None:
"""Validate command arguments. Override in subclasses if needed."""
pass
def execute(self, args: Namespace) -> int:
"""Execute the command with error handling."""
try:
self.validate_args(args)
return self.run(args)
except TinyTorchCLIError as e:
logger.error(f"Command failed: {e}")
self.console.print(f"[red]❌ {e}[/red]")
return 1
except Exception as e:
logger.exception(f"Unexpected error in command {self.name}")
self.console.print(f"[red]❌ Unexpected error: {e}[/red]")
return 1