-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Normalize flag names at the pflag level.
#18642
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
Normalize flag names at the pflag level.
#18642
Conversation
Instead of defining flags twice (once with underscores, once without), we just normalize them at the `pflag` level. Signed-off-by: Arthur Schreiber <[email protected]>
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
…ke `--tablet_types-to-wait` invalid). Signed-off-by: Arthur Schreiber <[email protected]>
Signed-off-by: Arthur Schreiber <[email protected]>
Signed-off-by: Arthur Schreiber <[email protected]>
Signed-off-by: Arthur Schreiber <[email protected]>
Signed-off-by: Arthur Schreiber <[email protected]>
Signed-off-by: Arthur Schreiber <[email protected]>
Otherwise this breaks consumers that e.g. expect output on stdout to be valid JSON. Signed-off-by: Arthur Schreiber <[email protected]>
Flag variations are handled at the pflag / cobra level via normalization. No need to test them in the unit tests anymore. Signed-off-by: Arthur Schreiber <[email protected]>
Signed-off-by: Arthur Schreiber <[email protected]>
Signed-off-by: Arthur Schreiber <[email protected]>
We call flag parsing directly here, so the dashed/underscored handling is not set up at this point. Signed-off-by: Arthur Schreiber <[email protected]>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #18642 +/- ##
=========================================
+ Coverage 0 67.53% +67.53%
=========================================
Files 0 1613 +1613
Lines 0 263778 +263778
=========================================
+ Hits 0 178134 +178134
- Misses 0 85644 +85644 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
rohit-nayak-ps
left a comment
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.
Great find!
Signed-off-by: Arthur Schreiber <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
Description
This pull request changes how we handle the migration from underscored to dashed CLI flags.
Previously, we defined flags twice, once with their new dashed name, and once with the legacy / deprecated underscored name. The underscored name was then marked as deprecated and hidden from things like the help output.
This worked sort of fine for regular flags, as we'd end up reusing the same memory to store the value for either the dashed or underscored flag, so specifying one or the other worked in most cases.
Unfortunately, this approach did not work at all with flags that were bound to Viper. Viper would bind to a specific flag object, and not directly to the pointer where the flag value is stored. Before accessing flag values, Viper would call
.Changedon the flag that was bound, and this would only return true on the flag struct for the exact variant that was specified on the CLI. As Viper can only bind to one flag object, this would break depending on which flag variant Viper was bound to and which flag was passed in via the CLI.We noticed this through CI failures, but users would eventually have noticed that too where deprecated flags would just break / not do anything anymore.
This pull request proposes to fix this using a different approach.
Namely, instead of defining different flag variants, we define flags once, using their dashed variant. Then we define a flag name normalizer function that translates underscored flag names to dashed flag names. This normalizer is called when flags are parsed, so internally we can just standardize on dashed names, while we still support underscored names to be passed to the CLI.
When an underscored name is encountered, we emit a deprecation message (just as before with the multiple flag variants defined).
Because there's only one flag defined, that's the flag we can bind to with Viper, and everything works just as expected.
Related Issue(s)
Checklist
Deployment Notes