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
Conversation
…d. Return directly from .opts().
…mand to action handler
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.
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.) |
Added section in README covering new routines and rationale, and example files for the three expected code patterns. Ready for feedback/review. |
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.
LGTM :-)
Woo hoo! 🎉 |
I am thinking perhaps do next release in early January? (I am starting to get busy leading into Xmas so my time will be limited.) |
Shipped in Commander v4.1 |
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.