diff --git a/lib/nconf/stores/argv.js b/lib/nconf/stores/argv.js index 881339a..ac00613 100644 --- a/lib/nconf/stores/argv.js +++ b/lib/nconf/stores/argv.js @@ -35,7 +35,12 @@ var Argv = exports.Argv = function (options, usage) { } else { this.transform = false; } - + if (typeof options.separator === 'string' || options.separator instanceof RegExp) { + this.separator = options.separator; + delete options.separator; + } else { + this.separator = ''; + } }; // Inherit from the Memory store @@ -86,7 +91,12 @@ Argv.prototype.loadArgv = function () { val = common.parseValues(val); } - self.set(key, val); + if (self.separator) { + self.set(common.key.apply(common, key.split(self.separator)), val); + } + else { + self.set(key, val); + } } }); diff --git a/test/fixtures/scripts/nconf-hierarchical-load-merge-with-separator.js b/test/fixtures/scripts/nconf-hierarchical-load-merge-with-separator.js new file mode 100644 index 0000000..a58a4b6 --- /dev/null +++ b/test/fixtures/scripts/nconf-hierarchical-load-merge-with-separator.js @@ -0,0 +1,19 @@ +/* + * nconf-hierarchical-load-merge.js: Test fixture for loading and merging nested objects across stores. + * + * (C) 2012, Charlie Robbins and the Contributors. + * (C) 2012, Michael Hart + * + */ + +var path = require('path'), + nconf = require('../../../lib/nconf'); + +nconf.argv({separator: '--'}) + .env('__') + .file(path.join(__dirname, '..', 'merge', 'file1.json')); + +process.stdout.write(JSON.stringify({ + apples: nconf.get('apples'), + candy: nconf.get('candy') +})); diff --git a/test/hierarchy-test.js b/test/hierarchy-test.js index de8dc4a..a76e2b5 100644 --- a/test/hierarchy-test.js +++ b/test/hierarchy-test.js @@ -111,7 +111,43 @@ vows.describe('nconf/hierarchy').addBatch({ }); } }, - "configured with .file(), .defaults() should deep merge objects": { + "configured with .argv() and separator, .file() and invoked with nested command line options": { + topic: function () { + var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-load-merge-with-separator.js'), + argv = ['--candy--something', 'foo', '--candy--something5--second', 'bar'], + that = this, + data = '', + child; + process.env.candy__bonbon = 'sweet'; + child = spawn('node', [script].concat(argv)); + delete process.env.candy__bonbon; + child.stdout.on('data', function (d) { + data += d; + }); + + child.on('close', function() { + that.callback(null, data); + }); + }, + "should merge nested objects ": function (err, data) { + console.log(data) + assert.deepEqual(JSON.parse(data), { + apples: true, + candy: { + bonbon: 'sweet', + something: 'foo', + something1: true, + something2: true, + something5: { + first: 1, + second: 'bar' + } + } + }); + } + }, + + "configured with .file(), .defaults() should deep merge objects": { topic: function () { var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-defaults-merge.js'), that = this, diff --git a/test/nconf-argv-test.js b/test/nconf-argv-test.js deleted file mode 100644 index ab1842b..0000000 --- a/test/nconf-argv-test.js +++ /dev/null @@ -1,25 +0,0 @@ -/* - * file-store-test.js: Tests for the nconf File store. - * - * (C) 2011, Charlie Robbins and the Contributors. - * - */ - -var fs = require('fs'), - path = require('path'), - vows = require('vows'), - assert = require('assert'), - nconf = require('../lib/nconf'), - yargs = require('yargs') - -vows.describe('nconf/argv').addBatch({ - "When using the nconf": { - "with a custom yargs": { - topic: function () { - fs.readFile(path.join(__dirname, '..', 'package.json'), this.callback); - }, - }, - "with the default yars": { - } - } -}).export(module);