-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Opt-in behaviour to avoid name pollution #1102
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…d. Return directly from .opts().
…mand to action handler
This comment has been minimized.
This comment has been minimized.
This was referenced Nov 23, 2019
Closed
Closed
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Old behaviour:
New behaviour, safe options, which are passed to action handler:
(The routines are named for the future when the default behaviour has changed, and would pass true to get the legacy behaviour.) |
shadowspawn
removed
the
pending release
Merged into a branch for a future release, but not released yet
label
Jan 6, 2020
Shipped in Commander v4.1 |
This was referenced Feb 25, 2020
This was referenced Mar 14, 2020
This was referenced Apr 13, 2022
This was referenced Jul 22, 2024
This was referenced Aug 6, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request
Problem
Commander has traditionally stored the option values on the command object. This gives a compact syntax especially when working with the program object, but means there are potential naming conflicts between user defined options and properties on Command. This catches people out and there are not good work-arounds.
Collected issues: #933
For example, code with a conflict on both program and a command::
Solution
Add a way of configuring Command to behave differently. Implement "modern" configuration which stores the option values separately, and passes just the options to action handlers. The main code change is accessing program options through (existing)
.opts()
member.With the addition of couple of lines to the example program above, no other code changes:
Now get:
ToDo
Add info to README.
Notes
I considered using a Map for the option values rather than a plain Object, but it would mean changing all existing client code accessing option values. Continuing to use an object means action handling code is largely unaffected.
I considered creating option values object with
Object.create(null)
to further reduce name collisions, but felt it was too unusual for the small benefits.I would have exposed a data property called
opts
if there was not already a function with that name! However, using the existing function does allow code that works with both the old way and the new way, which is nice.We could change all the code examples to use
program.opts()
so they work with both old and modern behaviour.In a future major version we could change the default behaviour. The routines are named for the future, where you need to opt back into the old behaviour.