Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves #64, usage and help #76

Merged
merged 2 commits into from
Nov 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/nconf/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ var Provider = exports.Provider = function (options) {
// Define wrapper functions for using basic stores
// 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) {
return this.add(type, options);
};
Expand Down Expand Up @@ -121,15 +125,15 @@ Provider.prototype.use = function (name, options) {
// provider.add('memory');
// provider.add('userconf', { type: 'file', filename: '/path/to/userconf' })
//
Provider.prototype.add = function (name, options) {
Provider.prototype.add = function (name, options, usage) {
options = options || {};
var type = options.type || name;

if (!require('../nconf')[common.capitalize(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) {
this.stores[name].loadSync();
Expand Down Expand Up @@ -157,8 +161,8 @@ Provider.prototype.remove = function (name) {
// Creates a store of the specified `type` using the
// specified `options`.
//
Provider.prototype.create = function (type, options) {
return new (require('../nconf')[common.capitalize(type.toLowerCase())])(options);
Provider.prototype.create = function (type, options, usage) {
return new (require('../nconf')[common.capitalize(type.toLowerCase())])(options, usage);
};

//
Expand Down
18 changes: 13 additions & 5 deletions lib/nconf/stores/argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ var util = require('util'),
// Constructor function for the Argv nconf store, a simple abstraction
// 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);

this.type = 'argv';
this.readOnly = true;
this.options = options || false;
this.usage = usage;
};

// Inherit from the Memory store
Expand All @@ -41,11 +42,15 @@ Argv.prototype.loadSync = function () {
//
Argv.prototype.loadArgv = function () {
var self = this,
argv;
optimist, argv;

argv = typeof this.options === 'object'
? require('optimist')(process.argv.slice(2)).options(this.options).argv
: require('optimist')(process.argv.slice(2)).argv;
optimist = typeof this.options === 'object'
? require('optimist')(process.argv.slice(2)).options(this.options)
: require('optimist')(process.argv.slice(2));

if (typeof this.usage === 'string') { optimist.usage(this.usage) }

argv = optimist.argv

if (!argv) {
return;
Expand All @@ -56,6 +61,9 @@ Argv.prototype.loadArgv = function () {
self.set(key, argv[key]);
});

this.showHelp = optimist.showHelp
this.help = optimist.help

this.readOnly = true;
return this.store;
};