-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Global CLI flag should override env var flag #10423
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #10423 +/- ##
==========================================
- Coverage 88.81% 88.81% -0.01%
==========================================
Files 180 180
Lines 22558 22562 +4
==========================================
+ Hits 20036 20038 +2
- Misses 2522 2524 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
@@ -380,6 +380,22 @@ def test_global_flag_at_child_context(self): | |||
|
|||
assert flags_a.USE_COLORS == flags_b.USE_COLORS | |||
|
|||
def test_global_flag_with_env_var(self, monkeypatch): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love this test
(cherry picked from commit 601fee0)
(cherry picked from commit 601fee0) Co-authored-by: Gerda Shank <[email protected]>
resolves #10304
Problem
When both an environment variable and a command line flag are provided, the cli flag should override the environment variable. This wasn't happening for "global" flags, which can occur both before and after the sub command, when the flag came after the command.
Solution
The click context contains a "child" context for command options and a "parent" context for flags which occur before the command. If there was no "cli" setting for a flag, it would use the env var. When processing the parent context flags we add code to check if the source of this parameter is an env var, and do not overwrite the value of the param if it has already been set by the user.
Checklist