[loop-generated] [refactor] Split timmy/cli.py — 693 lines, command-line interface monolith #1401

Closed
opened 2026-03-24 12:44:23 +00:00 by Timmy · 1 comment
Owner

Problem:
The CLI module is 693 lines and handles multiple concerns in one file: argument parsing, command routing, session management, and output formatting.

Current State:

  • Single large file with mixed responsibilities
  • Command parsing, routing, execution all bundled
  • Hard to test individual command handlers
  • Difficult to add new commands without editing core file

Proposed Structure:

src/timmy/cli/
├── __init__.py          # Main CLI entry point (50 lines)
├── parser.py            # Argument parsing and validation (150 lines)
├── commands/            # Command handlers package
│   ├── __init__.py      # Command registry (40 lines)
│   ├── chat.py          # Chat-related commands (120 lines)
│   ├── config.py        # Configuration commands (80 lines)
│   ├── session.py       # Session management commands (100 lines)
│   └── tools.py         # Tool-related commands (90 lines)
├── output.py            # Output formatting and display (80 lines)
└── session.py           # Session lifecycle management (70 lines)

Acceptance Criteria:

  • CLI split into focused modules under src/timmy/cli/ package
  • Command handlers separated by domain (chat, config, session, tools)
  • Argument parsing isolated in parser.py
  • Output formatting centralized in output.py
  • All existing CLI functionality preserved
  • Unit tests pass for all command handlers
  • Main entry point remains clean and minimal

Files to create:

  • src/timmy/cli/ package (new directory)
  • Split original src/timmy/cli.py into focused modules
  • Update imports throughout codebase

Priority: Medium - Code organization and maintainability

Benefits:

  • Easier to test individual commands
  • Simpler to add new commands
  • Better separation of concerns
  • More maintainable CLI architecture
**Problem:** The CLI module is 693 lines and handles multiple concerns in one file: argument parsing, command routing, session management, and output formatting. **Current State:** - Single large file with mixed responsibilities - Command parsing, routing, execution all bundled - Hard to test individual command handlers - Difficult to add new commands without editing core file **Proposed Structure:** ``` src/timmy/cli/ ├── __init__.py # Main CLI entry point (50 lines) ├── parser.py # Argument parsing and validation (150 lines) ├── commands/ # Command handlers package │ ├── __init__.py # Command registry (40 lines) │ ├── chat.py # Chat-related commands (120 lines) │ ├── config.py # Configuration commands (80 lines) │ ├── session.py # Session management commands (100 lines) │ └── tools.py # Tool-related commands (90 lines) ├── output.py # Output formatting and display (80 lines) └── session.py # Session lifecycle management (70 lines) ``` **Acceptance Criteria:** - [ ] CLI split into focused modules under `src/timmy/cli/` package - [ ] Command handlers separated by domain (chat, config, session, tools) - [ ] Argument parsing isolated in `parser.py` - [ ] Output formatting centralized in `output.py` - [ ] All existing CLI functionality preserved - [ ] Unit tests pass for all command handlers - [ ] Main entry point remains clean and minimal **Files to create:** - `src/timmy/cli/` package (new directory) - Split original `src/timmy/cli.py` into focused modules - Update imports throughout codebase **Priority:** Medium - Code organization and maintainability **Benefits:** - Easier to test individual commands - Simpler to add new commands - Better separation of concerns - More maintainable CLI architecture
Author
Owner

Implementation Instructions for CLI Refactor

Step 1: Analyze Current CLI Structure

  • Review src/timmy/cli.py (693 lines)
  • Identify command handlers, parsing logic, output formatting
  • Map dependencies and imports

Step 2: Create Package Structure

mkdir -p src/timmy/cli/commands
touch src/timmy/cli/__init__.py
touch src/timmy/cli/parser.py
touch src/timmy/cli/output.py
touch src/timmy/cli/session.py
touch src/timmy/cli/commands/__init__.py
touch src/timmy/cli/commands/chat.py
touch src/timmy/cli/commands/config.py
touch src/timmy/cli/commands/session.py
touch src/timmy/cli/commands/tools.py

Step 3: Split by Responsibility

parser.py (~150 lines):

  • Argument parser setup
  • Command-line validation
  • Help text generation
  • Common argument groups

