Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Argv store separator (#291)
* argv store now accept a separator argument to create nested values

* remove stub file that shouldnt have been commited

* write a test to ensure separator is working well and use delete rather than undefined assign
  • Loading branch information
AdrieanKhisbe authored and Matt Hamann committed Dec 18, 2017
1 parent bac910a commit 16667be
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 28 deletions.
14 changes: 12 additions & 2 deletions lib/nconf/stores/argv.js
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
});

Expand Down
@@ -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')
}));
38 changes: 37 additions & 1 deletion test/hierarchy-test.js
Expand Up @@ -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,
Expand Down
25 changes: 0 additions & 25 deletions test/nconf-argv-test.js

This file was deleted.

0 comments on commit 16667be

Please sign in to comment.