Simple node.js config file and arguments parser.
MIT License
The goal of this project is to provide a simple module that can load a JSON defaults file, read a flat configuration file and parse command line arguments.
Usually an application needs a basic defaults
structure, some config files
that override defaults and command line arguments
that override the resulting
object.
The usual scenario is when Flat Config loads a JSON file for defaults,
then loads a number of key-subkey=value
style configuration files and
mixes that with command line arguments.
You may also provide Flat Config with any existing javascript object and it will work. You can also provide an array of configuration entries instead of the config file if you feel more comfortable with that.
A basic example, the defaults:
{
"app": {
"mainmodule": "lib/main",
"port": 8080
},
"limit": {
"connections": 30,
"entries": 4096
},
"quiet": true,
"test": true
}
The config file (/etc/myapp/myconfig):
# a comment
app.port=3080
# another comment
limit.connections=20
entries=1024
The application loader (say app.js):
// Initialize flatconfig
var flatconfig = require('flatconfig');
// Load the configuration
var args = flatconfig.setArgs(process.argv.slice(2)),
cfg = args['config'];
// remove some 'special' command line args not found in config.
delete args['config'];
var config = flatconfig.load(
path.resolve(__dirname, 'cfg.json'),
path.resolve(process.cwd(), cfg),
args),
var app = require(config.app.mainmodule);
app.init(config);
The usage:
node app --config=/etc/myapp/myconfig --no-test --no-quiet --app-port=4080
The parser is based on traditional unix 'long-style' arguments, something like
--a-key=a\ value
.
Every hyphen in the config/argument name causes the parser will attempt to descend to the child object.
If the argument name starts with --no-*
the parser will set the value to
false
.
If the value is not given (--argument
) the parser will set the value to
true
.
See the example above.
A helper method that loads or uses the defaults, configuration and arguments. Returns modified defaults (the resulting config object).
Parameters are:
process.argv.slice(2)
Allows you to deflatten an ini file to the default config structure. Returns modified defaults (the resulting config object).
Parameters are:
Allows you to deflatten command line arguments to the default config structure. Returns modified defaults (the resulting config object).
Parameters are:
To be documented
Parses the arguments to a flat hash.
Parameters:
This is a helper function to flatten an object to a flat parameter hash.
Might be helpful to create a basic config file by listing all parameters within the defaults file.
Parameters:
This is a helper function to deflatten a flat parameter hash to an object.
Might be helpful to create a basic config file by listing all parameters within the defaults file.
Parameters:
This method will throw an error if a key is not found.
Deprecated: use compatible flatconfig.setArgs instead.
Method will be removed in 1.0.
Done:
parseArgs
)(The MIT License)
Copyright (c) 2012 Michał Czapracki [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.