yargs / yargs/yargs-parser

Default value of `false` doesn't work for `narg: { flag: 0 }`

Open
#418 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

yargs-parser doesn't seem to work when a boolean flag is defaulted to false and then activated from the args string. For example:

const YargsParser = require('yargs-parser');

YargsParser('-a hello -b world', {
    narg: {
        a: 1,
        b: 0,
    },
    default: {
        b: false,
    },
    alias: {
        blah: [ 'b', 'bl' ],
    },
});


// expected output
{
    _: ['world'],
    a: 'hello',
    blah: true,
    b: true,
    bl: true
}


// actual output
{
    _: ['world'],
    a: 'hello',
    blah: false,
    b: false,
    bl: false
}

If I explicitly marked it as boolean: true, it seems to work as expected. However, the problem is if the option/yargs-parser configuration had boolean replaced with narg to allow the flag value to be of multiple types (falling back to a boolean if no value given), then setting it solely as boolean will break the functionality of the other values it could be.

Admittedly, parsedArgs.b would be undefined if it weren't defaulted which is still falsey, but this still came as a surprise to me since narg: { blah: 0 } should be enough to mark it as a boolean. It's not the end of the world but would be nice to not have this gotcha present in the parser that breaks if the user happens to add a default value of false.

Related

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 by reproducing the shown YargsParser invocation with narg, default, and alias options, then trace how the parser handles a flag activated from the args string when its default is false. Done means the example returns true for b, blah, and bl while preserving support for other narg values.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.