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

Duplicate Boolean flag options "--foo/--foo" are not detected (defaults to False) #1465

Closed
ramses0 opened this issue Feb 4, 2020 · 2 comments · Fixed by #1594
Closed

Duplicate Boolean flag options "--foo/--foo" are not detected (defaults to False) #1465

ramses0 opened this issue Feb 4, 2020 · 2 comments · Fixed by #1594

Comments

@ramses0
Copy link

ramses0 commented Feb 4, 2020

I ran into an issue where I had the code:

@click.option( "--print-verbose-info/--print-verbose-info", help="...", default=False )

...and running cli.py --print-verbose-info was apparently defaulting over to the False value. Totally my mistake (it should have been --no-print-verbose-info), but it'd be nice to get a warning/error for boolean values which match exactly (ie: cannot be set to true).

It was kindof tricky to diagnose b/c I assumed that the --print-verbose-info that I was passing on the CLI was triggering the True path, not the False path, and usually Click option parsing is pretty bulletproof so it took a while to find that copy/paste problem.

This would be a good-first-issue, I'm submitting it now more as a reminder to maybe follow-up on it and figure out the appropriate P.R.

@ramses0
Copy link
Author

ramses0 commented Feb 4, 2020

untested, but probably something like this?

diff --git a/click/core.py b/click/core.py
index 36004fe..edddd9f 100644
--- a/click/core.py
+++ b/click/core.py
@@ -1731,6 +1731,8 @@ class Option(Parameter):
                     second = second.lstrip()
                     if second:
                         secondary_opts.append(second.lstrip())
+                    if first == second:
+                        raise TypeError('Boolean options cannot use the same flag for true and false.')
                 else:
                     possible_names.append(split_opt(decl))
                     opts.append(decl)

@davidism davidism added this to the 8.0.0 milestone Jun 10, 2020
@ovezovs
Copy link
Contributor

ovezovs commented Jun 17, 2020

I will work on this

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants