Skip to content

Multiple arguments are incremented if they are equal to 1. #506

@dennis-grunert

Description

@dennis-grunert

Yargs version used: 18.0.0

How to reproduce

const parser = require('yargs-parser')
parser("-x 3 -x 1")

returns

{ _: [], x: 4 }

i.e., it wrongfully calculated 3 + 1 = 4.

Expected would be

{ _: [], x: [ 3, 1 ] }

The option "duplicate-arguments-array": false still results in 4.

If the order is changed, i.e.,

const parser = require('yargs-parser')
parser("-x 1 -x 3")

then the result is correct.

Bug Cause

The increment happens in yargs-parser.ts -> setKey at this code line:

if (value === increment()) {
   o[key] = increment(o[key])
} 

increment() === 1, i.e., it increments o[key] each time we have any argument which equals to 1.

The increment function has a comment on top stating that it "should only be called when a count is given as an arg". Obviously, we have a number and not a count here.

Workaround

A workaround in yargs/yargs consists in setting type="string".

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