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

parserConfiguration "strip-aliased" causes "coerce" not be applied for expanded-camelcase #2307

Closed
gomain opened this issue Jan 12, 2023 · 5 comments · Fixed by #2308
Closed

Comments

@gomain
Copy link

gomain commented Jan 12, 2023

$ node -e 'const p = require("yargs")(["--n-e", "1"]).option("n-e", { coerce: n => n + 1}).parserConfiguration({ "strip-aliased": true }).parse(); console.log(p)'
{ _: [], 'n-e': 2, nE: 1, '$0': '' }

without is ok

$ node -e 'const p = require("yargs")(["--n-e", "1"]).option("n-e", { coerce: n => n + 1}).parse(); console.log(p)'
{ _: [], 'n-e': 2, nE: 2, '$0': '' }
@shadowspawn
Copy link
Member

I was not able to reproduce this with yargs-parser@21.1.1

const argv = require('yargs-parser')(["--n-e", "1"], 
    {
        coerce: { 'n-e': n => n + 1 }, 
        // alias: { 'n-e': 'not-ever' },
        configuration: { 
            'strip-aliased': true,
        }
    }
);
console.log(argv)
% node parser.js --n-e 1
{ _: [], 'n-e': 2, nE: 2 }

@shadowspawn
Copy link
Member

I can reproduce the issue calling yargs, like in your reproduction steps. I think the bug is downstream in yargs.

@shadowspawn shadowspawn transferred this issue from yargs/yargs-parser Mar 7, 2023
@shadowspawn
Copy link
Member

Possibly related: #2159

@shadowspawn
Copy link
Member

At this point in the parsing, the aliases includes an entry for the camel-case version of the option, and not just the user supplied aliases. This might be the source of the problem.

if (aliases[keys] && stripAliased !== true) {

{
  aliases: { help: [], version: [], 'n-e': [ 'nE' ], nE: [ 'n-e' ] },
  keys: 'n-e'
}

@shadowspawn
Copy link
Member

There were problems mixing coerce with parser configuration which removes keys from argv. I took a more general approach in #2308 to remove assumptions about configuration.

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

Successfully merging a pull request may close this issue.

2 participants