-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcli.py
More file actions
52 lines (45 loc) · 1.33 KB
/
Copy pathcli.py
File metadata and controls
52 lines (45 loc) · 1.33 KB
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
import typer
from typing import Optional
from rich.console import Console # NEW
from rich.table import Table # NEW
from rich import print # NEW
from beerlog.core import add_beer_to_database, get_beers_from_database
main = typer.Typer(help="Beer Management Application")
console = Console()
@main.command()
def add(
name: str,
style: str,
flavor: int = typer.Option(...),
image: int = typer.Option(...),
cost: int = typer.Option(...),
):
"""Adds a new beer to the database"""
if add_beer_to_database(name, style, flavor, image, cost):
print(":beer_mug: Beer added!!!") # NEW
else:
print(":no_entry: - Cannot add beer.") # NEW
@main.command("list")
def list_beers(style: Optional[str] = None):
"""Lists beers from the database"""
beers = get_beers_from_database(style)
table = Table(
title="Beerlog Database" if not style else f"Beerlog {style}"
)
headers = [
"id",
"name",
"style",
"flavor",
"image",
"cost",
"rate",
"date",
]
for header in headers:
table.add_column(header, style="magenta")
for beer in beers:
beer.date = beer.date.strftime("%Y-%m-%d")
values = [str(getattr(beer, header)) for header in headers]
table.add_row(*values)
console.print(table)