|
| 1 | +# CLI Package |
| 2 | + |
| 3 | +The CLI package provides the command-line interface for Hatch, organized into domain-specific handler modules following a handler-based architecture pattern. |
| 4 | + |
| 5 | +## Architecture Overview |
| 6 | + |
| 7 | +The CLI underwent a significant refactoring from a monolithic structure (`cli_hatch.py`) to a modular, handler-based architecture. This design emphasizes: |
| 8 | + |
| 9 | +- **Modularity**: Commands organized into focused handler modules |
| 10 | +- **Consistency**: Unified output formatting across all commands |
| 11 | +- **Extensibility**: Easy addition of new commands and features |
| 12 | +- **Testability**: Clear separation of concerns for unit testing |
| 13 | + |
| 14 | +### Package Structure |
| 15 | + |
| 16 | +``` |
| 17 | +hatch/cli/ |
| 18 | +├── __init__.py # Package exports and main() entry point |
| 19 | +├── __main__.py # Argument parsing and command routing |
| 20 | +├── cli_utils.py # Shared utilities and constants |
| 21 | +├── cli_mcp.py # MCP host configuration handlers |
| 22 | +├── cli_env.py # Environment management handlers |
| 23 | +├── cli_package.py # Package management handlers |
| 24 | +└── cli_system.py # System commands (create, validate) |
| 25 | +``` |
| 26 | + |
| 27 | +## Module Overview |
| 28 | + |
| 29 | +### Entry Point (`__main__.py`) |
| 30 | +The routing layer that parses command-line arguments and delegates to appropriate handler modules. Initializes shared managers and attaches them to the args namespace for handler access. |
| 31 | + |
| 32 | +**Key Components**: |
| 33 | +- `HatchArgumentParser`: Custom argument parser with formatted error messages |
| 34 | +- Command routing functions |
| 35 | +- Manager initialization |
| 36 | + |
| 37 | +### Utilities (`cli_utils.py`) |
| 38 | +Shared infrastructure used across all handlers, including: |
| 39 | + |
| 40 | +- **Color System**: HCL color palette with true color support |
| 41 | +- **ConsequenceType**: Dual-tense action labels for prompts and results |
| 42 | +- **ResultReporter**: Unified rendering for mutation commands |
| 43 | +- **TableFormatter**: Aligned table output for list commands |
| 44 | +- **Error Formatting**: Structured validation and error messages |
| 45 | + |
| 46 | +### Handler Modules |
| 47 | +Domain-specific command implementations: |
| 48 | + |
| 49 | +- **Environment Handlers** (`cli_env.py`): Environment lifecycle and Python environment operations |
| 50 | +- **Package Handlers** (`cli_package.py`): Package installation, removal, and synchronization |
| 51 | +- **MCP Handlers** (`cli_mcp.py`): MCP host configuration, discovery, and backup |
| 52 | +- **System Handlers** (`cli_system.py`): System-level operations (package creation, validation) |
| 53 | + |
| 54 | +## Getting Started |
| 55 | + |
| 56 | +### Programmatic Usage |
| 57 | + |
| 58 | +```python |
| 59 | +from hatch.cli import main, EXIT_SUCCESS, EXIT_ERROR |
| 60 | + |
| 61 | +# Run CLI programmatically |
| 62 | +exit_code = main() |
| 63 | + |
| 64 | +# Or import specific handlers |
| 65 | +from hatch.cli.cli_env import handle_env_create |
| 66 | +from hatch.cli.cli_utils import ResultReporter, ConsequenceType |
| 67 | +``` |
| 68 | + |
| 69 | +### Handler Signature Pattern |
| 70 | + |
| 71 | +All handlers follow a consistent signature: |
| 72 | + |
| 73 | +```python |
| 74 | +def handle_command(args: Namespace) -> int: |
| 75 | + """Handle 'hatch command' command. |
| 76 | + |
| 77 | + Args: |
| 78 | + args: Namespace with: |
| 79 | + - env_manager: HatchEnvironmentManager instance |
| 80 | + - mcp_manager: MCPHostConfigurationManager instance (if needed) |
| 81 | + - <command-specific arguments> |
| 82 | + |
| 83 | + Returns: |
| 84 | + Exit code (0 for success, 1 for error) |
| 85 | + """ |
| 86 | + # Implementation |
| 87 | + return EXIT_SUCCESS |
| 88 | +``` |
| 89 | + |
| 90 | +## Output Formatting |
| 91 | + |
| 92 | +The CLI uses a unified output formatting system: |
| 93 | + |
| 94 | +### Mutation Commands |
| 95 | +Commands that modify state use `ResultReporter`: |
| 96 | + |
| 97 | +```python |
| 98 | +reporter = ResultReporter("hatch env create", dry_run=False) |
| 99 | +reporter.add(ConsequenceType.CREATE, "Environment 'dev'") |
| 100 | +reporter.report_result() |
| 101 | +``` |
| 102 | + |
| 103 | +### List Commands |
| 104 | +Commands that display data use `TableFormatter`: |
| 105 | + |
| 106 | +```python |
| 107 | +from hatch.cli.cli_utils import TableFormatter, ColumnDef |
| 108 | + |
| 109 | +columns = [ |
| 110 | + ColumnDef(name="Name", width=20), |
| 111 | + ColumnDef(name="Status", width=10), |
| 112 | +] |
| 113 | +formatter = TableFormatter(columns) |
| 114 | +formatter.add_row(["my-env", "active"]) |
| 115 | +print(formatter.render()) |
| 116 | +``` |
| 117 | + |
| 118 | +## Backward Compatibility |
| 119 | + |
| 120 | +The old monolithic `hatch.cli_hatch` module has been refactored into the modular structure. For backward compatibility, imports from `hatch.cli_hatch` are still supported but deprecated: |
| 121 | + |
| 122 | +```python |
| 123 | +# Old (deprecated, still works): |
| 124 | +from hatch.cli_hatch import main, handle_mcp_configure |
| 125 | + |
| 126 | +# New (preferred): |
| 127 | +from hatch.cli import main |
| 128 | +from hatch.cli.cli_mcp import handle_mcp_configure |
| 129 | +``` |
| 130 | + |
| 131 | +## Related Documentation |
| 132 | + |
| 133 | +- [CLI Architecture](../../devs/architecture/cli_architecture.md): Detailed architectural design and patterns |
| 134 | +- [Adding CLI Commands](../../devs/implementation_guides/adding_cli_commands.md): Step-by-step implementation guide |
| 135 | +- [CLI Reference](../../users/CLIReference.md): User-facing command documentation |
0 commit comments