typer icon indicating copy to clipboard operation
typer copied to clipboard

✨Treat keyword only parameters without defaults as option

Open wouterzwerink opened this issue 5 months ago • 2 comments

Reviving #270 to make Typer treat keyword only parameters as options even when they dont have a default.

E.g.

@app.command()
def my_command(*, my_param):
     pass

Will now result in the following CLI with a required flag instead of positional parameter: python my_module --my-param value

This is a breaking change, but I think it is the more logical behaviour. It allows people to get required options without default values in a really pythonic way that is much more intuitive than annotating type hints with typer.Option(...).

Supports #269

wouterzwerink avatar Aug 02 '25 22:08 wouterzwerink

I'm not sure where to add documentation for this, i.e. what the correct page would be. If anyone from the team could make a suggestion I'd be happy to document this.

wouterzwerink avatar Aug 02 '25 22:08 wouterzwerink

@wouterzwerink, thanks for your interest!

I think the drawback of such approach is that it might break the existing code bases. It's common to declare all parameters as keyword-only in order to avoid having to place parameters with defaults after parameters without defaults. This feature might break such code..

@app.command()
def hello(
    *,
    formal: bool = False,
    name: str,  #  <=  This will become option..
    color: str = "green",
):
    pass

https://github.com/fastapi/typer/issues/269#issuecomment-3302330010

Let's wait for Sebastian to approve the idea

YuriiMotov avatar Sep 22 '25 21:09 YuriiMotov