-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Allow nargs=-1 in options with a non-whitespace separator #2771
Comments
@AndreasBackx What do you think? From a style standpoint, I have seen both styles. I prefer the first. It would also be nice to support and arbitrary number of parameters to options. Then we could just say use arguments for subcommands, urls, or files and options for everything else. |
I think it should be
I'd say this is a fair trade-off. Except that in the case that
Where So the limitation would have to be that this cannot be used for command that have subcommands. Unless we also support "global options" on subcommands (#66). I'd be an advocate for it, though the priority right now is getting up to speed with all issues and PRs before we can tackle parser changes I think. Also interested in hearing how typer tackles those issues. |
The implementation in typer requires a non-whitespace separator to be specified. Hence, The subcommand case is a good catch. I'd suggest forbidding multi-value CLI options in all but the last subcommand. |
I don't see that having much of a benefit compared to the current state. We should look at supporting |
I was very confused until I realized you didn't describe your Typer PR accurately here, compared to the actual PR description. The parser operates on tokens as given to us by the invoking shell. We can't control how the tokens are split incoming to us. So for example, Your examples in this thread suggest that you want to accept |
This request is similar to #2810, which I just closed. You can already accomplish this type of parsing without any changes to Click, by doing the following: import click
def separator(ctx: click.Context, param: click.Parameter, value: list[str]) -> list[str]:
out: list[str] = []
for item in value:
out.extend(v.strip() for v in item.split("|"))
return out
@click.command
@click.option("-a", multiple=True, callback=separator)
def cli(a: list[str]) -> None:
click.echo(a)
if __name__ == "__main__":
cli() Note that options already support a |
What should this feature do?
Allow parsing command line options with an arbitrary number of values. This would allow to type
instead of the more tedious and less familiar
This has been suggested in #2537. This request is different in that options with
nargs=-1
would require an additionalseparator
parameter, that has to be set to a non-whitespace character. This would mitigate the ambiguity issues of #2537.I have contributed this feature to a downstream CLI library (fastapi/typer#800) and would be happy to contribute the changes to click itself if this is something that you would like to see supported. The changes essentially imply adding a few lines to
click/src/click/core.py
Line 2267 in 4a758f5
Please comment accordingly.
The text was updated successfully, but these errors were encountered: