|
7 | 7 | from typing import NoReturn |
8 | 8 |
|
9 | 9 | import typer |
| 10 | +from pydantic import ValidationError |
10 | 11 | from rich.console import Console |
11 | 12 | from rich.theme import Theme |
12 | 13 |
|
13 | | -from antares import AntaresClient, ShipConfig |
| 14 | +from antares import AntaresClient |
14 | 15 | from antares.config_loader import load_config |
15 | 16 | from antares.errors import ConnectionError, SimulationError, SubscriptionError |
16 | 17 | from antares.logger import setup_logging |
| 18 | +from antares.models.ship import CircleShip, LineShip, RandomShip, ShipConfig, StationaryShip |
17 | 19 |
|
18 | 20 | app = typer.Typer(name="antares-cli", help="Antares CLI for ship simulation", no_args_is_help=True) |
19 | 21 | console = Console(theme=Theme({"info": "green", "warn": "yellow", "error": "bold red"})) |
@@ -86,21 +88,45 @@ def reset( |
86 | 88 |
|
87 | 89 |
|
88 | 90 | @app.command() |
89 | | -def add_ship( |
90 | | - x: float = typer.Option(..., help="X coordinate of the ship"), |
91 | | - y: float = typer.Option(..., help="Y coordinate of the ship"), |
| 91 | +def add_ship( # noqa: PLR0913 |
| 92 | + type: str = typer.Option(..., help="Type of ship: 'line', 'circle', 'random', or 'stationary'"), |
| 93 | + x: float = typer.Option(..., help="Initial X coordinate of the ship"), |
| 94 | + y: float = typer.Option(..., help="Initial Y coordinate of the ship"), |
| 95 | + angle: float = typer.Option(None, help="(line) Movement angle in radians"), |
| 96 | + speed: float = typer.Option(None, help="(line/circle) Constant speed"), |
| 97 | + radius: float = typer.Option(None, help="(circle) Radius of the circular path"), |
| 98 | + max_speed: float = typer.Option(None, help="(random) Maximum possible speed"), |
92 | 99 | config: str = typer.Option(None, help="Path to the TOML configuration file"), |
93 | 100 | verbose: bool = typer.Option(False, "--verbose", "-v", help="Enable verbose output"), |
94 | 101 | json_output: bool = typer.Option(False, "--json", help="Output in JSON format"), |
95 | 102 | ) -> None: |
96 | 103 | """ |
97 | | - Add a ship to the simulation with the specified parameters. |
| 104 | + Add a ship to the simulation, specifying its motion pattern and parameters. |
98 | 105 | """ |
99 | 106 | client = build_client(config, verbose, json_output) |
| 107 | + |
| 108 | + base_args = {"initial_position": (x, y)} |
| 109 | + |
| 110 | + ship: ShipConfig | None = None |
| 111 | + try: |
| 112 | + if type == "line": |
| 113 | + ship = LineShip(**base_args, angle=angle, speed=speed) # type: ignore[arg-type] |
| 114 | + elif type == "circle": |
| 115 | + ship = CircleShip(**base_args, radius=radius, speed=speed) # type: ignore[arg-type] |
| 116 | + elif type == "random": |
| 117 | + ship = RandomShip(**base_args, max_speed=max_speed) # type: ignore[arg-type] |
| 118 | + elif type == "stationary": |
| 119 | + ship = StationaryShip(**base_args) # type: ignore[arg-type] |
| 120 | + else: |
| 121 | + raise ValueError(f"Invalid ship type: {type!r}") |
| 122 | + |
| 123 | + except (ValidationError, ValueError, TypeError) as e: |
| 124 | + handle_error(f"Invalid ship parameters: {e}", code=2, json_output=json_output) |
| 125 | + return |
| 126 | + |
100 | 127 | try: |
101 | | - ship = ShipConfig(initial_position=(x, y)) |
102 | 128 | client.add_ship(ship) |
103 | | - msg = f"🚢 Added ship at ({x}, {y})" |
| 129 | + msg = f"🚢 Added {type} ship at ({x}, {y})" |
104 | 130 | typer.echo(json.dumps({"message": msg}) if json_output else msg) |
105 | 131 | except (ConnectionError, SimulationError) as e: |
106 | 132 | handle_error(str(e), code=2, json_output=json_output) |
|
0 commit comments