Typer subcommand when called as function call from another subcommand doesn't take default typer option value, but it takes the typer object as parameter. #605
-
First Check
Commit to Help
Example Codeimport typer
app = typer.Typer()
@app.command()
def cmd1(number: int = typer.Option(1, help="An integer for cmd1")):
typer.echo(f"Running cmd1 with number: {number}")
cmd2()
@app.command()
def cmd2(number: int = typer.Option(2, help="An integer for cmd2")):
typer.echo(f"Running cmd2 with number: {number}")
if __name__ == "__main__":
app()
~ DescriptionTyper subcommand when called as a function call from another subcommand, the function doesn't take the default value as an argument but rather takes the typer object. In the above sample snippet, i was expecting the output
But, I am seeing the following output.
Am I missing here something? Operating SystemLinux Operating System DetailsUbuntu 20.04 Typer Version0.7.0 Python VersionPython 3.8.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi, I do not know how Typer works internally, but you called |
Beta Was this translation helpful? Give feedback.
-
This is a disadvantage of old-style approach when you specify `` as default value. Instead of def cmd1(number: int = typer.Option(1, help="An integer for cmd1")): use def cmd1(number: Annotated[int, typer.Option(help="An integer for cmd1")] = 1): Full example: from typing import Annotated
import typer
app = typer.Typer()
@app.command()
def cmd1(number: Annotated[int, typer.Option(help="An integer for cmd1")] = 1):
typer.echo(f"Running cmd1 with number: {number}")
cmd2()
@app.command()
def cmd2(number: Annotated[int, typer.Option(help="An integer for cmd2")] = 2):
typer.echo(f"Running cmd2 with number: {number}")
if __name__ == "__main__":
app() |
Beta Was this translation helpful? Give feedback.
This is a disadvantage of old-style approach when you specify `` as default value.
To avoid this, use Annotated approach:
Instead of
use
Full example: