nconf-lite/lib/nconf/stores/argv.js

70 lines
1.5 KiB
JavaScript
Raw Normal View History

/*
* argv.js: Simple memory-based store for command-line arguments.
*
2014-11-26 06:31:48 +00:00
* (C) 2011, Charlie Robbins and the Contributors.
*
*/
2012-04-14 19:28:55 +00:00
var util = require('util'),
Memory = require('./memory').Memory;
2012-04-14 19:28:55 +00:00
//
// ### function Argv (options)
// #### @options {Object} Options for this instance.
// 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, usage) {
Memory.call(this, options);
this.type = 'argv';
this.readOnly = true;
this.options = options || false;
this.usage = usage;
};
// Inherit from the Memory store
util.inherits(Argv, Memory);
//
// ### function loadSync ()
// Loads the data passed in from `process.argv` into this instance.
//
Argv.prototype.loadSync = function () {
this.loadArgv();
return this.store;
};
//
// ### function loadArgv ()
2012-04-14 19:28:55 +00:00
// Loads the data passed in from the command-line arguments
// into this instance.
//
Argv.prototype.loadArgv = function () {
2012-04-14 19:28:55 +00:00
var self = this,
2015-07-07 20:06:41 +00:00
yargs, argv;
2012-04-14 19:28:55 +00:00
2015-07-07 20:06:41 +00:00
yargs = typeof this.options === 'object'
? require('yargs')(process.argv.slice(2)).options(this.options)
: require('yargs')(process.argv.slice(2));
2015-07-07 20:06:41 +00:00
if (typeof this.usage === 'string') { yargs.usage(this.usage) }
2015-07-07 20:06:41 +00:00
argv = yargs.argv
2012-04-14 19:28:55 +00:00
if (!argv) {
return;
}
2012-04-14 19:28:55 +00:00
this.readOnly = false;
Object.keys(argv).forEach(function (key) {
self.set(key, argv[key]);
});
2012-04-14 19:28:55 +00:00
2015-07-07 20:06:41 +00:00
this.showHelp = yargs.showHelp
this.help = yargs.help
this.readOnly = true;
return this.store;
2015-07-07 20:06:41 +00:00
};