Comments (10)
I ran into this problem as well, and had to work around this using a trick:
- if you would like to overwrite the boolean param to be true, assign it to be 'true' in your command line
- if you would like to overwrite the boolean param to be false, assign it to be empty string '' instead of 'false' in your command line
In this way, your app does not require any change at all
from nconf.
I agree that parsing "true" and "false" into booleans would be useful. Right now I am using optimist to load any boolean parameters but it would be simpler to just use nconf
from nconf.
What if you'd like myBool (or any variable) to actually be the string "true" though? Some sort of flag at the command-line level would likely be needed, adding complexity.
My suggestion would be to cast it to a bool on your end, like so:
!!nconf.get('myBool'); // => true if truthy, false if falsy.
from nconf.
I think it's more common to use true and false as booleans. It makes more sense to apply the manual cast in case of a string usage of myBool because it would happen less often than the other way round. Don't you agree?
from nconf.
In theory you could relegate command line arguments to being flags proper, i.e., booleans, and have environment variables and config file settings' values still be parsed as strings, but you'd probably have to convince the author, Charlie Robbins, that argv should always be booleans, which would break backwards compatibility. It'd probably be worth surveying usage patterns, but I don't think we should assume that command line flags are always meant to be booleans; many CLI programs that are configurable via flags accept various types. Type -h or --help with any number of *nix programs to see what I mean.
I'm not saying I don't sympathize with your want, but perhaps there's some sort of compromise?
from nconf.
Or nconf could be updated to use commander, which in addition to being more current and widely used than optimist, has the advantage of having typed arguments with coercion (solving this exact problem). And as a bonus, commander is also not deprecated ;)
from nconf.
Any updates on this? Had to deal with this situation myself today.
@BobDickinson (old) suggestion seemed reasonable at the time. I'm throwing yargs into the mix as well.
EDIT: Don't mind me. I noticed nconf is already using yargs.
from nconf.
Our workaround is that we create a memory store and read the env vars into it (filtering to just pick the ones we want, and converting booleans and null):
conf.use("memory");
var memStore = conf.stores["memory"];
Object.keys(process.env).filter(function (key)
{
return true; // Filter here as appropriate
}).forEach(function (key)
{
var value = process.env[key];
if (value == "true")
{
value = true;
}
else if (value == "false")
{
value = false;
}
else if (value == "null")
{
value = null;
}
memStore.set(key, value);
});
memStore.readOnly = true;
from nconf.
@BobDickinson You could consider using JSON.parse
instead of that chain of conditionals.
Also, regarding boolean values, what about using simple yargs
flag?
So, instead of --foo=true
(which will be parsed as 'foo': 'true'
), you could just use --foo
and get the result you want. Set the default value to false
and that's it.
EDIT: This was suggested before here: #72 (comment)
In theory you could relegate command line arguments to being flags proper
from nconf.
JSON.parse() assumes that the value being parsed is valid JSON. I have no idea what values might be in cmd line args or env vars and whether they will be valid JSON. For example, a string with a brace in the middle might be a perfectly valid config value, but will throw an exception if you call JSON.parse on it. And if the string was actually a JSON object, using the output of JSON.parse would change the behavior by parsing that as an object instead of treating it like a string (you might want that behavior, but it's definitely a different contract than the behavior I was trying to fix).
My goal was to fix a specific problem without causing any unintended side-effects, and I prefer to be explicit as opposed to trying to save a couple of lines of code. YMMV.
from nconf.
Related Issues (20)
- yargs dependency is not updated HOT 2
- 0.12 release for ini vulnerability HOT 7
- 'Env' Store is Loaded Twice HOT 3
- Write docs for upgrading to v1.0.0
- Dependency Dashboard
- [Question/issue] How to load hierarchical settings from env
- Security vulnerability in nested ansi-regex package HOT 4
- esbuild cannot bundle nconf HOT 6
- Patch the yargs package to resolve security issue in ansi-args. HOT 1
- env doesn't append array values to default HOT 2
- Incorrect nesting of Object when value is Object and Object properties contain a colon
- How to stop yargs handling --help, --version etc HOT 2
- new release of nconf needed to address CVE-2021-43138 HOT 5
- Inconsistency between npm artifact and source code in v0.x branch HOT 3
- When using multiple files how to save to specific one? HOT 3
- async should be a dependency
- config json file is empty (all data is wiped out) HOT 2
- Is nconf abandoned? HOT 3
- Does nconf work with multiple files? HOT 5
- file.secure option is incompatible with NodeJS 22
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nconf.