typer icon indicating copy to clipboard operation
typer copied to clipboard

[FEATURE] treating keyword-only arguments as Required CLI Options

Open podhmo opened this issue 4 years ago • 1 comments

Is your feature request related to a problem

There is a shorthand for the unrequired CLI option.

def command(verbose:bool=False):
    ...

But the shorthand for the required CLI option is not provided.

The solution you would like

So, we can use using keyword-only arguments as a marker for the required CLI Option. This is my suggestion.

following

def command(*, verbose:bool):
    ...

Now, verbose is treated as the required flag.

Describe alternatives you've considered

Currently, if we need the required CLI options, write the code as follows:

def command(verbose:bool=typer.Option(...)):
    ...

https://typer.tiangolo.com/tutorial/options/required/

Additional context

podhmo avatar Apr 19 '21 00:04 podhmo

Similar feature request was converted into discussion: #331

I think the drawback of such approach is that it might break the existing code bases. It's common do 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..

import typer

app = typer.Typer()

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


if __name__ == "__main__":
    app()

YuriiMotov avatar Sep 17 '25 10:09 YuriiMotov