-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.py
56 lines (45 loc) · 1.68 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from typing import Any
import typer
from rich import print as rprint
from rich.console import Console
from gamewinner import play
from gamewinner.bracket import available_years, this_year
from gamewinner.printers import all_printers
from gamewinner.printers.basic_file_printer import BasicFilePrinter
from gamewinner.printers.iprinter import Printer
from gamewinner.strategies import Strategy, available_strategies
PRINTERS: dict[str, Printer] = {
printer.name: printer for printer in all_printers # type: ignore
}
STRATEGIES: dict[str, Strategy] = {
strategy.name: strategy for strategy in available_strategies
}
YEARS: dict[int, int] = {year: year for year in available_years}
app = typer.Typer(
pretty_exceptions_show_locals=False,
add_completion=False,
)
console = Console()
def _error_msg(tag: str, name: Any, legals: dict[Any, Any]) -> None:
rprint(
f"\n[red]Invalid {tag} [bold]`{name}`[/bold]. Must be one of [green]{list(legals.keys())}\n" # noqa
)
raise ValueError(f"illegal {tag}")
@app.command()
def main(
strategy: str = typer.Option(..., help="strategy you want to use"),
year: int = typer.Option(this_year, help="year you want to use"),
printer: str = typer.Option(BasicFilePrinter.name, help="printer to use"),
) -> None:
_year = YEARS.get(year, None)
if not _year:
_error_msg("year", year, YEARS)
_strategy = STRATEGIES.get(strategy, None)
if not _strategy:
_error_msg("strategy", strategy, STRATEGIES)
_printer = PRINTERS.get(printer, None)
if not _printer:
_error_msg("printer", printer, PRINTERS)
play(_strategy, _year, _printer) # type: ignore
if __name__ == "__main__":
app()