Yargs: Question: how to set multiple default config files?

Created on 31 Oct 2016  路  5Comments  路  Source: yargs/yargs

How can I configure yargs so that it accepts multiple config files as default? E.g. if ~/.foobar.json exists use that, otherwise use /etc/foobar.json.

I suspect this will soon get morphed into a "implement multiple default config files" issue, but oh well :P

enhancement inactive wontfix

Most helpful comment

Just cleaning up some old issues, and this has been inactive for quite some time

I'd advocate, that if someone has a need for merging multiple configs, it's probably better addressed in application logic (vs., us adding significantly more complexity to yargs' config loading).

All 5 comments

@strugee yargs doesn't currently support multiple config files, but we do allow you to pass an arbitrary config object to .config() rather than a config key.

I wonder if you could get away with a tiny bit of custom logic that attempts to read a few configuration files, and then passes which ever it successfully finds as an object to yargs.config()?

Another option would be to use the custom parseFn that config can take as a third parameter, in a similar way I think you could use this to check a cascading series of filenames.

@bcoe nice, sounds like a plan.

Would you take patches to implement this "properly," if I find time? I'm imagining logic where you could pass an array of filenames and it would check each one in order.

https://github.com/dominictarr/rc would be a convenient way to do this, except it has the regrettable duplication of minimist internally (which can be avoided by passing an empty object).

const argv = require('yargs')
    .config(require('rc')('appname', null, {}))
    .argv;

FWIW if anyone ends up stumbling across this in the future, here's how we did this in pump.io: https://github.com/pump-io/pump.io/blob/464668b2fc091313a6062426f5746663dfe914e5/bin/pump#L33

Bearing in mind that we also had backwards-compatibility to consider.

Just cleaning up some old issues, and this has been inactive for quite some time

I'd advocate, that if someone has a need for merging multiple configs, it's probably better addressed in application logic (vs., us adding significantly more complexity to yargs' config loading).

Was this page helpful?
0 / 5 - 0 ratings