2011-11-20 21:21:09 +00:00
|
|
|
|
/*
|
|
|
|
|
* 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.
|
2011-11-20 21:21:09 +00:00
|
|
|
|
*
|
|
|
|
|
*/
|
2012-04-14 19:28:55 +00:00
|
|
|
|
|
2011-11-20 21:21:09 +00:00
|
|
|
|
var util = require('util'),
|
2017-10-21 19:39:16 +00:00
|
|
|
|
common = require('../common'),
|
2011-11-20 21:21:09 +00:00
|
|
|
|
Memory = require('./memory').Memory;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
|
2011-11-20 21:21:09 +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.
|
|
|
|
|
//
|
2013-02-23 17:32:42 +00:00
|
|
|
|
var Argv = exports.Argv = function (options, usage) {
|
2011-11-20 21:21:09 +00:00
|
|
|
|
Memory.call(this, options);
|
|
|
|
|
|
2017-10-21 19:39:16 +00:00
|
|
|
|
options = options || {};
|
2011-11-21 01:00:04 +00:00
|
|
|
|
this.type = 'argv';
|
|
|
|
|
this.readOnly = true;
|
2017-10-21 19:39:16 +00:00
|
|
|
|
this.options = options;
|
2013-02-23 17:32:42 +00:00
|
|
|
|
this.usage = usage;
|
2017-11-04 03:41:23 +00:00
|
|
|
|
if(typeof options.parseValues === 'boolean') {
|
|
|
|
|
this.parseValues = options.parseValues;
|
|
|
|
|
delete options.parseValues;
|
|
|
|
|
} else {
|
|
|
|
|
this.parseValues = false;
|
|
|
|
|
}
|
|
|
|
|
if (typeof options.transform === 'function') {
|
|
|
|
|
this.transform = options.transform;
|
|
|
|
|
delete options.transform;
|
|
|
|
|
} else {
|
|
|
|
|
this.transform = false;
|
|
|
|
|
}
|
2017-11-05 01:30:14 +00:00
|
|
|
|
if (typeof options.separator === 'string' || options.separator instanceof RegExp) {
|
|
|
|
|
this.separator = options.separator;
|
|
|
|
|
delete options.separator;
|
|
|
|
|
} else {
|
|
|
|
|
this.separator = '';
|
|
|
|
|
}
|
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 ()
|
2012-04-14 19:28:55 +00:00
|
|
|
|
// Loads the data passed in from the command-line arguments
|
2011-11-20 21:21:09 +00:00
|
|
|
|
// 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
|
|
|
|
|
2016-01-06 22:47:14 +00:00
|
|
|
|
yargs = isYargs(this.options) ?
|
|
|
|
|
this.options :
|
|
|
|
|
typeof this.options === 'object' ?
|
|
|
|
|
require('yargs')(process.argv.slice(2)).options(this.options) :
|
|
|
|
|
require('yargs')(process.argv.slice(2));
|
2013-02-23 17:32:42 +00:00
|
|
|
|
|
2015-07-07 20:06:41 +00:00
|
|
|
|
if (typeof this.usage === 'string') { yargs.usage(this.usage) }
|
2013-02-23 17:32:42 +00:00
|
|
|
|
|
2015-07-07 20:06:41 +00:00
|
|
|
|
argv = yargs.argv
|
2012-04-14 19:28:55 +00:00
|
|
|
|
|
2011-11-20 21:21:09 +00:00
|
|
|
|
if (!argv) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
|
2017-10-26 02:57:58 +00:00
|
|
|
|
if (this.transform) {
|
|
|
|
|
argv = common.transform(argv, this.transform);
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2017-10-21 19:39:16 +00:00
|
|
|
|
var val = argv[key];
|
|
|
|
|
|
|
|
|
|
if (typeof val !== 'undefined') {
|
|
|
|
|
if (self.parseValues) {
|
|
|
|
|
val = common.parseValues(val);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-05 01:30:14 +00:00
|
|
|
|
if (self.separator) {
|
|
|
|
|
self.set(common.key.apply(common, key.split(self.separator)), val);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
self.set(key, val);
|
|
|
|
|
}
|
2015-08-19 16:16:16 +00:00
|
|
|
|
}
|
2011-11-20 21:21:09 +00:00
|
|
|
|
});
|
2012-04-14 19:28:55 +00:00
|
|
|
|
|
2015-07-07 20:06:41 +00:00
|
|
|
|
this.showHelp = yargs.showHelp
|
|
|
|
|
this.help = yargs.help
|
2013-02-23 17:33:23 +00:00
|
|
|
|
|
2011-11-21 01:00:04 +00:00
|
|
|
|
this.readOnly = true;
|
2011-11-20 21:21:09 +00:00
|
|
|
|
return this.store;
|
2015-07-07 20:06:41 +00:00
|
|
|
|
};
|
2016-01-06 22:47:14 +00:00
|
|
|
|
|
|
|
|
|
function isYargs(obj) {
|
2017-10-28 17:49:46 +00:00
|
|
|
|
return (typeof obj === 'function' || typeof obj === 'object') && ('argv' in obj);
|
2016-01-06 22:47:14 +00:00
|
|
|
|
}
|