refactor: simplify CLI command logic by extracting helper functions #13

Merged
mathias2784 merged 5 commits from cli into master 2026-10-02 12:03:20 +00:00
2 changed files with 12 additions and 4 deletions
Showing only changes of commit 5c7d300619 - Show all commits
+1
View File
@@ -1,4 +1,5 @@
from .cli import app from .cli import app
def main(): def main():
app() app()
+11 -4
View File
@@ -1,5 +1,5 @@
from pathlib import Path from pathlib import Path
from typing import Any, Optional from typing import Annotated, Any
import typer import typer
@@ -8,10 +8,11 @@ from photools3.photo import Photo
app = typer.Typer(help="PhotoTools3: A CLI for image processing.") app = typer.Typer(help="PhotoTools3: A CLI for image processing.")
@app.callback(invoke_without_command=True) @app.callback(invoke_without_command=True)
def main( def main(
ctx: typer.Context, ctx: typer.Context,
version: Optional[bool] = typer.Option(None, "--version", is_eager=True), version: bool | None = typer.Option(None, "--version", is_eager=True),
): ):
""" """
Main entry point for PhotoTools3 CLI. Main entry point for PhotoTools3 CLI.
@@ -24,8 +25,11 @@ def main(
typer.echo(ctx.get_help()) typer.echo(ctx.get_help())
raise typer.Exit() raise typer.Exit()
@app.command("keywords") @app.command("keywords")
def keywords(file_path: Path = typer.Argument(..., help="Path to the image file")): def keywords(
file_path: Annotated[Path, typer.Argument(..., help="Path to the image file")],
):
"""Extract keywords from a single image.""" """Extract keywords from a single image."""
if not file_path.exists(): if not file_path.exists():
typer.echo(f"Error: File '{file_path}' does not exist.", err=True) typer.echo(f"Error: File '{file_path}' does not exist.", err=True)
@@ -35,16 +39,18 @@ def keywords(file_path: Path = typer.Argument(..., help="Path to the image file"
typer.echo(f"Processing: {file_path}") typer.echo(f"Processing: {file_path}")
result = _run_keywords_extraction(file_path) result = _run_keywords_extraction(file_path)
_print_results(result) _print_results(result)
except Exception as e: except Exception as e: # noqa: BLE001
typer.echo(f"Error processing image: {e}", err=True) typer.echo(f"Error processing image: {e}", err=True)
raise typer.Exit(code=1) raise typer.Exit(code=1)
def _run_keywords_extraction(file_path: Path) -> Any: def _run_keywords_extraction(file_path: Path) -> Any:
"""Handles the core logic of loading the photo and calling the LLM.""" """Handles the core logic of loading the photo and calling the LLM."""
session = LlmSessionCaptionsAndKeywords() session = LlmSessionCaptionsAndKeywords()
photo = Photo(file_path) photo = Photo(file_path)
return session.call([photo]) return session.call([photo])
def _print_results(result: Any) -> None: def _print_results(result: Any) -> None:
"""Formats and prints the extraction results to the console.""" """Formats and prints the extraction results to the console."""
typer.echo("\n--- Results ---") typer.echo("\n--- Results ---")
@@ -57,5 +63,6 @@ def _print_results(result: Any) -> None:
typer.echo(str(result)) typer.echo(str(result))
typer.echo("---------------") typer.echo("---------------")
if __name__ == "__main__": if __name__ == "__main__":
app() app()