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(halt-at-non-option): prevent known args from being parsed when "unknown-options-as-args" is enabled #438

Merged
merged 2 commits into from Jul 19, 2022

Conversation

kherock
Copy link
Contributor

@kherock kherock commented Mar 26, 2022

Closes #437

The test case included in this PR has a simple reproduction of a scenario I've encountered where I want parse all options up to a non-option. All options after this argument should be considered unknown, including --.

Essentially, the use case that this accounts for is when a CLI provides two ways to indicate that the remainder of the argument list should not be parsed: by passing a non-option argument (enabling halt-at-non-option), or by passing -- (enabling populate--). In addition to this, unknown options provided before the script are collected separately by enabling unknown-options-as-args. In my use case, these unknown options are Node.js CLI args that I forward to a child process.

@kherock
Copy link
Contributor Author

kherock commented May 31, 2022

bump?

@kherock
Copy link
Contributor Author

kherock commented Jul 14, 2022

@bcoe anything I can do to get this merged? I would greatly appreciate having this fix generally available.

@@ -3008,6 +3008,16 @@ describe('yargs-parser', function () {
_: ['./file.js', '--foo', '--', 'barbar']
})
})

it('is not influenced by unknown options when "unknown-options-as-args" is true', function () {
Copy link
Member

Choose a reason for hiding this comment

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

What would the parsed output be prior to your fix? This output seems good, I'm just not 100% understanding the original bug.

Copy link
Contributor Author

@kherock kherock Jul 18, 2022

Choose a reason for hiding this comment

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

I just realized I must have screwed up the test assertion when I cleaned up these commits a while ago. I just amended the assertion so it matches the expected unknown-options-as-args behavior. Let me know if the output still looks good to you.

The full parsed output without the fix is

{ _: [ '-v', '--long', 'arg', './file.js', 'barbar' ], foo: true }

As you can see, yargs ignores halt-at-non-option and parses the --foo and -- options anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for clarifying, this fix looks right to me 👍

@bcoe bcoe merged commit c474bc1 into yargs:main Jul 19, 2022
@bcoe
Copy link
Member

bcoe commented Jul 19, 2022

@kherock thank you for the contribution.

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.

halt-at-non-option is incompatible with unknown-options-as-args
2 participants