Skip to content
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

fix: assignment of negate property #1302

Closed
wants to merge 1 commit into from
Closed

Conversation

snitin315
Copy link

@snitin315 snitin315 commented Jul 17, 2020

Pull Request

Resolve #1301

Problem

We were checking for indexOf -no- for negate: true instead of --no-

Solution

check index of --no-

ChangeLog

@@ -23,7 +23,7 @@ class Option {
this.required = flags.indexOf('<') >= 0; // A value must be supplied when the option is specified.
this.optional = flags.indexOf('[') >= 0; // A value is optional when the option is specified.
this.mandatory = false; // The option must have a value after parsing, which usually means it must be specified on command line.
this.negate = flags.indexOf('-no-') !== -1;
this.negate = flags.indexOf('--no-') !== -1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should test for it being at the start of the string. The other places in the code use a regular expression, but checking the index would be fine here I think.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like this?

Suggested change
this.negate = flags.indexOf('--no-') !== -1;
this.negate = flags.startsWith('--no-');

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better! (We moved away from using index to using routines in #1264 which is on the develop branch.)

program
.option('--module-no-parse <value>', 'test description');
program.parse(['node', 'test']);
expect(program.opts()).toEqual({});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding a test.

Testing .opts() being the empty object is a little fragile as .opts() has some subtle differences between storeOptionsAsProperties true/false, but this approach does avoid needing to know what property name the flag turned into. Good enough for now!

Copy link
Collaborator

@shadowspawn shadowspawn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please change the test to startsWith as per discussion

  2. Please try changing the target branch to release/6.x, which is close to release. If you get merge conflicts that look tricky to sort out then ok to leave it targeting master and I'll sort it out.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jul 18, 2020

I was just looking at the merge conflicts, and discovered I already fixed --no- on the release/6.x branch while reworking the option flag handling for #1256 !

I'll do some extra checking, but think this is already done.

@shadowspawn
Copy link
Collaborator

Adding a regression test with @snitin315 as co-author

@snitin315
Copy link
Author

Thanks @shadowspawn, how soon can we expect the release?

@shadowspawn
Copy link
Collaborator

We are getting ready to release v6 now, anytime from tonight to end of week.

@shadowspawn
Copy link
Collaborator

Fixed in Commander v6.0.0 which has been released : https://github.com/tj/commander.js/releases/tag/v6.0.0

@snitin315 snitin315 deleted the fix/negate branch July 29, 2020 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: defining an option which includes no- makes the option true by default
4 participants