output.py (~80 lines):

  • Terminal formatting utilities
  • Progress indicators
  • Error display
  • Success messages

session.py (~80 lines):

  • Session lifecycle management
  • Session file operations
  • Session state validation

commands/ package:

  • __init__.py: Command registry and dispatcher (40 lines)
  • chat.py: Chat/conversation commands (120 lines)
  • config.py: Configuration commands (80 lines)
  • session.py: Session commands (100 lines)
  • tools.py: Tool-related commands (90 lines)

init.py (~50 lines):

  • Main entry point
  • Import and setup commands
  • Call parser and dispatcher

Step 4: Maintain Backward Compatibility

  • Keep original cli.py as thin wrapper initially
  • Ensure all existing commands work identically
  • Preserve command-line interface exactly

Step 5: Testing

# Verify all commands still work
python -m timmy.cli --help
python -m timmy.cli chat
python -m timmy.cli config
python -m timmy.cli session list

# Run existing tests
tox -e unit -k cli

Step 6: Update Imports

  • Update any imports of timmy.cli throughout codebase
  • Use find/grep to locate import references
  • Update gradually to avoid breaking changes

Acceptance Criteria:

  • CLI module split into focused packages
  • All existing commands work identically
  • Tests pass: tox -e unit -k cli
  • No import errors anywhere in codebase
  • Line count reduced from 693 to <100 per file
  • Each module has single responsibility

Notes:

  • This is a pure refactor - no functional changes
  • Focus on clean separation of concerns
  • Maintain exact same command-line behavior
## Implementation Instructions for CLI Refactor ### Step 1: Analyze Current CLI Structure - Review `src/timmy/cli.py` (693 lines) - Identify command handlers, parsing logic, output formatting - Map dependencies and imports ### Step 2: Create Package Structure ```bash mkdir -p src/timmy/cli/commands touch src/timmy/cli/__init__.py touch src/timmy/cli/parser.py touch src/timmy/cli/output.py touch src/timmy/cli/session.py touch src/timmy/cli/commands/__init__.py touch src/timmy/cli/commands/chat.py touch src/timmy/cli/commands/config.py touch src/timmy/cli/commands/session.py touch src/timmy/cli/commands/tools.py ``` ### Step 3: Split by Responsibility **parser.py** (~150 lines): - Argument parser setup - Command-line validation - Help text generation - Common argument groups **output.py** (~80 lines): - Terminal formatting utilities - Progress indicators - Error display - Success messages **session.py** (~80 lines): - Session lifecycle management - Session file operations - Session state validation **commands/** package: - `__init__.py`: Command registry and dispatcher (40 lines) - `chat.py`: Chat/conversation commands (120 lines) - `config.py`: Configuration commands (80 lines) - `session.py`: Session commands (100 lines) - `tools.py`: Tool-related commands (90 lines) **__init__.py** (~50 lines): - Main entry point - Import and setup commands - Call parser and dispatcher ### Step 4: Maintain Backward Compatibility - Keep original `cli.py` as thin wrapper initially - Ensure all existing commands work identically - Preserve command-line interface exactly ### Step 5: Testing ```bash # Verify all commands still work python -m timmy.cli --help python -m timmy.cli chat python -m timmy.cli config python -m timmy.cli session list # Run existing tests tox -e unit -k cli ``` ### Step 6: Update Imports - Update any imports of `timmy.cli` throughout codebase - Use find/grep to locate import references - Update gradually to avoid breaking changes ### Acceptance Criteria: - [ ] CLI module split into focused packages - [ ] All existing commands work identically - [ ] Tests pass: `tox -e unit -k cli` - [ ] No import errors anywhere in codebase - [ ] Line count reduced from 693 to <100 per file - [ ] Each module has single responsibility ### Notes: - This is a pure refactor - no functional changes - Focus on clean separation of concerns - Maintain exact same command-line behavior
kimi was assigned by Timmy 2026-03-24 12:49:06 +00:00
kimi was unassigned by Timmy 2026-03-24 19:19:17 +00:00
Timmy closed this issue 2026-03-24 21:54:14 +00:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Rockachopa/Timmy-time-dashboard#1401