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

Unknown arguments cause incorrect command positional coerce() #1457

Closed
evocateur opened this issue Oct 22, 2019 · 0 comments · Fixed by #1459
Closed

Unknown arguments cause incorrect command positional coerce() #1457

evocateur opened this issue Oct 22, 2019 · 0 comments · Fixed by #1459

Comments

@evocateur
Copy link
Contributor

As evidenced in lerna/lerna#2315, when arguments are passed after -- with the default parserConfiguration (append them to argv._), yargs@14.2.0 causes an incorrect coerce("") call on a command's positional argument, as well as re-parsing any flags (which fails when strict() is enabled).

require('yargs')
  .strict()
  .option('loglevel', {
    type: 'string',
  })
  .command('repro [stuff]', 'example', yargs => {
    yargs.positional('stuff', {
      describe: 'things',
      coerce: stuff => {
        if (stuff === '') {
          throw new Error('fail');
        }
        return stuff;
      },
    });
  }, argv => {
    console.log(argv);
  })
  .parse('repro -- --loglevel=woo');

It also causes the valid --loglevel option in the example above to fail, once the coerce() method is modfied to ensure that the parameter is in fact non-empty first (or if it's an identity function stuff => stuff, or removed entirely). So that's fun.

The current workaround is to enable populate-- in yargs-parser:

  .parserConfiguration({
    "populate--": true,
  })

This seems sub-optimal.

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

Successfully merging a pull request may close this issue.

2 participants