yargs / yargs/yargs-parser

Multiple arguments are incremented if they are equal to 1.

Open
#506 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
519
Forks
129
Avg merge
1h 34m
Merged PRs (30d)
1

Description

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".

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/yargs-parser.ts at setKey, around the linked line, and inspect how increment() is selected when repeated values are parsed. Reproduce with parser("-x 3 -x 1") and the duplicate-arguments-array option; done means repeated numeric values produce [3, 1] rather than 4 while count arguments retain their existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.