-
-
Notifications
You must be signed in to change notification settings - Fork 682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default value for optional multi-value argument #518
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.
|
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. |
Using a callback worked for me
|
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
|
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"] |
First Check
Commit to Help
Example Code
Description
The provided example raises the following error:
Looking at the Click docs, my understanding is that
nargs=-1
is used to denote Variadic arguments, i.e. multi-value arguments.So if
default
is not supported fornargs=-1
, is there any way to have default values for multi-value arguments in Typer?Desired behaviour: the default values are used if no argument values are provided, with a help message that looks something like this:
Operating System
Windows
Operating System Details
No response
Typer Version
0.7.0
Python Version
3.10.9
Additional Context
I believe this is related to #108, where the conclusion was to use
None
as default to achieve an optional multi-value argument. However, it would be useful if other default values were supported as well. But this might just be a limitation of Click?Love the project ❤️
The text was updated successfully, but these errors were encountered: