Skip to content

Refactor CLI into modular structure - #524

Merged
martin-majlis merged 2 commits into
masterfrom
split-cli-into-smaller-files
Mar 30, 2026
Merged

martin-majlis merged 2 commits into
masterfrom
split-cli-into-smaller-files

Conversation

@martin-majlis

@martin-majlis martin-majlis commented Mar 30, 2026 •

Copy link
Copy Markdown
Owner

CLI Refactoring: Split monolithic cli.py into modular structure

Summary

This PR refactors the large 1481-line wikipediaapi/cli.py file into a clean, maintainable modular structure while preserving 100% backward compatibility.

Changes Made

🏗️ New Modular Structure

  • Main CLI: Reduced from 1481 lines to 54 lines (96% reduction)
  • Command modules: Split into 6 focused files by functionality
  • Shared utilities: Centralized common code in base.py

📁 File Structure

wikipediaapi/
├── cli.py                     # Main entry point (54 lines)
└── commands/                  # CLI command modules
    ├── __init__.py
    ├── base.py               # Shared utilities & options (267 lines)
    ├── page_commands.py      # Page content commands (261 lines)
    ├── link_commands.py      # Link-related commands (172 lines)
    ├── category_commands.py  # Category commands (151 lines)
    ├── geo_commands.py       # Geographic commands (427 lines)
    └── search_commands.py    # Search & discovery commands (256 lines)

🔧 Technical Improvements

  1. Maintainable file sizes: Each module 150-430 lines vs one 1481-line file
  2. Logical organization: Related commands grouped together
  3. Shared utilities: Common options, formatters, and helpers centralized
  4. Clean imports: Proper module organization with clear dependencies
  5. Code quality: All pre-commit checks pass (flake8, mypy, black, isort)

📚 Documentation Updates

  • DESIGN.rst: Added comprehensive CLI section documenting the new architecture
  • File layout: Updated to include new modular structure
  • Usage examples: Documented command patterns and extension guidelines

✅ Backward Compatibility

  • All CLI commands work identically: No changes to user interface
  • Same options and arguments: Complete preservation of existing behavior
  • Identical output formats: Text and JSON outputs unchanged
  • All tests pass: 79 CLI tests verified with updated snapshots

Command Modules

📄 page_commands.py

  • summary, text, sections, section, page
  • Page content and metadata operations

🔗 link_commands.py

  • links, backlinks, langlinks
  • Link relationship and language operations

📂 category_commands.py

  • categories, categorymembers
  • Category hierarchy and membership operations

🌍 geo_commands.py

  • coordinates, images, geosearch
  • Geographic and media operations

🔍 search_commands.py

  • search, random
  • Search and discovery operations

🛠️ base.py

  • Shared utilities, common options, formatters
  • TypedDict classes, enum validators, helper functions

Benefits

🎯 Maintainability

  • Easier navigation: Find relevant code quickly
  • Focused changes: Modify specific functionality without touching unrelated code
  • Clear responsibilities: Each module has a well-defined scope

🚀 Development

  • Parallel development: Multiple developers can work on different command groups
  • Targeted testing: Test individual command modules in isolation
  • Easier debugging: Isolate issues to specific command areas

📖 Documentation

  • Better organization: Related commands documented together
  • Clear patterns: Established patterns for adding new commands
  • Comprehensive coverage: All aspects of CLI architecture documented

Quality Assurance

✅ Code Quality

  • All pre-commit hooks pass
  • No flake8, mypy, or formatting issues
  • Proper type annotations and documentation

✅ Functionality

  • All 79 CLI tests pass
  • Command help systems work correctly
  • Real API calls function properly

✅ Compatibility

  • Zero breaking changes
  • All existing CLI usage patterns preserved
  • Entry point and command structure unchanged

Migration Guide

For Users

No changes required - all existing CLI commands work identically.

For Developers

To add new commands:

  1. Choose appropriate command module based on functionality
  2. Follow established patterns in existing modules
  3. Register command in module's register_commands() function
  4. Add tests following existing conventions

Files Changed

  • 45 files changed: 1842 insertions, 1634 deletions
  • 7 new files: Modular command structure
  • Updated: Tests, documentation, and imports

This refactoring transforms an unwieldy monolithic file into a clean, maintainable modular structure while preserving perfect backward compatibility and improving the developer experience.

@martin-majlis martin-majlis changed the title trivial: Split cli.py into smaller files Refactor CLI into modular structure Mar 30, 2026
@codecov

codecov Bot commented Mar 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.69889% with 65 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.50%. Comparing base (c35d015) to head (8a8bec9).
⚠️ Report is 96 commits behind head on master.

Files with missing lines Patch % Lines
wikipediaapi/commands/geo_commands.py 77.43% 37 Missing ⚠️
wikipediaapi/commands/base.py 85.45% 16 Missing ⚠️
wikipediaapi/commands/search_commands.py 86.81% 12 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #524      +/-   ##
==========================================
+ Coverage   95.29%   95.50%   +0.21%     
==========================================
  Files          59       65       +6     
  Lines        2676     2758      +82     
==========================================
+ Hits         2550     2634      +84     
+ Misses        126      124       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@martin-majlis
martin-majlis merged commit 95166a3 into master Mar 30, 2026
40 of 42 checks passed
@martin-majlis
martin-majlis deleted the split-cli-into-smaller-files branch March 30, 2026 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant