-
First Check
Commit to Help
Example Codeimport typer
def main(names: list[str] = typer.Argument(["Peter", "Paul", "Mary"])):
for name in names:
typer.echo(f"Hi, {name}")
if __name__ == '__main__':
typer.run(main)DescriptionThe provided example raises the following error: Looking at the Click docs, my understanding is that So if Desired behaviour: the default values are used if no argument values are provided, with a help message that looks something like this: Operating SystemWindows Operating System DetailsNo response Typer Version0.7.0 Python Version3.10.9 Additional ContextI believe this is related to #108, where the conclusion was to use Love the project ❤️ |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
|
This may or may not help but I got around this issue by manually creating a callback that detects if the given parameter is I also set I can post the complete code if anyone needs it but I feel it's easy enough to understand from this.
|
Beta Was this translation helpful? Give feedback.
-
|
Edit: nvm it works for: def stdin_callback(value: Optional[Path]) -> Path:
return value if value else [Path('/dev/stdin')]def stdin_callback(value: Optional[Path]) -> Path:
return value if value else Path('/dev/stdin')
def main(
files: List[Path] = typer.Argument(
default=[None].
allow_dash=True,
exists=True,
dir_okay=False,
readable=True,
callback=stdin_callback)
Introducing a callback doesn't solve the issue unfortunately. |
Beta Was this translation helpful? Give feedback.
-
|
Using a callback worked for me |
Beta Was this translation helpful? Give feedback.
-
|
Trying to do something similar (a command that optionally takes any number of string arguments), I tried many variations, before I discovered that this is the only way that works: However, this doesn't, even though it should mean the same thing, it still makes it required. These similar variations also still end up required: This one throws the "'default' is not supported for nargs=-1" error: And these variations do make it optional, but also turn it into a |
Beta Was this translation helpful? Give feedback.
-
|
Using However, I use another workaround that might be interesting: def main(names: list[str] | None = typer.Argument(None)):
if names is None:
names = ["Peter", "Paul", "Mary"] |
Beta Was this translation helpful? Give feedback.
-
Note that you can simplify this to something like: def main(names: Annotated[list[str] | None, typer.Argument()] = None):
do_something_with(names or ["Peter", "Paul", "Mary"])(also adjusted to the newer Typer API) |
Beta Was this translation helpful? Give feedback.
Using
typer=0.12.5I still can reproduce the reported issue.However, I use another workaround that might be interesting: