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

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

Open
D-Pow opened this issue Oct 17, 2021 · 2 comments
Open

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

D-Pow opened this issue Oct 17, 2021 · 2 comments
Labels

Comments

@D-Pow
Copy link

D-Pow commented Oct 17, 2021

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

@bcoe
Copy link
Member

bcoe commented Nov 8, 2021

@D-Pow thank you for the bug report, would happily accept a patch 👌

@D-Pow
Copy link
Author

D-Pow commented Nov 8, 2021

Thank you! It's much appreciated, especially now that nullish coalescing is supported natively in all NodeJS LTS >=14.5.x (haven't tried anything earlier than that so can't speak for them).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants