-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
45 lines (33 loc) · 1.34 KB
/
Copy pathexample.py
File metadata and controls
45 lines (33 loc) · 1.34 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
import datetime
from pathlib import Path
import typer
from minimal import ApiConfig
from pydantic import BaseModel, Field
import typer_pydantic_config
class AppConfig(BaseModel):
credentials: ApiConfig
output_dir: Path = Field("./output", description="Output directory")
init_timestamp: datetime.datetime = Field(
default_factory=lambda: datetime.datetime.now(tz=datetime.UTC),
description="Timestamp when config has been initialized.",
init=False,
)
timeout: int | None = Field(30, description="Request timeout in seconds")
def get_config() -> AppConfig:
"""This function is only implemented to have a proper type hint."""
return typer_pydantic_config.get_config()
app = typer.Typer(
name="example_app_api", # Setting the name of the app is required.
help="Example CLI using Pydantic + Typer.",
)
@app.command()
def hello() -> None:
"""Simple command to verify we can read the config."""
config = get_config()
typer.echo(f"Hello, {config.credentials.username}!")
typer.echo(f"Your API key: {config.credentials.api_key}")
typer.echo(f"Timeout: {config.timeout} seconds")
typer.echo(f"Init timestamp: {config.init_timestamp}")
typer.echo(f"Output path: {config.output_dir}")
if __name__ == "__main__":
typer_pydantic_config.start_config_app(app=app, config_cls=AppConfig)