Skip to content
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

Can't set the default value for multiple options with optional values #2001

Closed
ikapelyukhin opened this issue Jul 12, 2021 · 6 comments · Fixed by #2004
Closed

Can't set the default value for multiple options with optional values #2001

ikapelyukhin opened this issue Jul 12, 2021 · 6 comments · Fixed by #2004
Milestone

Comments

@ikapelyukhin
Copy link
Contributor

The following example combines optional values with multiple options:

#!/usr/bin/env python3

import click


@click.command()
@click.option('--label', multiple=True, is_flag=False, flag_value='default')
def cli(label):
    print(label)
    print(f"click.core._flag_needs_value: {click.core._flag_needs_value}")


cli()
$ ./test.py --label --label test
('<object object at 0x7f5ef63ec250>', 'test')
click.core._flag_needs_value: <object object at 0x7f5ef63ec250>

Expected result: ('default', 'test')

Looks like when multiple=True, the sentinel value (click.core._flag_needs_value) is never substituted with the default value specified by flag_value.

Environment:

  • Python version: 3.7.3
  • Click version: 8.0.1
@janluke
Copy link
Contributor

janluke commented Aug 3, 2021

Is this thing of using an option both as a flag and a non-flag common practice? I've never seen something like that in the CLIs I've used. Personally I wouldn't even allow it.

@davidism
Copy link
Member

davidism commented Aug 3, 2021

Yeah, this doesn't seem like a feature we were intending to support. I'll probably close this, if someone wants to make a PR that makes it work consistently I'm open to it, but I don't think it needs to be an open issue.

@ikapelyukhin
Copy link
Contributor Author

Is this thing of using an option both as a flag and a non-flag common practice?

It's probably rather uncommon, but it is something I've run into. And I'm not using it as a flag per se: in my (somewhat strange, I admit) use case it is possible to create a label without any text, so --label is basically a shorthand for --label "". This also happens to work perfectly fine when multiple=False.

Yeah, this doesn't seem like a feature we were intending to support.

Well, if it isn't supported then it would be nice to get an exception when conflicting options (is_flag=False and multiple=True) are specified. Otherwise you get '<object object at 0x7f5ef63ec250>' as the option value instead of the default, that doesn't seem like correct behavior.

I'll probably close this, if someone wants to make a PR that makes it work consistently I'm open to it, but I don't think it needs to be an open issue.

I can undo all changes related to prompting in #2004 for options with multiple values, then ParameterSource would be consistent.

@davidism
Copy link
Member

davidism commented Aug 3, 2021

We don't generally validate every single possible combination of things. There are a ton of combinations, and as a CLI application it has to be reasonably fast to create everything so it can execute when the user types the command.

I'm not clear why you want to roll back #2004, but you're welcome to do that if you want, just keep in mind our license.

@davidism davidism closed this as completed Aug 3, 2021
@ikapelyukhin
Copy link
Contributor Author

I'm not clear why you want to roll back #2004.

I don't mean the whole PR, I've made some changes that made things weird when it came to keeping ParameterSource consistent.

@ikapelyukhin
Copy link
Contributor Author

OK, I've made the changes that I wanted, so #2004 is good to go from my perspective.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 19, 2021
@davidism davidism reopened this Oct 8, 2021
@pallets pallets unlocked this conversation Oct 8, 2021
@davidism davidism added this to the 8.0.2 milestone Oct 8, 2021
@davidism davidism closed this as completed Oct 8, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants