From 4894c8fcf7825270f6ee6f43a6fe73570c814695 Mon Sep 17 00:00:00 2001 From: Johnny Domino Date: Sat, 23 Feb 2013 12:32:42 -0500 Subject: [PATCH 1/2] resolves #64 passing usage string to optimist --- lib/nconf/provider.js | 14 +++++++++----- lib/nconf/stores/argv.js | 15 ++++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index 413df5f..e0aeb42 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -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); }; @@ -121,7 +125,7 @@ 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; @@ -129,7 +133,7 @@ Provider.prototype.add = function (name, options) { 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(); @@ -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); }; // diff --git a/lib/nconf/stores/argv.js b/lib/nconf/stores/argv.js index 3841f7d..b130ce3 100644 --- a/lib/nconf/stores/argv.js +++ b/lib/nconf/stores/argv.js @@ -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 @@ -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; From b73b0e1a3746a67a7dfceaacc655ba22fc1ca9a3 Mon Sep 17 00:00:00 2001 From: Johnny Domino Date: Sat, 23 Feb 2013 12:33:23 -0500 Subject: [PATCH 2/2] attach help and showHelp arguments to the argv store --- lib/nconf/stores/argv.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/nconf/stores/argv.js b/lib/nconf/stores/argv.js index b130ce3..60c4171 100644 --- a/lib/nconf/stores/argv.js +++ b/lib/nconf/stores/argv.js @@ -61,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; }; \ No newline at end of file