Merge pull request #76 from jmonster/usage

Resolves #64, usage and help
This commit is contained in:
Charlie Robbins 2014-11-26 00:59:36 -05:00
commit a3404b4062
2 changed files with 22 additions and 10 deletions

View file

@ -29,7 +29,11 @@ var Provider = exports.Provider = function (options) {
// Define wrapper functions for using basic stores // Define wrapper functions for using basic stores
// in this instance // in this instance
// //
['argv', 'env'].forEach(function (type) { Provider.prototype['argv'] = function(options, usage) {
return this.add('argv', options, usage);
};
['env'].forEach(function (type) {
Provider.prototype[type] = function (options) { Provider.prototype[type] = function (options) {
return this.add(type, options); return this.add(type, options);
}; };
@ -121,7 +125,7 @@ Provider.prototype.use = function (name, options) {
// provider.add('memory'); // provider.add('memory');
// provider.add('userconf', { type: 'file', filename: '/path/to/userconf' }) // provider.add('userconf', { type: 'file', filename: '/path/to/userconf' })
// //
Provider.prototype.add = function (name, options) { Provider.prototype.add = function (name, options, usage) {
options = options || {}; options = options || {};
var type = options.type || name; var type = options.type || name;
@ -129,7 +133,7 @@ Provider.prototype.add = function (name, options) {
throw new Error('Cannot add store with unknown type: ' + type); throw new Error('Cannot add store with unknown type: ' + type);
} }
this.stores[name] = this.create(type, options); this.stores[name] = this.create(type, options, usage);
if (this.stores[name].loadSync) { if (this.stores[name].loadSync) {
this.stores[name].loadSync(); this.stores[name].loadSync();
@ -157,8 +161,8 @@ Provider.prototype.remove = function (name) {
// Creates a store of the specified `type` using the // Creates a store of the specified `type` using the
// specified `options`. // specified `options`.
// //
Provider.prototype.create = function (type, options) { Provider.prototype.create = function (type, options, usage) {
return new (require('../nconf')[common.capitalize(type.toLowerCase())])(options); return new (require('../nconf')[common.capitalize(type.toLowerCase())])(options, usage);
}; };
// //

View file

@ -14,12 +14,13 @@ var util = require('util'),
// Constructor function for the Argv nconf store, a simple abstraction // Constructor function for the Argv nconf store, a simple abstraction
// around the Memory store that can read command-line arguments. // around the Memory store that can read command-line arguments.
// //
var Argv = exports.Argv = function (options) { var Argv = exports.Argv = function (options, usage) {
Memory.call(this, options); Memory.call(this, options);
this.type = 'argv'; this.type = 'argv';
this.readOnly = true; this.readOnly = true;
this.options = options || false; this.options = options || false;
this.usage = usage;
}; };
// Inherit from the Memory store // Inherit from the Memory store
@ -41,11 +42,15 @@ Argv.prototype.loadSync = function () {
// //
Argv.prototype.loadArgv = function () { Argv.prototype.loadArgv = function () {
var self = this, var self = this,
argv; optimist, argv;
argv = typeof this.options === 'object' optimist = typeof this.options === 'object'
? require('optimist')(process.argv.slice(2)).options(this.options).argv ? require('optimist')(process.argv.slice(2)).options(this.options)
: require('optimist')(process.argv.slice(2)).argv; : require('optimist')(process.argv.slice(2));
if (typeof this.usage === 'string') { optimist.usage(this.usage) }
argv = optimist.argv
if (!argv) { if (!argv) {
return; return;
@ -56,6 +61,9 @@ Argv.prototype.loadArgv = function () {
self.set(key, argv[key]); self.set(key, argv[key]);
}); });
this.showHelp = optimist.showHelp
this.help = optimist.help
this.readOnly = true; this.readOnly = true;
return this.store; return this.store;
}; };