-
Notifications
You must be signed in to change notification settings - Fork 252
/
argv.js
71 lines (58 loc) · 1.58 KB
/
argv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* argv.js: Simple memory-based store for command-line arguments.
*
* (C) 2011, Charlie Robbins and the Contributors.
*
*/
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, 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 ()
// Loads the data passed in from the command-line arguments
// into this instance.
//
Argv.prototype.loadArgv = function () {
var self = this,
yargs, argv;
yargs = typeof this.options === 'object'
? require('yargs')(process.argv.slice(2)).options(this.options)
: require('yargs')(process.argv.slice(2));
if (typeof this.usage === 'string') { yargs.usage(this.usage) }
argv = yargs.argv
if (!argv) {
return;
}
this.readOnly = false;
Object.keys(argv).forEach(function (key) {
if (typeof argv[key] !== 'undefined') {
self.set(key, argv[key]);
}
});
this.showHelp = yargs.showHelp
this.help = yargs.help
this.readOnly = true;
return this.store;
};