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

Dash inside option name with parser option strip-dashed yields undefined value #2311

Closed
koraa opened this issue May 4, 2021 · 4 comments · Fixed by #2308
Closed

Dash inside option name with parser option strip-dashed yields undefined value #2311

koraa opened this issue May 4, 2021 · 4 comments · Fixed by #2308
Labels

Comments

@koraa
Copy link

koraa commented May 4, 2021

This is a bug report; minimal example of the bug:

$ node -e "console.log(require('yargs').parserConfiguration({ 'strip-dashed': true}).option('foo-bar', { coerce: (v) => v }).parse(['executable', '--foo-bar', 'hello']))"

outputs:

{
  _: [ 'executable' ],
  fooBar: undefined,
  '$0': '',
  'foo-bar': undefined
}

This is incorrect. The value of 'fooBar' should be 'hello', not undefined and the 'foo-bar' property should be entirely removed.

This bug does not appear with options not containing a dash:

node -e "console.log(require('yargs').parserConfiguration({ 'strip-dashed': true}).option('foobar', { coerce: (v) => v }).parse(['executable', '--foobar', 'hello']))"

yields the following correct output:

{ _: [ 'executable' ], foobar: 'hello', '$0': '' }

Workaround No 1

Setting 'strip-dashed' to false yields correct behaviour:

node -e "console.log(require('yargs').parserConfiguration({ 'strip-dashed': false }).option('foo-bar', { coerce: (v) => v }).parse(['executable', '--foo-bar', 'hello']))"

resulting js:

{ _: [ 'executable' ], 'foo-bar': 'hello', fooBar: 'hello', '$0': '' }

Woraround No 2

Using an alias also yields less wrong behavior (still no stripping).

node -e "console.log(require('yargs').parserConfiguration({ 'strip-dashed': true}).option('foobar', { alias: ['foo-bar'], coerce: (v) => v }).parse(['executable', '--foo-bar', 'hello']))"

Output:

{
  _: [ 'executable' ],
  foobar: 'hello',
  fooBar: 'hello',
  '$0': '',
  'foo-bar': 'hello'
}
@bcoe bcoe transferred this issue from yargs/yargs May 6, 2021
@bcoe bcoe added the bug label May 6, 2021
@bcoe
Copy link
Member

bcoe commented May 6, 2021

@koraa thank you for the bug report. I went ahead and transferred this over to yargs-parser, which is potentially where the bug will need to be fixed.

Although, the fact that coerce is now handled in yargs means you it could also be an issue there 🤔

@marikaner
Copy link

I just noticed a similar (probably the same) bug: I have options that are paths (e.g. outputDir), I used a parser config:

    .parserConfiguration({
      'strip-aliased': true,
      'strip-dashed': true
    })

and configured the path options with normalize. With this combination I always get output-dir and o (alias) in the parsed object.

@shadowspawn
Copy link
Member

Thanks for the report. I recently looked into some related issues in yargs@17.x and opened a PR to (hopefully) fix the interaction of coerce and non-default configuration settings. I am confident enough that this will fix the issues, I'll move this issue back to yargs!

#2308

@shadowspawn shadowspawn transferred this issue from yargs/yargs-parser Mar 12, 2023
@shadowspawn
Copy link
Member

Clever having the reproduction on the command-line. The behaviour has changed in current yargs version, but still broken as coerce is not run:

% node -e "console.log(require('yargs').parserConfiguration({ 'strip-dashed': true}).option('foo-bar', { coerce: (v) => v.toUpperCase() }).parse(['executable', '--foo-bar', 'hello']))"

{ _: [ 'executable' ], fooBar: 'hello', '$0': '' }

Running against #2308 code:

% node -e "console.log(require('yargs').parserConfiguration({ 'strip-dashed': true}).option('foo-bar', { coerce: (v) => v.toUpperCase() }).parse(['executable', '--foo-bar', 'hello']))"

{ _: [ 'executable' ], fooBar: 'HELLO', '$0': '' }

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 a pull request may close this issue.

4 participants