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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/yargs-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export class YargsParser {
let value: string

// any unknown option (except for end-of-options, "--")
if (arg !== '--' && isUnknownOptionAsArg(arg)) {
if (arg !== '--' && /^-/.test(arg) && isUnknownOptionAsArg(arg)) {
pushPositional(arg)
// ---, ---=, ----, etc,
} else if (truncatedArg.match(/^---+(=|$)/)) {
Expand Down
10 changes: 10 additions & 0 deletions test/yargs-parser.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 👍

const parse = parser(
['-v', '--long', 'arg', './file.js', '--foo', '--', 'barbar'],
{ configuration: { 'halt-at-non-option': true, 'unknown-options-as-args': true }, boolean: ['foo'] }
)
parse.should.deep.equal({
_: ['-v', '--long', 'arg', './file.js', '--foo', '--', 'barbar']
})
})
})

describe('unknown-options-as-args = true', function () {
Expand Down