2011-11-20 21:21:09 +00:00
|
|
|
/*
|
|
|
|
* argv.js: Simple memory-based store for command-line arguments.
|
|
|
|
*
|
2011-11-24 05:33:08 +00:00
|
|
|
* (C) 2011, Nodejitsu Inc.
|
2011-11-20 21:21:09 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var util = require('util'),
|
|
|
|
Memory = require('./memory').Memory;
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### 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) {
|
|
|
|
Memory.call(this, options);
|
|
|
|
|
2011-11-21 01:00:04 +00:00
|
|
|
this.type = 'argv';
|
|
|
|
this.readOnly = true;
|
|
|
|
this.options = options || false;
|
2011-11-20 21:21:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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 ()
|
|
|
|
// Loads the data passed in from the command-line arguments
|
|
|
|
// into this instance.
|
|
|
|
//
|
|
|
|
Argv.prototype.loadArgv = function () {
|
|
|
|
var self = this,
|
|
|
|
argv;
|
|
|
|
|
|
|
|
argv = typeof this.options === 'object'
|
2011-11-23 02:22:09 +00:00
|
|
|
? require('optimist')(process.argv.slice(2)).options(this.options).argv
|
|
|
|
: require('optimist')(process.argv.slice(2)).argv;
|
2011-11-20 21:21:09 +00:00
|
|
|
|
|
|
|
if (!argv) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-21 01:00:04 +00:00
|
|
|
this.readOnly = false;
|
2011-11-20 21:21:09 +00:00
|
|
|
Object.keys(argv).forEach(function (key) {
|
|
|
|
self.set(key, argv[key]);
|
|
|
|
});
|
|
|
|
|
2011-11-21 01:00:04 +00:00
|
|
|
this.readOnly = true;
|
2011-11-20 21:21:09 +00:00
|
|
|
return this.store;
|
|
|
|
};
|