Commit 24aea94
committed
refactor: convert CLI module into package with modular structure
Convert vcs_versioning._cli from a single module into a well-organized package
with separate concerns for argument parsing, command execution, and resources.
Package Structure:
- vcs-versioning/src/vcs_versioning/_cli/__init__.py:
- Main CLI entry point and command execution
- Extracted _get_version_for_cli() helper function
- Refactored print functions (print_json, print_plain, print_key_value)
- Added PRINT_FUNCTIONS dispatch table
- Updated to use CliNamespace for type safety
- Simplified file finder import (no try/except)
- Added assertion for archival_template (guaranteed by argparse)
- vcs-versioning/src/vcs_versioning/_cli/_args.py:
- New module for CLI argument parsing
- CliNamespace class: typed argparse.Namespace subclass
- Type annotations for all CLI options
- get_cli_parser() function moved from __init__.py
- vcs-versioning/src/vcs_versioning/_cli/git_archival_stable.txt:
- Template for stable git archival file (no branch names)
- Loaded as package resource
- vcs-versioning/src/vcs_versioning/_cli/git_archival_full.txt:
- Template for full git archival file (with branch info)
- Loaded as package resource
Key Changes:
- Argument parsing:
- --stable/--full now store_const template filenames
- parse_args() receives CliNamespace() instance for type safety
- Version handling:
- Extracted into _get_version_for_cli() function
- Returns str (not str | None) - simpler type
- Exception handling (SystemExit) happens naturally
- Template handling:
- Archival templates moved to resource files
- Single loading point using importlib.resources.files()
- Removed _get_stable_archival_content() helper
- Removed _get_full_archival_content() helper
- Print functions:
- Renamed _print_plain -> print_plain
- Renamed _print_key_value -> print_key_value
- Added print_json function
- Dispatch via PRINT_FUNCTIONS dict
- Accept MutableMapping instead of dict for flexibility
Benefits:
- Better code organization and separation of concerns
- Type safety with CliNamespace annotations
- Easier to test individual components
- Templates are maintainable resource files
- More data-driven design (template filenames, print dispatch)
- Prepares for future CLI feature additions
- No external dependencies (importlib.resources is Python 3.10+)
All 10 CLI tests pass.1 parent 5a6c4b3 commit 24aea94
File tree
5 files changed
+333
-304
lines changed- vcs-versioning/src/vcs_versioning
- _cli
5 files changed
+333
-304
lines changedThis file was deleted.
0 commit comments