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

feat!: rework collect-unknown-options into unknown-options-as-args, providing more comprehensive functionality #202

Merged
merged 5 commits into from
Oct 6, 2019

Conversation

henderea
Copy link
Contributor

@bcoe: since I don't seem to be able to re-open #181, I'm creating a new pull request. It turns out that the issue I was having with the command parsing after the previous pull request was that it uses an array arg, rather than the _ array. I've updated my feature from collect-unknown-options to unknown-options-as-args so that it can also cover this case.

I've tested this in the script I plan to use it in, and it works as I would expect now.

@bcoe
Copy link
Member

bcoe commented Sep 27, 2019

@henderea thank you for digging into this, I've been dragging my feet for too long on the next release of yargs, because I was hoping to loop back around to try to dig into this issue.

I've been the victim of my of my libraries' own success these days, and have been spread a bit thin on code review I'm afraid 😦... thanks for your hard work.

@mleguen, could I perhaps loop you in to help review this.

@mleguen
Copy link
Member

mleguen commented Sep 27, 2019

@bcoe my pleasure, but I am afraid I will not be available for a review before wednesday (my weekly OSS day at work).

@bcoe bcoe added the p1 label Sep 27, 2019
@mleguen
Copy link
Member

mleguen commented Oct 2, 2019

Wow! Just finished reading the extensive discussion in yargs/yargs#1243, #181 and #202 :-)...
Time to start reviewing!

Copy link
Member

@mleguen mleguen left a comment

Choose a reason for hiding this comment

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

This PR sounds OK to me, apart from the small refactoring suggestion.

However, I still have one question about your former PR: why do you use Array.forEach loops instead of Array.some, which would be shorter, more readable and more efficient. If it is once again a NodeJS 6 compatibility issue, perhaps you should mark it as a compatibility hack to be removed later, as you did for Object.values?

index.js Outdated
@@ -361,7 +361,7 @@ function parse (args, opts) {
// and terminates when one is observed.
var available = 0
for (ii = i + 1; ii < args.length; ii++) {
if (!args[ii].match(/^-[^0-9]/)) available++
if (!args[ii].match(/^-[^0-9]/) || (configuration['unknown-options-as-args'] && isUnknownOption(args[ii]))) available++
Copy link
Member

Choose a reason for hiding this comment

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

As you are using configuration['unknown-options-as-args'] && isUnknownOption(arg) rather frequently, and for the sake of readability, I would advise refactoring it into isUnknownOptionAsArg(arg):

Suggested change
if (!args[ii].match(/^-[^0-9]/) || (configuration['unknown-options-as-args'] && isUnknownOption(args[ii]))) available++
if (!args[ii].match(/^-[^0-9]/) || isUnknownOptionAsArg(args[ii])) available++

@henderea
Copy link
Contributor Author

henderea commented Oct 2, 2019

@mleguen On the topic of the forEach stuff, I was largely following the pattern of the existing code. The comment I put about Node 6 compatibility on the Object.values stuff was also copied from the existing code.

I'll make the requested refactoring to reduce the configuration['unknown-options-as-args'] && isUnknownOption(arg) into isUnknownOptionAsArg(arg) soon.

@mleguen
Copy link
Member

mleguen commented Oct 2, 2019

@henderea OK, I can see now the source of this complicated use of Array.foreach in place of Array.some is checkAllAliases.

I will submit a PR to improve this.

Copy link
Member

@mleguen mleguen left a comment

Choose a reason for hiding this comment

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

OK for me, thanks @henderea.

@henderea
Copy link
Contributor Author

henderea commented Oct 2, 2019

@mleguen: I've committed the suggested refactoring of isUnknownOptionAsArg(arg), and also have converted my 2 usages of forEach with a boolean to some. I left checkAllAliases alone, though, but I can include that in this PR if that would make it easier for everyone.

@henderea
Copy link
Contributor Author

henderea commented Oct 2, 2019

According to https://node.green/, it seems that support for array.some goes back just as far as array.forEach, so I think it should be fine on v6.

@bcoe bcoe changed the title Followup to #181: convert to 'unknown-options-as-args' to allow unknown options to be used as values of known options with parameters feat!: rework collect-unknown-options into unknown-options-as-args, providing more comprehensive functionality Oct 6, 2019
@bcoe bcoe merged commit ef771ca into yargs:master Oct 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants