Skip to content

Commit fa862ad

Browse files
committed
Change CLI tool argument parsing to click
The click library allows for more complex interactions with the CLI tool for upcoming changes that add support for interacting with the actual trading API. In future the listing of day ahead prices from the entsoe API might be moved into a separate CLI tool. Signed-off-by: cwasicki <[email protected]>
1 parent a5f0a41 commit fa862ad

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

src/frequenz/client/electricity_trading/cli/__main__.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,49 @@
33

44
"""CLI tool to interact with the trading API."""
55

6-
import argparse
76
from datetime import datetime, timedelta
87
from zoneinfo import ZoneInfo
98

9+
import click
10+
1011
from frequenz.client.electricity_trading.cli.day_ahead import list_day_ahead_prices
1112

13+
TZ = ZoneInfo("Europe/Berlin")
1214

13-
def main() -> None:
14-
"""Run main entry point for the CLI tool."""
15-
tz = ZoneInfo("Europe/Berlin")
16-
midnight = datetime.combine(datetime.now(tz), datetime.min.time(), tzinfo=tz)
17-
parser = argparse.ArgumentParser()
18-
parser.add_argument("--entsoe_key", type=str, required=True)
19-
parser.add_argument(
20-
"--start",
21-
type=datetime.fromisoformat,
22-
required=False,
23-
default=midnight,
24-
)
25-
parser.add_argument(
26-
"--end",
27-
type=datetime.fromisoformat,
28-
required=False,
29-
default=midnight + timedelta(days=2),
30-
)
31-
parser.add_argument("--country_code", type=str, required=False, default="DE_LU")
32-
args = parser.parse_args()
15+
iso = datetime.fromisoformat
16+
17+
18+
def midnight(days: int = 0) -> str:
19+
"""Return today's midnight."""
20+
return (
21+
datetime.combine(datetime.now(TZ), datetime.min.time(), tzinfo=TZ)
22+
+ timedelta(days)
23+
).isoformat()
3324

25+
26+
@click.group()
27+
def cli() -> None:
28+
"""CLI tool to interact with the trading API."""
29+
30+
31+
@cli.command()
32+
@click.option("--entsoe-key", required=True, type=str)
33+
@click.option("--start", default=midnight(), type=iso)
34+
@click.option("--end", default=midnight(days=2), type=iso)
35+
@click.option("--country-code", type=str, default="DE_LU")
36+
def list_day_ahead(
37+
entsoe_key: str, *, start: datetime, end: datetime, country_code: str
38+
) -> None:
39+
"""List day-ahead prices."""
3440
list_day_ahead_prices(
35-
entsoe_key=args.entsoe_key,
36-
start=args.start,
37-
end=args.end,
38-
country_code=args.country_code,
41+
entsoe_key=entsoe_key, start=start, end=end, country_code=country_code
3942
)
4043

4144

45+
def main() -> None:
46+
"""Run the main Click CLI."""
47+
cli()
48+
49+
4250
if __name__ == "__main__":
4351
main()

0 commit comments

Comments
 (0)