|
| 1 | +"""CLI of example module. |
| 2 | +
|
| 3 | +This module demonstrates how to create CLI commands using Typer that integrate |
| 4 | +with the main Aignostics CLI system. The CLI follows a modular pattern where |
| 5 | +each module can register its own commands. |
| 6 | +
|
| 7 | +Usage examples: |
| 8 | + uv run aignostics example hello |
| 9 | + uv run aignostics example hello "Your Name" |
| 10 | + uv run aignostics example data |
| 11 | + uv run aignostics example process "some text to process" |
| 12 | +""" |
| 13 | + |
| 14 | +from typing import Annotated |
| 15 | + |
| 16 | +import typer |
| 17 | + |
| 18 | +from aignostics.utils import console, get_logger |
| 19 | + |
| 20 | +from ._service import Service |
| 21 | + |
| 22 | +logger = get_logger(__name__) |
| 23 | + |
| 24 | +# Create a Typer instance for this module's CLI commands |
| 25 | +# - name="example": This becomes the subcommand name (aignostics example ...) |
| 26 | +# - help: Shown when user runs "aignostics example --help" |
| 27 | +# This CLI object is automatically discovered and registered with the main CLI |
| 28 | +# through the module's __init__.py file which exports it in __all__ |
| 29 | +cli = typer.Typer(name="example", help="Example module commands") |
| 30 | + |
| 31 | + |
| 32 | +@cli.command() |
| 33 | +def hello(name: Annotated[str, typer.Argument(help="Name to greet")] = "World") -> None: |
| 34 | + """Say hello to someone. |
| 35 | +
|
| 36 | + This is a simple command that demonstrates: |
| 37 | + - How to use Typer's @cli.command() decorator to register a function as a CLI command |
| 38 | + - How to use Annotated types for command arguments with help text |
| 39 | + - How to provide default values for optional arguments |
| 40 | + - How to use the console utility for colored output |
| 41 | +
|
| 42 | + Usage: |
| 43 | + uv run aignostics example hello # Uses default "World" |
| 44 | + uv run aignostics example hello "Alice" # Custom name |
| 45 | +
|
| 46 | + Args: |
| 47 | + name (str): Name to greet. This is a positional argument with a default value. |
| 48 | + """ |
| 49 | + # Use the console utility from aignostics.utils for rich text output |
| 50 | + # The [green] syntax is Rich markup for colored text |
| 51 | + console.print(f"[green]Hello {name} from Example module![/green]") |
| 52 | + |
| 53 | + |
| 54 | +@cli.command() |
| 55 | +def data() -> None: |
| 56 | + """Get example data. |
| 57 | +
|
| 58 | + This command demonstrates: |
| 59 | + - How to create a command with no arguments |
| 60 | + - How to call service layer methods from CLI commands |
| 61 | + - How to format and display structured data in the terminal |
| 62 | +
|
| 63 | + Usage: |
| 64 | + uv run aignostics example data |
| 65 | + """ |
| 66 | + # Call the service layer to get data - this follows the separation of concerns pattern |
| 67 | + # where CLI commands are thin wrappers around business logic in the service layer |
| 68 | + example_data = Service.get_example_data() |
| 69 | + |
| 70 | + # Display the data with formatting |
| 71 | + console.print("[blue]Example Data:[/blue]") |
| 72 | + for key, value in example_data.items(): |
| 73 | + console.print(f" {key}: {value}") |
| 74 | + |
| 75 | + |
| 76 | +@cli.command() |
| 77 | +def process(text: Annotated[str, typer.Argument(help="Text to process")]) -> None: |
| 78 | + """Process some text. |
| 79 | +
|
| 80 | + This command demonstrates: |
| 81 | + - How to use required positional arguments |
| 82 | + - How to pass user input to service layer methods |
| 83 | + - How to display processed results |
| 84 | +
|
| 85 | + Usage: |
| 86 | + uv run aignostics example process "Hello World" |
| 87 | + uv run aignostics example process "Any text you want to process" |
| 88 | +
|
| 89 | + Args: |
| 90 | + text (str): Text to process. This is a required positional argument. |
| 91 | + """ |
| 92 | + # Process the text using the service layer |
| 93 | + result = Service.process_example(text) |
| 94 | + |
| 95 | + # Display the result with yellow coloring |
| 96 | + console.print(f"[yellow]{result}[/yellow]") |
0 commit comments