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

pkgConf has no effect after the first usage #2191

Open
fasttime opened this issue May 24, 2022 · 8 comments
Open

pkgConf has no effect after the first usage #2191

fasttime opened this issue May 24, 2022 · 8 comments

Comments

@fasttime
Copy link

yargs v17.5.1

Currently, pkgConf only loads package.json options when it is called for the first time. All other usages are silently ignored. This makes it impossible to reuse the same instance of Yargs when the cwd or the key value changes.

const Yargs = require('yargs/yargs')([]);
const opts1 = Yargs.pkgConf('test', 'a').parse();
const opts2 = Yargs.pkgConf('test', 'b').parse();
const opts3 = Yargs.pkgConf('test2', 'a').parse();
console.log({ opts1, opts2, opts3 }); // opts1, opts2 and opts3 are the same(!)

In the example above, opts2 and opts3 always end up getting the same values as opts1, because only the first usage of pkgConf has an effect.

It would be helpful if those repeated usages would produce an error telling the user that they are doing something wrong, or even better, if using pkgConf multiple times was allowed.

@shadowspawn
Copy link
Member

shadowspawn commented Jul 19, 2023

You could make a new yargs instance for every parse.

const Yargs = require('yargs/yargs');
const opts1 = Yargs([]).pkgConf('test', 'a').parse();
const opts2 = Yargs([]).pkgConf('test', 'b').parse();

[Edit: changed "should make" to "could make".]

@fasttime

This comment was marked as outdated.

@shadowspawn

This comment was marked as outdated.

@shadowspawn

This comment was marked as outdated.

@fasttime

This comment was marked as outdated.

@shadowspawn

This comment was marked as outdated.

@shadowspawn

This comment was marked as outdated.

@shadowspawn
Copy link
Member

I misunderstood that Yargs does mostly make .parse() a non-breaking call. I am going to change my earlier "should make a new yargs instance for every parse" to "could...", hide some of my intermediate answers, and take another look at some point!

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

No branches or pull requests

2 participants