1 file changed, +79, -5
69dbdebUpdate cli to use pygame view
src/cli.py
+79, -5
+79, -5
@@ -1,14 +1,90 @@
1 1 """Command-line entry point handling."""
2 2
3+import argparse
3 4 import sys
5+from dataclasses import dataclass
4 6
5 7 from .errors import FlyInError
6 8 from .flow.scheduler import Scheduler
7 9 from .parser import MapParser
10+from .render.base import Broadcast, Renderer
8 11 from .render.text import ColorTextRenderer
9 12 from .simulation import Simulation
10 13
11 14
15+@dataclass(frozen=True)
16+class Options:
17+ """What the command line asked for.
18+
19+ Attributes:
20+ map_file: Path of the map to fly.
21+ visual: Whether to open the graphical view as well.
22+ color: Whether the printed lines may carry color.
23+ """
24+
25+ map_file: str
26+ visual: bool
27+ color: bool
28+
29+
30+def parse_options(argv: list[str]) -> Options:
31+ """Read the command line.
32+
33+ Args:
34+ argv: Arguments, excluding the program name.
35+
36+ Returns:
37+ The options asked for.
38+ """
39+ parser = argparse.ArgumentParser(
40+ prog="python -m src",
41+ description="Fly a fleet of drones across a map of zones."
42+ )
43+ parser.add_argument("map_file", help="map file to read")
44+ parser.add_argument(
45+ "--visual",
46+ action="store_true",
47+ help="open a graphical view of the run as well"
48+ )
49+ parser.add_argument(
50+ "--no-color",
51+ action="store_true",
52+ help="print the turns without color"
53+ )
54+ parsed = parser.parse_args(argv)
55+ return Options(
56+ map_file=str(parsed.map_file),
57+ visual=bool(parsed.visual),
58+ color=not bool(parsed.no_color)
59+ )
60+
61+
62+def build_view(options: Options) -> Renderer:
63+ """Assemble the views the run will be shown.
64+
65+ The graphical view is imported only when it is wanted, so the
66+ program still runs on a machine where pygame is missing.
67+
68+ Args:
69+ options: What the command line asked for.
70+
71+ Returns:
72+ The view to hand the simulation.
73+
74+ Raises:
75+ FlyInError: If the graphical view was asked for and pygame is
76+ not installed.
77+ """
78+ text = ColorTextRenderer(enabled=None if options.color else False)
79+ if not options.visual:
80+ return text
81+ try:
82+ from .render.pygame_view import PygameView
83+ except ImportError as err:
84+ raise FlyInError(f"the graphical view needs pygame: {err}") from err
85+ return Broadcast(text, PygameView())
86+
87+
12 88 def main(argv: list[str]) -> int:
13 89 """Run the simulator.
14 90
@@ -18,13 +94,11 @@ def main(argv: list[str]) -> int:
18 94 Returns:
19 95 Process exit code, 0 on success.
20 96 """
21 - if len(argv) != 1:
22 - print("usage: python -m src <map_file>", file=sys.stderr)
23 - return 1
97+ options = parse_options(argv)
24 98 try:
25 - scenario = MapParser(argv[0]).parse()
99+ scenario = MapParser(options.map_file).parse()
26100 schedule = Scheduler(scenario).solve()
27 - Simulation(scenario, schedule).run(ColorTextRenderer())
101+ Simulation(scenario, schedule).run(build_view(options))
28102 except FlyInError as err:
29103 print(f"error: {err}", file=sys.stderr)
30104 return 1