-
-
Notifications
You must be signed in to change notification settings - Fork 702
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
[BUG] Cannot create optional multi-valued positional argument #108
Comments
Hi, To enable you to take in a list with zero or more values instead of using Running the script below with import typer
import typing
app = typer.Typer()
app.command()
def test(
patterns: typing.List[str] = typer.Argument(None),
untracked: bool = typer.Option(False, "-u", "--untracked"),
):
if patterns:
typer.echo("Arguments for patterns provided")
else:
typer.echo("No arguments for patterns provided")
if __name__ == "__main__":
app() |
@IamCathal thank you for the response. I am very happy to know that it is possible to have optional multi-valued arguments in typer! 🥳 I seem to have missed the part of the docs where a similar example is given... 😅 Perhaps this should be fixed for consistency across I am closing the issue since it is not a bug after all. Thanks again! |
Thanks a lot for the help @IamCathal ! 🍰 🙇 Yeah, I agree @rrei it is not great. 😅 It is actually a bug in Click itself if I remember correctly. Anyway, thanks for reporting back and closing the issue! |
Describe the bug
First off, let me thank you for creating typer 😄 I absolutely love it, especially because click was my favorite CLI library and you went and made it even nicer 👍
Soooo, I was attempting to create a CLI with an optional multi-valued positional argument (i.e. taking zero or more values). I made two attempts to no avail. The first one didn't even run, and the second one didn't allow me to omit the argument (the "optional" part).
To Reproduce
Here is a minimal example that shows my first attempt at using a list-valued
typer.Argument()
. According to the docs, providing a default value makes the argument optional, so I provided an empty list.Running this with
python test.py
gives me the following stack trace:So in an attempt to fix this error, I replaced with argument's default value with ellipsis
...
, which allows the program to run but requires at least one value for the argument.Expected behavior
I believe the error is due to typer marking the argument as required despite it being a list-valued argument. I would expect it to be possible (and even the default behavior!) to create list-valued arguments that take zero or more values, instead of one or more. Even better (if possible) would be a generalization where minimum and maximum number of values consumed by the argument are specified, defaulting to 0 and infinity, respectively.
Environment
The text was updated successfully, but these errors were encountered: