Skip to content

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

Merged
bcoe merged 5 commits into
yargs:masterfrom
henderea:master
Oct 6, 2019
Merged

feat!: rework collect-unknown-options into unknown-options-as-args, providing more comprehensive functionality#202
bcoe merged 5 commits into
yargs:masterfrom
henderea:master

Conversation

@henderea

Copy link
Copy Markdown
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

bcoe commented Sep 27, 2019

Copy link
Copy Markdown
Member

@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

mleguen commented Sep 27, 2019

Copy link
Copy Markdown
Member

@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

mleguen commented Oct 2, 2019

Copy link
Copy Markdown
Member

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

@mleguen mleguen left a comment

Copy link
Copy Markdown
Member

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?

Comment thread index.js Outdated
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
Copy Markdown
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

henderea commented Oct 2, 2019

Copy link
Copy Markdown
Contributor Author

@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

mleguen commented Oct 2, 2019

Copy link
Copy Markdown
Member

@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.

@mleguen mleguen left a comment

Copy link
Copy Markdown
Member

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

henderea commented Oct 2, 2019

Copy link
Copy Markdown
Contributor Author

@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

henderea commented Oct 2, 2019

Copy link
Copy Markdown
Contributor Author

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