Skip to content
Discussion options

You must be logged in to vote

I found the solution; it is because default values should of course not be passed as the first argument to typer.Option when using Annotated, but instead they should be specified as default values for the method parameter:

from pathlib import Path
from typing import Annotated
import typer

DEFAULT_CFG = Path.home() / "config.yaml"

def main(config: Annotated[Path, typer.Option("--config", "-c") = DEFAULT_CFG]):
    typer.echo(f"Using configuration: {config}")

if __name__ == "__main__":
    typer.run(main)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ulrikrasmussen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
1 participant