-
-
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
Prevent options with flags already in use from being added #1923
Conversation
Commander only checks some of the possible mistakes/accidental-misuse by the "author", but willing to consider more as they come up. Related: #1903 |
I am hopeful this can be done with less code by using the internal |
This comment was marked as resolved.
This comment was marked as resolved.
Done in 5ec2249. |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
The way an option is usually referred to in the end-user messages is I suggest listing the found item in same format too. I am deliberately simplifying here and not doing the error breakdown you did (although I admire the effort). I think it will be clear enough to the author?
|
A combo is already used, but with option-argument and extra spaces removed: see |
I did forget you were using one combo! But no need to calculate it, just use I think identifying the conflicting option is important and the whole |
Co-authored-by: John Gee <john@ruru.gen.nz>
Fair enough. I used your suggestion in 9721ae8, just added the command name and changed "as" to "since" because it reads better (with "as", I first read it as |
const matchingOption = (option.short && this._findOption(option.short)) | ||
|| (option.long && this._findOption(option.long)); | ||
if (matchingOption) { | ||
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to '${this._name}'`} since an option using same flag has already been added |
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.
command name is good addition 👍
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.
Code looks good to me, simple and informative and effective.
Just needs a couple of tests, for short and long conflicts.
(I am concerned this may be a blocking change for people upgrading big programs from older versions of Commander, because their code will be broken until they resolve the clashes. I might want to add configuration to make this optional specifically to ease migration, but won't do that now. We can do a prerelease and see if people hit issues that they considered an obstacle.) |
Carried on in #2055 |
Pull Request
Problem
Adding options with flags that are already in use is allowed.
Demo
ChangeLog
Changed
.addOption()
is called with an option whose flags are already in usePeer PRs
…solving similar problems
_registerOption()
, too – watch out for merge conflicts!)