Replies: 2 comments
-
|
I had a similar usecase where I am calling a typer app from a snakemake workflow, and sometimes the argument was missing in which case I wanted to use the app's default. I could workaround this, e.g. sending an empty string like |
Beta Was this translation helpful? Give feedback.
-
|
Worth splitting your argparse example into its two cases, because Typer handles them very differently. The positional import typer
app = typer.Typer()
@app.command()
def main(bar: str = typer.Argument("d")):
typer.echo(f"bar={bar!r}")The The clean workaround that reproduces the behavior is two parameters: a bool flag plus a value option. from typing import Optional
import typer
app = typer.Typer()
CONST = "c"
@app.command()
def main(
bar: str = typer.Argument("d"),
foo: bool = typer.Option(False, "--foo"),
foo_value: Optional[str] = typer.Option(None, "--foo-value"),
):
resolved = foo_value if foo_value is not None else (CONST if foo else "d")
typer.echo(f"bar={bar!r} foo={resolved!r}")If you genuinely need the single-flag Tested on Typer 0.26.8, Python 3.11. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I'm looking for an equivalent to python's argparse
nargs='?'option, where:(from python's argparse docs)
The attached code is an example of the desired behavior when using
argparse. The main feature that I'm looking is defining some constant value (or behavior) when a "bare" flag is provided, e.g:in this case, I want
footo contain some value, but still allowing the user to provide their own value:I hope that it makes sense...
I wasn't able to find a way to implement this using typer, would appreciate any help. thanks!
Operating System
Linux
Operating System Details
No response
Typer Version
0.15.1
Python Version
3.10.14
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions