Skip to content

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

@koraa

Description

@koraa

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'
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions